struts自定义拦截器 有三种方式:
方式一、实现Interceptor 接口
public interface Interceptor extends Serializable{
public void init();
public void destroy();
public String intercept(ActionInvocation invocation)();
}
此方式需要实现三个方法
方式二、继承AbstractInterceptor类 重写interceptor方法
AbstractInterceptor类实现了Interceptor接口
方式三、继承MethodFilterInterceptor类,重写方法doIntercept()
MethodFilterInterceptor实现过滤其中用到的两个参数
execludeMethods 该参数指定拦截器拒绝拦截的方法列表,多个方法用,隔开,指定这个参数,拦截器不会拦截指定的列表中的方法
includeMethods 该参数指定拦截器需要拦截的方法,如果指定了参数,则指定的Action在执行前会被拦截
包内定义拦截器
//注册拦截器
<interceptor-stack name="自定义默认拦截器栈">
<interceptor-ref name="自定义拦截器名"><interceptor-ref>
//如果自定义拦截器 是继承的MethodFilterInterceptor可以配置参数
<interceptor-ref name="自定义拦截器名2">
<param name="excludeMethods">方法名</param>
<interceptor-ref>
//默认的拦截器
<interceptor-ref name="defaultStack"><interceptor-ref>
</interceptor-stack>
</interceptor>
//将自己配置的拦截器栈设置为默认的拦截器栈 在该包下创建Action 不需要配置 就默认走该拦截器
//如果其他包下的package也 用当前包的设置,只需要让 其他包继承该拦截器
<default-interceptor-ref name="自定义默认拦截器栈"></default-interceptor-ref>
action内定义
<action>
<result ...></result>
<interceptor-ref name="自定义拦截器名"><interceptor-ref>
//注意:如果配置了Action自己要走的拦截器,那么 默认拦截器栈就不会走了,需要我们自己配置
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>