让Struts的过滤器“放过”Action的某些方法

53 篇文章 0 订阅
35 篇文章 0 订阅

Struts的过滤器本身提供了类似AOP,java.servlet.Filter的过滤拦截功能,所以对Action以及Action的方法的拦截可以用更加“Struts”的Interceptor来实现。

Interceptor可以直接拦截到Action的方法层面,只要在Interceptor.intercept()中判断方法是否需要被拦截即可实现,Struts本身提供了抽象类MethodFilterInterceptor用来解决这类问题。

public class MyMethodInterceptor extends MethodFilterInterceptor {

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {

        //do something

    }

}

通过继承MethodFilterInterceptor,重写doIntercept(),即可在实现拦截器功能的同时,具有MethodFilterInterceptor提供的指定方法拦截策略的配置能力,配置方式和Parameters拦截器的配置方法类似。

  • 直接在Interceptor-ref中配置
<interceptor-ref name="validation">
        <param name="excludeMethods">myValidationExcudeMethod</param>
</interceptor-ref>
  • 或者在Interceptor Stack中配置
<action name="myAction" class="myActionClass">
    <interceptor-ref name="myStack">
        <param name="myMethodInterceptor.excludeMethods">hello</param>
    </interceptor-ref>
</action>

MethodFilterInterceptor提供了两个可以设置的参数:

  • excludeMethods - 指示需要排除的方法名
  • includeMethods - 指示需要包括的方法名

includeMethods的优先级大于excludeMethods.

一些Struts自带的拦截器也是基于MethodFilterInterceptor实现的,比如常见的ValidationInterceptor。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值