拦截器方法过滤

一般情况下,为Action配置了拦截器,拦截器会拦截Action中的所有方法,但有时只需要拦截其中的某些方法。这是就需要struts的拦截器方法过滤特性。自定义拦截器需继承MethodFilterInterceptor抽象类。

package com.demo1.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class Methodfilterinterceptor extends MethodFilterInterceptor{

	public String doIntercept(ActionInvocation ai) throws Exception{
		
		System.out.println("拦截器方法过滤实现");
		return ai.invoke();
	}

}
因为MethodFilterInterceptor类提供了如下两个方法。

设置不需过滤的方法:

	public void setExcludeMethods(String excludeMethods) {
		this.excludeMethods = TextParseUtil
				.commaDelimitedStringToSet(excludeMethods);
	}
设置过滤的方法

	public void setIncludeMethods(String includeMethods) {
		this.includeMethods = TextParseUtil
				.commaDelimitedStringToSet(includeMethods);
	}

这样就可以在配置文件中设置参数

	 	<interceptors>
	 		<interceptor name="methodfilterInterceptor" class="com.demo1.interceptor.Methodfilterinterceptor"/>
	 	</interceptors>
		<action name="loginAction" class="com.demo1.action.LoginAction" method="checkLogin">
	 		<result name="success">/index.jsp</result>
	 		<result name="login">/login.jsp</result>
	 		<interceptor-ref name="defaultStack"/>
	 		<interceptor-ref name="methodfilterInterceptor">
	 			<param name="excludeMethods">checkLogin</param><!--此方法不需顾虑,多个方法以半角逗号隔开,-->
	 			<!--<param name="includeMethods">checkLogin<param> 方法需要过滤,此语句也可以不用配置-->
	 		</interceptor-ref>
	 	</action>

如果一个方法既配置在 excludeMethods又被配置在 includeMethods中,拦截器会以includeMethods优先即拦截此方法。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值