Struts2之ModelDriven接口实现登陆及退出
来源:互联网 发布:人工智能的股票有哪些 编辑:程序博客网 时间:2024/06/06 14:28
当用POST方式提交表单,提交的数据量大时,action中定义各种数据,并实现get/set请求就显得过于臃肿,改用Struts2的ModelDriven接口来获取用户提交的HTTP请求,只需要定义相应的Model,Struts2框架会自动将用户提交的HTTP信息赋给相应的Model。
对项目中的LoginAction进行修改,采用ModelDriven方式。
LoginModel
首先,系统采用电话+密码的登陆方式,建立对应的LoginModel.java。
public class LoginModel { private String telephone; private String password; public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
LoginAction
表单提交的action继承ModelDriven接口,实现getter方法,重载execute方法。
@ParentPackage(value = "json-default")@Controller@Action(value = "/loginAction2", results = { @Result(name = "success", type = "redirect", location = "/index.jsp"), @Result(name = "input", type = "redirect", location = "/login.html"), @Result(name = "error", type = "redirect", location = "/login.html") }, exceptionMappings = { @ExceptionMapping(exception = "java.lang.Exception", result = "error") })public class LoginAction2 extends BaseAction implements ModelDriven<LoginModel> { private static final long serialVersionUID = 1L; @Resource private IUserService userManagerImpl; private LoginModel login = new LoginModel(); @Override public LoginModel getModel() { // TODO Auto-generated method stub return login; } // 重载 public String execute() throws Exception { String password = login.getPassword(); String telephone = login.getTelephone(); if (password != null && !"".equals(password)) { // password = MD5.MD5Encode(password); } if (telephone == null || telephone == "" || password == null || password == "") return INPUT; Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(telephone, password); token.setRememberMe(true);// 记住我 try { // 登录,即进行身份验证操作 subject.login(token);// 调用 myRealm的 doGetAuthenticationInfo方法 String telephone2 = (String) subject.getPrincipal();// 登陆成功就可以这样子拿到用户名了 User user = userManagerImpl.queryByTel(telephone2); subject.getSession().setAttribute("uname", user.getName()); } catch (UnknownSessionException uae) { subject.getSession().setAttribute("myerror", "登录异常!"); return ERROR; } catch (UnknownAccountException ex) { subject.getSession().setAttribute("myerror", "用户名不存在!"); return INPUT; } catch (IncorrectCredentialsException ice) { subject.getSession().setAttribute("myerror", "密码错误!"); return INPUT; } catch (LockedAccountException lae) { subject.getSession().setAttribute("myerror", "登录异常!"); return INPUT; } catch (AuthenticationException e) { subject.getSession().setAttribute("myerror", "用户登录失败!"); return INPUT; } return SUCCESS; } public String logout() throws Exception { Subject subject = SecurityUtils.getSubject(); if (subject.isAuthenticated()) { subject.logout(); // session 会销毁,在SessionListener监听session销毁,清理权限缓存 } return INPUT; }}
注意subject.getSession().setAttribute(arg0, arg1);
shiro的seeion,可以在login.jsp页面获取到错误类型。
<% Object error = session.getAttribute("myerror"); if(error==null) error=" "; %> <form action="login.action"> ... <label style="color:red"><%=error%></label> <div class="form-group"> <input type="text" name="telephone" class="email" placeholder="请输入电话" autocomplete="off" required="required" /> </div> <div class="form-group "> <input type="password" name="password" class="password" placeholder="请输入密码" autocomplete="off" required="required" /> </div> ...</form>
LogoutAction
注销账号退出系统,可以在LoginAction中添加一个logout方法,直接调用loginAction!logout.action
,或者重新写一个Action
@ParentPackage(value = "json-default")@Controllerpublic class LogoutAction extends BaseAction { private static final long serialVersionUID = 1L; @Action(value = "/logoutAction", results = { @Result(name = "success", type = "redirect", location = "/login.jsp"), @Result(name = "error", type = "redirect", location = "/index.jsp") }, exceptionMappings = { @ExceptionMapping(exception = "java.lang.Exception", result = "error") }) public String logout() throws Exception { Subject subject = SecurityUtils.getSubject(); if (subject.isAuthenticated()) { subject.logout(); // session 会销毁,在SessionListener监听session销毁,清理权限缓存 } return SUCCESS; }}
调用logoutAction.action
即可。
阅读全文
0 0
- Struts2之ModelDriven接口实现登陆及退出
- Struts2接口之ModelDriven分析
- struts2的ModelDriven接口
- Struts2-Modeldriven接口用法
- Struts2-Modeldriven接口用法
- Struts2-Modeldriven接口用法
- struts2之ModelDriven
- struts2之ModelDriven
- struts2之ModelDriven
- struts2之ModelDriven
- struts2之ModelDriven
- struts2之ModelDriven
- struts2之ModelDriven
- struts2之ModelDriven
- struts2之ModelDriven
- struts2之ModelDriven
- struts2之ModelDriven
- struts2之ModelDriven
- MySQL中的UUID外部取值时错误
- Drawable学习之---TransitionDrawable
- **win10+loadrunner12基础教程**
- javascript定时器做一个又快到慢的点名/抽奖效果
- webpack使用笔记(一)
- Struts2之ModelDriven接口实现登陆及退出
- 非单调队列
- html li标签前面添加图标三种方法
- AlertDialog样式
- 文件上传并且把文件名字保存到数据库(java+js代码)
- 纯JS实现页面滚动至顶部并自动显示或隐藏
- spring security3 配置使用注解
- Kotlin Reference (九) 抽象类、密封类
- HTML5 CSS3 经典案例:无插件拖拽上传图片 (支持预览与批量) (一)