● JPetStore用户管理分析
JPetStore将登陆后的用户ActionForm保持于Session作用域中,在页面中判断该ActionForm中的用户信息,实现了简单的用户验证。
Ø 进入用户管理
<a href="<c:url value="/shop/signonForm.do"/>">
Ø Struts-config.xml配置
<action path="/shop/signonForm"
type="org.springframework.samples.jpetstore.web.struts.DoNothingAction"
validate="false">
<forward name="success" path="/WEB-INF/jsp/struts/SignonForm.jsp"/>
</action>
Ø 进入用户登陆的SignonForm.jsp
… …
<form action="<c:url value="/shop/signon.do"/>" method="POST">
<c:if test="${!empty signonForwardAction}">
<input type="hidden" name="forwardAction"
value="<c:url value="${signonForwardAction}"/>"/>
</c:if>
<td colspan="2">Please enter your username and password.
<td><input type="text" name="username" value="j2ee" /></td>
<td><input type="password" name="password" value="j2ee" /></td>
<td><input type="image" border="0" src="../images/button_submit.gif" name="update"
<a href="<c:url value="/shop/newAccountForm.do"/>">
<img border="0" src="../images/button_register_now.gif" />
</a>
</form>
… …
Ø SignonAction类
i) <action path="/shop/signon"
type="org.springframework.samples.jpetstore.web.struts.SignonAction"
name="accountForm" scope="session" validate="false">
<forward name="success" path="/shop/index.do"/>
</action>
ii) <form-bean name="accountForm"
type="org.springframework.samples.jpetstore.web.struts.AccountActionForm"/>
iii) SignonAction
public class SignonAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
request.getSession().removeAttribute("workingAccountForm");
request.getSession().removeAttribute("accountForm");
//如果用户注销,则清空Session
if (request.getParameter("signoff") != null) {
request.getSession().invalidate();
return mapping.findForward("success");
}
else {
AccountActionForm acctForm = (AccountActionForm) form;
String username = acctForm.getUsername();
String password = acctForm.getPassword();
//用户验证
Account account = getPetStore().getAccount(username, password);
if (account == null) {
request.setAttribute("message", "Invalid username or password. Signon failed.");
return mapping.findForward("failure");
}
else {
String forwardAction = acctForm.getForwardAction();
acctForm = new AccountActionForm();
acctForm.setForwardAction(forwardAction);
acctForm.setAccount(account);
acctForm.getAccount().setPassword(null);
PagedListHolder myList = new PagedListHolder(getPetStore().getProductListByCategory(account.getFavouriteCategoryId()));
myList.setPageSize(4);
acctForm.setMyList(myList);
//用户通过验证后,将ActiongFrom存入Session
request.getSession().setAttribute("accountForm", acctForm);
if (acctForm.getForwardAction() == null || acctForm.getForwardAction().length() < 1) {
return mapping.findForward("success");
}
else {
response.sendRedirect(acctForm.getForwardAction());
return null;
}
}
}
}
}
iv)
<!—查找Session中是否存在accountForm.account对象,以此判断用户是否登陆-->
<c:if test="${!empty accountForm.account}">
<b><i><font size="2" color="BLACK">Welcome
<!—如果已经登陆,显示欢迎信息-->
<c:out value="${accountForm.account.firstName}"/>!</font></i></b>
</c:if>
<!—查找Session中是否存在accountForm.account对象,以此判断用户是否登陆-->
<c:if test="${!empty accountForm.account}" >
<!—如果已经登陆,显示修改用户信息URL-->
<a href="<c:url value="/shop/signon.do?signoff=true"/>">
<img border="0" name="img_signout" src="../images/sign-out.gif" /></a>
<img border="0" src="../images/separator.gif" />
<a href="<c:url value="/shop/editAccountForm.do"/>">
<img border="0" name="img_myaccount" src="../images/my_account.gif" /></a>
</c:if>
v)
<action path="/shop/editAccountForm" type="org.springframework.samples.jpetstore.web.struts.EditAccountFormAction"
name="workingAccountForm" scope="session" validate="false">
<forward name="success" path="/WEB-INF/jsp/struts/EditAccountForm.jsp"/>
</action>
<form-bean name="workingAccountForm" type="org.springframework.samples.jpetstore.web.struts.AccountActionForm"/>
大概看了看,总的来说,JPetStore的应用逻辑还是比较简单,JpetStore的学习和分析也就到此结束。