自定义拦截器,class类要继承AbstractInterceptor,Struts2中的拦截器都是继承它,实现intercept方法:
package com.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("before");
String result = invocation.invoke();
System.out.println("after");
return result;
}
}
Struts.xml中的配置
<interceptors>
<interceptor name="MyInterceptor" class="com.interceptor.MyInterceptor"></interceptor>
</interceptors>
<interceptor-ref name="MyInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>