struts2自定义拦截器的使用
来源:互联网 发布:md5加密Java应用 密钥 编辑:程序博客网 时间:2024/05/20 13:08
要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口
举个小例子,当用户登陆后信息保存Session中,且可浏览其它页面,若Session中没有用户信息就返回登录页面。
创建一个拦截器类
public class SessionInterceptor implements Interceptor {@Overridepublic void destroy() {// TODO Auto-generated method stub}@Overridepublic void init() {// TODO Auto-generated method stub}@Overridepublic String intercept(ActionInvocation arg0) throws Exception { //获取当前Action的名字 Object action = arg0.getAction(); // 一般对登录Action不做该项拦截,因为你登录时候肯定没有Session if (action instanceof LoginAction) { //不拦截就放行 return arg0.invoke(); }//取出session中信息Map<String, Object> session = arg0.getInvocationContext().getSession(); if(session.get("user")!=null){ System.out.println("intercept:合法!!"); //符合就放行 return arg0.invoke(); }else{ System.out.println("intercept:不合法!!"); //返回值要和struts.xml文件配置要匹配 return "noSession"; }}}
struts.xml中的配置思路是:
1、先创建一个包继承struts-default用来配置拦截器
<package name="sessionInterceptor" namespace="/" extends="struts-default"><interceptors><!--我们自定义的拦截器--><interceptor name="permissionSession"class="com.lmk.web.interceptor.SessionInterceptor" /><!--这里用拦截器栈,因为每个action只能用一个拦截器,所以这个栈放我们的自定义拦截器和struts2的默认拦截器--><interceptor-stack name="permissionStack"><!--struts2的默认拦截器,不使用就等于白使用struts2了,还不如用struts呢!--><interceptor-ref name="defaultStack" /><interceptor-ref name="permissionSession" /></interceptor-stack></interceptors><!-- 将上述拦截器设定为默认拦截器: --><default-interceptor-ref name="permissionStack" /> <!--配置拦截器的全局结果--><global-results><!-- 这个名字要和拦截器类的返回值一致--><result name="noSession">/WEB-INF/public/index.jsp</result></global-results></package>
2、再创建另一个包继承上面的拦截器包,哪个action用到拦截器就添加interceptor-ref标签
<package name="user" namespace="/" extends="sessionInterceptor"><!-- <global-results> <result name="noSession">/WEB-INF/public/index.jsp</result> </global-results> --><action name="user_*" class="profileAction" method="{1}"><result name="goToHome">/WEB-INF/self/home.jsp</result><result name="goToRegUi">/WEB-INF/public/register.jsp</result><result name="goToIndex">/WEB-INF/public/index.jsp</result><result name="success">/WEB-INF/test/showTest.jsp</result><!-- 这个action用到拦截器就配置--><interceptor-ref name="permissionStack" /></action><action name="login_*" class="loginAction" method="{1}"><result name="goToHomeUi">/WEB-INF/self/home.jsp</result><result name="loginErr">/WEB-INF/public/index.jsp</result><result name="goToIndex">/WEB-INF/public/index.jsp</result></action></package>
0 0
- struts2自定义拦截器的使用
- struts2--使用自定义拦截器
- Struts2的自定义拦截器
- Struts2的自定义拦截器
- struts2的自定义拦截器
- Struts2的自定义拦截器
- Struts2的自定义拦截器
- struts2的拦截器自定义,使用默认拦截器,组合拦截器堆 的案例
- struts2中拦截器的全局拦截和自定义拦截
- Struts2的拦截器、自定义拦截器、Struts2 验证机制
- Struts2 自定义拦截器配置和使用
- 十三、struts2使用自定义拦截器
- Struts2使用14 -- 拦截器4 -- 自定义拦截器
- 框架技术--struts2拦截器与自定义拦截器使用
- struts2 自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- scrollTop scrollLeft的封装
- NIPS2016总结回顾
- Spark集群搭建+基于zookeeper的高可用HA
- 【GDB调试学习笔记】利用core文件调试程序
- 栈的C++实现
- struts2自定义拦截器的使用
- android ScrollView smoothScrollTo源码的一点理解
- HTTP 协议详解
- zookeeper技术浅析
- EasyDarwin开源流媒体服务器支持basic基本认证和digest摘要自定义认证
- 高精度加减乘除
- Android extsd open failed: EACCES (Permission denied)
- 167.[LeetCode]Two SumII
- 面试题1