一.自定义拦截器
使用Interceptor接口或继承AbstractInterceptor类
Interceptor接口要实现init,intercept,destroy三个方法
AbstractInterceptor类只需要实现intercept方法,init和destroy方法已经在AbstractInterceptor中实现
1.使用Interceptor接口
package com.zucc.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class TestInterceptor1 implements Interceptor{
public TestInterceptor1(){
System.out.println("TestInterceptor1 constructor....");
}
@Override
public void destroy() {
System.out.println("TestInterceptor1 destroy....");
}
@Override
public void init() {
System.out.println("TestInterceptor1 init....");
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("TestInterceptor1 intercept....");
String resultName = invocation.invoke();
return resultName;
}
}
2. 继承AbstractInterceptor类
package com.zucc.interceptor;
import org.w3c.dom.views.AbstractView;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class TestInterceptor2 extends AbstractInterceptor{
public TestInterceptor2(){
System.out.println("TestInterceptor2 constructor....");
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("TestInterceptor2 intercept....");
String resultName = invocation.invoke();
return resultName;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="testInterceptor1" class="com.zucc.interceptor.TestInterceptor1"></interceptor>
<interceptor name="testInterceptor2" class="com.zucc.interceptor.TestInterceptor2"></interceptor>
</interceptors>
<action name="test" class="com.zucc.action.TestAction">
<result>/Hello.jsp</result>
<interceptor-ref name="testInterceptor1"></interceptor-ref>
<interceptor-ref name="testInterceptor2"></interceptor-ref>
</action>
</package>
</struts>
二. 自定义方法拦截器
继承MethodFilterInterceptor抽象类,实现MethodFilterInterceptor抽象类中的doIntercept方法可以对需要拦截的方法做一些处理,然后在struts.xml中进行配置。
MethodFilterInterceptor实现类
package com.zucc.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class TestMethodFilterInterceptor extends MethodFilterInterceptor{
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
System.out.println("TestMethodFilterInterceptor doIntercept...");
String resultCode = invocation.invoke();
return resultCode;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="testMethodFilterInterceptor" class="com.zucc.interceptor.TestMethodFilterInterceptor"></interceptor>
</interceptors>
<action name="test" class="com.zucc.action.TestAction">
<result>/Hello.jsp</result>
<result name="error">/error.jsp</result>
<interceptor-ref name="testMethodFilterInterceptor">
<!-- 配置哪些方法不需要拦截 -->
<param name="excludeMethods">list,execute</param>
<!-- 配置哪些方法需要拦截 -->
<param name="includeMethods">add,del</param>
</interceptor-ref>
</action>
</package>
</struts>
注意:excludeMethods和includeMethods一般只需要写一个就行,不用两个都写。在excludeMethods和includeMethods里也可以使用通配符,如add*表示所有以add开头的方法。在excludeMethods和includeMethods都写的情况下:如果某个方法同时在excludeMethods和includeMethods里,以includeMethods为准,即被拦截;如果某个方法在excludeMethods和includeMethods里都没有,以excludeMethods为准,即不被拦截。