1.在UserAction中添加三个方法,如下:
/** 登陆页面 */
public String loginUI() throws Exception {
return "loginUI";
}
/** 登陆*/
public String login() throws Exception {
User user=userService.findByLoginNameAndPassword(model.getLoginName(),model.getPassword()); //注意,此方法需要在service层实现,具体代码在步骤2
if(user==null){
//登陆失败,提示相关消息
addFieldError("login", "用户名或密码错误");
return "loginUI";
}else{
//登陆成功
ActionContext.getContext().getSession().put("user", user);
}
return "toIndex";
}
/** 注销 */
public String logout() throws Exception {
ActionContext.getContext().getSession().remove("user");
return "logout";
}
2.
在UserService添加
User findByLoginNameAndPassword(String loginName, String password);
在UserServiceImpl中实现
public User findByLoginNameAndPassword(String loginName, String password) {
return (User) getSession().createQuery(//
"from User u where u.loginName=? and u.password=?")//
.setParameter(0, loginName)//
.setParameter(1, DigestUtils.md5Hex(password))//
.uniqueResult();
}
3.配置struts.xml
<!-- 用户管理 -->
<action name="userAction_*" class="userAction" method="{1}">
<result name="list">/WEB-INF/jsp/userAction/list.jsp</result>
<result name="addUI">/WEB-INF/jsp/userAction/addUI.jsp</result>
<result name="editUI">/WEB-INF/jsp/userAction/editUI.jsp</result>
<result name="loginUI">/WEB-INF/jsp/userAction/loginUI.jsp</result>
<result name="login" type="redirect">index.jsp</result>
<result name="logout">/WEB-INF/jsp/userAction/logout.jsp</result>
<result name="toList" type="redirectAction">userAction_list</result>
</action>
4.添加相应的页面,运行即可