1.自定义拦截器
@SuppressWarnings("serial") public class SessionInterceptor extends AbstractInterceptor { private static final Object LOGIN_KEY = "admin"; public static final String LOGIN_PAGE = "loginPage"; @Override public String intercept(ActionInvocation actionInvocation) throws Exception { Map session = actionInvocation.getInvocationContext().getSession(); Admin admin = (Admin) session.get(LOGIN_KEY); if (admin != null) { return actionInvocation.invoke(); } else { return LOGIN_PAGE; } } }
2.配置拦截器
有三种配置方法:
方法1. 普通配置法
<struts> <package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="SessionInterceptor"></interceptor> </interceptors> <action name="register" class="edu.hust.action.RegisterAction"> <result name="input">/register.jsp</result> <result>/result.jsp</result> <!-- 在自定义interceptor并将其ref时, 系统会覆盖掉默认的interceptor-stack(defaultStack), 为了保证系统默认的defaultStack不受印象, 我们需要显式的将其引入 --> <!-- 注意两个interceptor-ref的顺序, 顺序不同, 执行效果也不同: 先配置的先执行/后配置的先退出(先进后出) --> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myInterceptor"></interceptor-ref> </action> </package> </struts>
方法2. 配置拦截器栈
<struts> <package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="SessionInterceptor"></interceptor> <interceptor-stack name="myInterceptorStack"> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <action name="register" class="edu.hust.action.RegisterAction"> <result name="input">/register.jsp</result> <result>/result.jsp</result> <interceptor-ref name="myInterceptorStack"></interceptor-ref> </action> </package> </struts>
方法3. 修改默认拦截器,将自定义的拦截器栈定义为struts2的默认拦截器
<struts> <package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="SessionInterceptor"></interceptor> <interceptor-stack name="myInterceptorStack"> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 此默认interceptor是针对所有action的 --> <!-- 如果某个action中引入了interceptor, 则在这个action中此默认interceptor就会失效 --> <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref> <action name="register" class="edu.hust.action.RegisterAction"> <result name="input">/register.jsp</result> <result>/result.jsp</result> </action> </package> </struts>
相关推荐
简单的拦截器例子,适合初学者学习,也是一个备忘录
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
struts2 拦截器_教程.zip
STRUTS2:拦截器Interceptor
struts2 Interceptor拦截器 http://whitewolf.0fees.net
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
拦截器是一种AOP(面向切面编程)思想的编程方式.它提供一种机制是开发者能够把相对独立的代码抽离出来,...下面这篇文章主要给大家介绍了关于Struts2拦截器Interceptor的原理与配置的相关资料,需要的朋友可以参考下。
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.
Struts2规定用户自定义拦截器必须实现com.opensymphony.xwork2.interceptor.Interceptor接口。该接口声明了3个方法, void init(); void destroy(); String intercept(ActionInvocation invocation) throws ...
Struts2.3.6实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34086699
创建自己struts2拦截器,手工创建自己的拦截器实现
综合使用servlet filter与struts2 interceptor对权限进行验证。
NULL 博文链接:https://johnson2132.iteye.com/blog/720398
使用Struts2实现用户权限拦截、重点是掌握拦截器的使用和配置方法,通过案例的学习,就能掌握struts中拦截器的使用方法,让我们能快速上手
Struts2 拦截器详细配置过程 1:所有拦截器的超级接口Interceptor ,Action去实现这个接口; Interceptor 它其中有三个方法(init(),destroy() ,interceptor()): Init()方法:在服务器起动的时候加载一次,并且只加载一...
Interceptor拦截器详解和部分源码的指导,方便深入学习struts2,增加理解
拦截器可以说相当于是个过滤器:就是把你不想要的或不想显示的内容给过滤掉。拦截器可以抽象出一部分代码可以用来完善原来的Action。同时可以减轻代码冗余,提高重用率。比如在登入一个页面时,如果要求用户密码、权限...
Struts2在com.opensymphony.xwork2.interceptor.annotations包中定义了3个拦截器注解类型。
(2)拦截器组件可以继承AbstractIntercepter类(实现了Intercepter接口)。 (3)拦截器组件可以继承MethodFilterIntercepter类(继承自AbstractIntercepter),增加了方法过滤功能(上面两种方式是拦截所有方法,这个...