struts自定义拦截器的三种方式

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>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值