Struts2拦截器的实例
来源:互联网 发布:人知将死 其言也善后句 编辑:程序博客网 时间:2024/05/20 08:26
最近在学习struts2的拦截器,做了一个小例子:
首先写一个登录页面login.jsp
<body><s:form action="login" method="post"><s:textfield name="username" label = "用户名"/><s:password name="pass" label="密码" /><s:submit value="提交" /></s:form></body>
再写LoginAction:
private String username;private String pass;
和两变量的set、get方法
public String execute() throws Exception {// TODO Auto-generated method stubif(isNull(getUsername())) return INPUT;if(isNull(getPass())) return INPUT;ActionContext.getContext().getSession().put("user",getUsername());ActionContext.getContext().getSession().put("pass", getPass());return SUCCESS;}private boolean isNull(String str) {return str==null||str.length()==0;}
拦截器LoginInterceptor:
public String intercept(ActionInvocation ai) throws Exception {// TODO Auto-generated method stubMap session = ai.getInvocationContext().getSession();String username = (String)session.get("user");String pass = (String)session.get("pass");if(username!=null&&username.length()>0&&pass!=null){return ai.invoke();}else{return Action.LOGIN;}}
最后配置struts.xml:
<struts><package name="struts2" extends="struts-default"><!-- 配置自定义拦截器LoginInterceptor --><interceptors><interceptor name="loginedCheck"class="com.struts2.interceptor.LoginInterceptor" /><interceptor-stack name="mystack"><interceptor-ref name="loginedCheck" /><interceptor-ref name="defaultStack" /></interceptor-stack></interceptors><!-- 定义全局result --><global-results><!-- 定义名为exception的全局result --><result name="exception">/exception.jsp</result><result name="tologin" type="redirect">/login.jsp</result></global-results><!-- 定义全局异常映射 --><global-exception-mappings><!-- 捕捉到Exception异常(所有异常)时跳转到exception所命名的视图上 --><exception-mapping exception="java.lang.Exception"result="exception" /></global-exception-mappings><!-- 用户登录 --><action name="login" class="loginAction"><result name="input">/login.jsp</result><result name="error">/login.jsp</result><result name="success" type="redirect">/index.jsp</result></action><action name="logout" class="com.struts2.action.LoginAction"><result name="success" type="redirect">/login.jsp</result></action><!-- 系统用户信息管理 --><action name="listSystemUser" class="systemUserAction"method="listSystemUser"><result name="success">/list.jsp</result><interceptor-ref name="mystack" /></action></package></struts>
0 0
- Struts2拦截器的实例
- Struts2拦截器实例-权限拦截器
- struts2 拦截器实现实例
- struts2拦截器配置实例
- Struts2拦截器配置实例
- struts2-timer拦截器实例
- Struts2自定义拦截器实例—Session超时的处理
- Struts2自定义拦截器实例—Session超时的处理
- Struts2自定义拦截器实例—Session超时的处理
- Struts2自定义拦截器实例—Session超时的处理
- struts2中拦截器使用的代码实例
- Struts2的拦截器
- Struts2的拦截器
- Struts2的拦截器
- struts2的拦截器
- Struts2的拦截器
- struts2的拦截器
- Struts2的拦截器
- POJ 3278 Catch That Cow BFS(第一题)
- 参数化查询为什么能够防止SQL注入
- Dev gridcontrol获取选定行,指定列单元格的内容
- OpenCV学习(五)平滑滤波
- Cocos2dx 3.0rc 创建工程、交叉编译至android 的懒人脚本
- Struts2拦截器的实例
- 约瑟夫环:用java解决的一种办法
- 利用for循环实现程序的延时
- poj 2771 Guardian of Decency (二分图求最大独立集)
- 参数化查询为什么能够防止SQL注入
- 输入函数的细节
- Java 基础 - 常见运算符
- 二进制位的操作
- J - Min Number