拦截器在action执行前后都会执行, 拦截器修改action状态, 取消action执行. 可以定义拦截器栈, 站内拦截器依次执行.
public interface Interceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;
}
拦截器初始化时调用init()
执行初始化, 销毁时调用destroy()
释放资源, 处理请求时调用intercept()
.
所有action公用一个拦截器实例. 所以需要保证拦截器是线程安全的.
通常继承AbstractInterceptor
实现自定义拦截器
public class SimpleInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
MyAction action = (MyAction)invocation.getAction();
action.setDate(new Date());
return invocation.invoke();
}
}
<package name="my-package" extends="struts-default">
<interceptors>
<interceptor name="test" class="SimpleInterceptor" />
</interceptors>
</package>