Struts2拦截器的方法过滤

struts2框架提供了MethodInterceptor类, 自定义的拦截器只要集成该类,就可以使用拦截器的方法过滤功能,来拦截Action中的特定方法。
(可以查看MethodInterceptor文档,在此声明,我没查。com.opensymphony.xwork2.interceptor.AbstractInterceptor拦截器的子类,实现类Interceptor和Serializable接口。)

两个重要的参数:
1,excludeMethods:该参数指定拦截器拒绝拦截的方法列表,如果有多个方法值,中间用逗号法恩凯,例如“method1,method2”。如果指定了该参数,那么该拦截器不会拦截指定的Action方法,类似于所谓的黑名单。
2,includeMethods:该参数指定拦截器需要拦截的方法列表,如果有多个方法值,中间使用逗号分开。如果指定了该参数,则指定的Action在执行前会被拦截器所拦截,即所谓的白名单。
注意:如果黑名单和白名单指定了同一个Action方法那么该方法还是会别拦截。

MethodInterceptor类的主要方法:
protected abstract String doIntercept(ActionInvocation invocation):继承该类的子类必须重写该方法,并实现拦截器逻辑。 (如果没继承MethodInterceptor类强制重写的是intercept()方法而不是doIntercept()方法);
String intercept(ActionInvocation invocation): 继承子com.opensymphony.xwork2.interceptor.AbstractInterceptor类,该方法不需要强制重写。
void setExcludeMethods(String includeMethods):设置拦截器的白名单,该方法参数为一个字符串,即对应的Action方法名称。使用该方法设置后,拦截器会拦截该方法。
set getExcludeMethodsSet():获得拦截器的黑名单。
Set getIncludeMethodsSet():获得拦截器的白名单。
如果使用MethodFilterInterceptor类的子类来是想拦截器的方法过滤,不需要同前面那样来重写intercept(ActionInvocation invocation)方法,这里只需重写doIntercept(ActionInvocation  invocation)方法即可。

说明:方法过滤拦截器和普通拦截器用法没什么区别,只是继承了 com.opensymphony.xwork2.interceptor.AbstractInterceptor类的子类MethoidFilterInterceptor,并重写 doIntercept(ActionInvocation invocation)方法;

实现方法过滤拦截的配置文件示例:
<action name="FilterAction" class="FilterActionClass">
<result name="success">/success.jsp</result>
<result name="input">/error.jsp</result>
<interceptor-ref name="defaultStack"></interceptor>
<interceptor-ref name="FilterInterceptor">
<!--使用方法过滤,设置白名单或黑名单-->
<param name="excludeMethods">method1</param>      <!--不拦截Action中的method1-->
<param name="includeMethods">method2</param>       <!--拦截Action中的method2-->
</action>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值