首先,向大家推荐一个关于拦截器较好的blog:http://developer.51cto.com/art/200906/126894.htm
1.拦截器的使用规则:先定义;再使用;
拦截器的定义:
<interceptors>
<interceptor name="theInterceptor1" class="com.shengsiyuan.interceptor.TheInterceptor1">
<param name="test">shengsiyuan</param>
</interceptor>
<interceptor name="theInterceptor2" class="com.shengsiyuan.interceptor.TheInterceptor2">
</interceptor>
<interceptor name="theInterceptor3" class="com.shengsiyuan.interceptor.TheInterceptor3">
</interceptor>
<interceptor name="loginInterceptor" class="com.shengsiyuan.interceptor.LoginInterceptor">
</interceptor>
</interceptors>
拦截器的使用:
<action name="action1" class="com.shengsiyuan.struts2.Action1" method="myExecute">
<result name="success" type="chain">
<param name="actionName">action2</param>
<param name="username">${username}</param>
<param name="password">${password}</param>
<param name="usernameAndPassword">${usernameAndPassword}</param>
</result>
<interceptor-ref name="theInterceptor1"></interceptor-ref>
<interceptor-ref name="theInterceptor2"></interceptor-ref>
<interceptor-ref name="theInterceptor3">
<param name="includeMethods">execute, myExecute</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
2.
struts2中有一个系统默认的拦截器栈是 defaultStack,如果你手动引用自己的拦截器,系统默认的拦截器栈将不起作用;这样必需手动引入系统的拦截器栈<interceptor-ref name="defaultStack">
</interceptor-ref>
如果想改变系统默认的拦截器栈,可以这样配置:
<default-interceptor-ref name="myDefaultInterceptorStack"></default-interceptor-ref>
其中myDefaultInterceptorStack是自己定义的拦截器栈名字;
如果拦截器栈中有多个拦截器,在执行action之前的顺序跟配置拦截器的顺序一致,而在action之后执行的顺序是相反的;
3.拦截器和拦截器栈是一个级别的,也就是说一个拦截器栈中包括许多拦截器, 一个拦截器栈中还可以包括许多拦截器栈,配置如下方式:
<interceptor-stack name="myDefaultInterceptorStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>