Struts2 之拦截器interceptor(3)

很多时候如果我们需要拦截指定的方法,前面的特性是完成不了的,引入了一个新的东西


1.拦截方法的拦截器

这时候我们要继承MethodFilterInterceptor这个类,它是支持方法过滤的

这个方法我们只需要实现doIntercept(ActionInvocation arg0)这个方法


看下原理

public String intercept(ActionInvocation invocation) throws Exception {
        if (applyInterceptor(invocation)) {
            return doIntercept(invocation);
        } 
        return invocation.invoke();
    }

我们可以看到interceptor是回调这个方法实现的,真正的拦截逻辑也在这里


protected boolean applyInterceptor(ActionInvocation invocation) {
        String method = invocation.getProxy().getMethod();
        // ValidationInterceptor
        boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method);
        if (log.isDebugEnabled()) {
        	if (!applyMethod) {
        		log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list.");
        	}
        }
        return applyMethod;
    }

事实上excludeMethods和includeMethods都是Set集合,一个存储不拦截的方法--黑名单  一个则是白名单

黑白名单则是一般通过配置文件来指定的,不能手动指定,估计还是用的反射机制


public void setExcludeMethods(String excludeMethods) {
        this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);
    }
    

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


1.写Filter

package org.interceptor;

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

public class MyFilterInterceptor extends MethodFilterInterceptor {

	@Override
	protected String doIntercept(ActionInvocation arg0) throws Exception {
		// TODO Auto-generated method stub
		this.setExcludeMethods("arg");
		System.out.println("before");
		String result=arg0.invoke();
		if(arg0.isExecuted())
		{
			System.out.println("isexcuted");
		}
		System.out.println("after");
		return result;
	}

}

2.写Action

package org.actions;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {
	
	public String arg()
	{
		return SUCCESS;
	}
	
	

}

3.配置文件


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>  
    <constant name="struts.devMode" value="false" />
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="actions" extends="struts-default" namespace="/actions">
    <interceptors>
    <interceptor name="myfilter" class="org.interceptor.MyFilterInterceptor">
    
	</interceptor>
	</interceptors>
        <action name="user" class="org.actions.TestAction" method="arg">
	    	<result >/success.jsp</result>
	    	
	    	
	    	<interceptor-ref name="defaultStack"/>
	    	<interceptor-ref name="myfilter">
	    	  <param name="excludeMethods">arg</param>
	    	</interceptor-ref>    	
	    </action>       
    </package>
</struts>


 可以发现之前的效果输出没有了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值