javaweb登陆,注销功能实现的一般步奏

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.添加相应的页面,运行即可

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值