struts2.0中基本拦截器的配置采用的是可插拔的方式
1.在struts.xml中的配置
通过在package下定义
<interceptors>
<interceptor name="simple" class=""></interceptor>
</interceptors>
在action中应用,写在<result>标签下
<interceptor-ref name="simple"/>
2.JAVA文件
继承一个AbstractInterceptor
重写他的
public String intercept(ActionInvocation invocation) throws Exception方法
例如:
@Override
public String intercept(ActionInvocation invocation) throws Exception {
LoginAction action = (LoginAction)invocation.getAction();
//我们可以通过invocation.getAction()来获得我们要拦截的action实例
System.out.println(name+"改名后的拦截器");
action.execute();
System.out.println("拦截器执行完毕");
return null;
}
我们也可以通过实现MethodFilterInterceptor拦截器来过滤我们想要过滤的某个ACTION中的某个方法
例如:
public class MethodInterceptor extends MethodFilterInterceptor {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override//该部分的写法和一般的拦截器写法是一样的
protected String doIntercept(ActionInvocation invocation) throws Exception {
LoginAction action = (LoginAction)invocation.getAction();
System.out.println(name+"改名后的拦截器");
action.execute();
System.out.println("拦截器执行完毕");
return null;
}
}
struts.xml中的配置
<action name="login" class="com.test.action.LoginAction">
<result name="success">/result.jsp</result>
<result name="input">/converter.jsp</result>
<interceptor-ref name="method">
<param name="name">雨的孩子</param>
<param name="excludeMethods">execute</param>
<param name="includeMethods">login</param>
<!--excludeMethods表示不包含的过滤方法-->
<!--includeMethods表示包含的过滤方法-->
</interceptor-ref>
</action>