有些动作类不需要设置拦截器,那么就要排除使用这个拦截器,如果某一个拦截器需要被排除使用,那么这个拦截器创建的时候,就不能继承AbstractInterceptor而是继承:MethodFilterInterceptor
代码:
package com.rl.intereptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class Myinterceptor1 extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("做一些前置工作111");
//让拦截器向下走,并且返回结果代码:成功或者失败
String invoke = invocation.invoke();
System.out.println("做一些后置工作111");
return invoke;
}
}
同样,,需要在struts.xml中配置拦截器
<package name="my-interceptorSrack" extends="struts-default" abstract="true">
<interceptors>
<!-- 配置自己的拦截器 -->
<interceptor name="myInterceptor" class="com.rl.intereptor.Myinterceptor"></interceptor>
<!-- 这个是delete方法不使用的拦截器的配置 -->
<interceptor name="myInterceptor1" class="com.rl.intereptor.Myinterceptor1"></interceptor>
<!-- 配置拦截器栈 -->
<interceptor-stack name="rlStack">
<!-- 拦截器栈中要引用已经配置好的拦截器或者拦截器栈 -->
<interceptor-ref name="myInterceptor"></interceptor-ref>
<!-- 这个拦截器delete方法不使用 -->
<interceptor-ref name="myInterceptor1"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 把自己配置的rlStack拦截器栈设置成默认的拦截器,只要继承了my-interceptorSrack,就默认执行rlStack中的所有拦截器 -->
<default-interceptor-ref name="rlStack"></default-interceptor-ref>
</package>
如果在delete方法中不使用这个拦截器,首先,需要在action中引入默认的拦截器栈,
然后增加属性
<!--
指定要排除的拦截器
name:要排除的拦截器的名字.exludeMethods
元素:要排除的方法
-->
<action name="delete" class="com.rl.action.PersonAction" method="delete">
<!-- 首先引入默认的拦截器栈 -->
<interceptor-ref name="rlStack">
<!-- 排除的拦截器方法名.excludeMethods
括号中最好写上方法名,防止动态的方法
-->
<param name="myInterceptor1.excludeMethods">delete</param>
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/form.jsp</result>
</action>