Struts2学习笔记(五)拦截器下 struts2拦截器配置及详解
2014-03-11 14:06
344 查看
6.2Struts2拦截器
6.2.1拦截器的配置Struts2的拦截器基于xwork包实现
init()方法只在启动的时候执行,且只执行一次。
package org.lei.interceptor;
importcom.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyInterceptor implementsInterceptor{
@Override
publicvoid destroy() {
//TODO Auto-generated method stub
System.out.println("destory");
}
@Override
publicvoid init() {
//TODO Auto-generated method stub
System.out.println("init");
}
@Override
publicString intercept(ActionInvocation invocation) throws Exception {
//TODO Auto-generated method stub
System.out.println("intercptor");
//判断如果还有下一个拦截器,则去执行下一个拦截器,如何拦截器没有了就回到execute方法
Stringresult = invocation.invoke();
returnresult;
}
}
在strut.xml定义拦截器
<interceptor
name="leiInterceptor"
class="org.lei.interceptor.MyInterceptor">
</interceptor>
6.2.2在指定action中使用,方法过滤拦截器
如果当action中有一个拦截器时,则必须手工的添加默认的拦截器,如果没有的添加拦截器话,则默认的拦截器会自动引入。类似于默认构造。(默认拦截器需要在package包中定义)
<interceptor-ref
name="leiInterceptor">
<!-- 在interceptor中会根据属性的名字(name),自动注入。在定义和使用的时候都可以给param赋值,一个相对于默认值一个
相当于动态值,如果有动态值,会覆盖定义时的赋值-->
<param
name="excludeMethods">test,execute</param>
<param
name="includeMethods">test</param>
</interceptor-ref>
通过excludeMethods指定不拦截拦截哪几个方法,includeMethods
指定拦截action中的哪几个方法,但是该拦截器必须继承MethodFilterInterceptor类。
其中includeMethods的优先级高于excludeMethods。
public class MyInterceptor3 extendsMethodFilterInterceptor
{
@Override
publicvoid init()
{
System.out.println("init3");
}
@Override
protectedString doIntercept(ActionInvocation invocation) throws Exception
{
System.out.println("myinterceptor3");
Stringresult = invocation.invoke();
returnresult;
}
}
6.2.3extends AbstractInterceptor
此时只需实现下面这一个方法就可以了
publicString intercept(ActionInvocation invocation) throws Exception
在strut.xml中配置的方法不都是相同的
6.2.5拦截器栈
拦截器栈里面可以包含拦截器,也可以包含拦截器栈。(底层是通过动态代理实现的)
<interceptor-stack
name="myStack">
<interceptor-refname="myInterceptor"></interceptor-ref>
<interceptor-refname="myInterceptor2"></interceptor-ref>
<interceptor-ref
name="defaultStack"></interceptor-ref>
</interceptor-stack>
执行的顺序与配置的顺序是相同的
先执行第一个拦截器,然后执行第二个拦截器,第二个execute,在回到第一个execute。
相关文章推荐
- Struts2拦截器Interceptor的原理与配置实例详解
- struts2的拦截器配置详解
- Struts2配置文件、拦截器详解
- Struts2拦截器的使用(详解) ----全局配置
- Struts2配置文件、拦截器等详解
- Struts2拦截器Interceptor的原理与配置实例详解(十一)
- struts2拦截器配置详解
- Struts2配置详解
- struts2中的constant配置详解
- Struts2 XML配置详解
- Struts2的配置以及详解
- struts2.x的常量配置,动态方法调用,通配符,拦截器,ValueStack和标签
- Struts2拦截器(Interceptor)原理详解
- struts2 properties配置详解
- Struts2 XML配置详解
- 4.5.5: Struts2的拦截器机制---配置默认拦截器
- struts2 所有常量配置详解
- struts2 18拦截器详解(十)
- struts2 配置的XML 详解
- Struts2中struts.xml的Action配置详解