首页 > 其他分享 >实现拦截器的三种方法

实现拦截器的三种方法

时间:2023-08-27 11:04:21浏览次数:32  
标签:拦截器 String excludeMethods includeMethods 三种 invocation 拦截 方法


说是三种方法,实际上是一种方法,其实只要这个类实现了Interceptor接口,即可成为一个拦截器类。

第一种方法就是直接实现Interceptor接口,这样的话,就要实现这个接口中的三个方法。

第二种方法是继承自AbstractInterceptor类,这是个抽象类,实现了Interceptor接口,并且对里面的init()和destroy()方法进行空实现,而把intercept()方法设置为抽象方法,让继承它的子类去实现,这样的话,子类只要实现这个intercept()方法就可以了,比直接实现接口要简单一些。

第三种方法是继承自MethodFilterInterceptor,这个类叫做方法过滤拦截器,这个类继承自AbstractInterceptor,并且提供了一种机制,即可以指定对Action中某些方法进行拦截或者是不拦截,所谓拦截不拦截,指的就是拦截器中的intercept()方法是否被执行了,若没有执行,就是没有拦截,若执行了,就是拦截了。

第一、二种方法这里就不详细说明了,主要说一下第三种方法,即MethodFilterInterceptor的使用。

首先来看一下这个类的部分代码:

public abstract class MethodFilterInterceptor extends AbstractInterceptor {
    protected transient Logger log = LoggerFactory.getLogger(getClass());
    
    protected Set<String> excludeMethods = Collections.emptySet();
    protected Set<String> includeMethods = Collections.emptySet();

    public void setExcludeMethods(String excludeMethods) {
        this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);
    }
    
    public Set<String> getExcludeMethodsSet() {
    	return excludeMethods;
    }

    public void setIncludeMethods(String includeMethods) {
        this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods);
    }
    
    public Set<String> getIncludeMethodsSet() {
    	return includeMethods;
    }

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

    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;
    }

    protected abstract String doIntercept(ActionInvocation invocation) throws Exception;
    
}

可以看到这里面有两个成员变量:includeMethods和excludeMethods,并且有对应的get/set方法,这两个变量的类型为String类型的集合,即可以放置多个String类型的数据,其实,这两个变量就是用来存放要拦截或者是不拦截的方法的名称的。includeMethods存放要进行拦截的方法名,excludeMethods存放不进行拦截的方法名。

在其中还实现了intercept方法,在里面经过某个判断后,调用了一个doIntercept()方法,这个方法是抽象的,就是要其子类去实现的。那么这个判断是什么呢?这个判断就是根据includeMethods和excludeMethods中存放的方法名,进行判断是否要对其进行拦截。

因为在类中已经实现了intercept()方法,而提供了一个抽象的doIntercept()方法,所以我们只要去实现这个抽象的方法,就可以实现针对到方法的拦截器了。

下面来个简单的例子:

拦截器类:

package com.suo.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.suo.listeners.BeforeResultListener;

public class MyInterceptor3 extends MethodFilterInterceptor {

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		
		System.out.println("before MyInterceptor3 invoke !");
		
		String result=invocation.invoke();
		
		System.out.println("after MyInterceptor3 invoke !");
		
		return result;
	}
}
配置拦截器:

<interceptor name="myInterceptor3" class="com.suo.interceptor.MyInterceptor3"></interceptor>
在action中引用拦截器:

<action name="action1" class="com.suo.actions.Action1" method="myExecute">
	 <result name="success" type="chain">
	 	<param name="actionName">action2</param>
	 </result>
	 		
	 <interceptor-ref name="myInterceptor3">	
	 	<param name="includeMethods">myExecute</param>
	 	<param name="excludeMethods">execute</param><!--对myExecute进行拦截,对execute不拦截-->
	 </interceptor-ref>
	 		
</action>

其实,这里有一个疑问:

因为一个action只能指定一个执行的方法,即使你写了很多自定义的方法,但是它只会执行指定的方法,这里提供可以对多个方法进行拦截的机制有什么用呢?没有被指定的方法,是不会执行的,拦截它与不拦截它又能起什么作用呢? 


标签:拦截器,String,excludeMethods,includeMethods,三种,invocation,拦截,方法
From: https://blog.51cto.com/u_5173797/7251445

相关文章

  • Struts2 中拦截器和Action的调用关系
    所谓的拦截器就是指实现了Interceptor接口的类,该接口中定义了三个方法:init(),destroy(),intercept()。init()用于在服务器启动的时候,初始化这个拦截器。destroy()用于清理分配给拦截器的资源,intercept()是用来起拦截作用的,这个方法还有一个ActionInvocation类型的参数invocation,并且......
  • Struts2输入校验以及错误信息处理(1)——用Action中定义的validate()方法进行校验
    Struts2的输入校验有两种方式:一种是用Action中定义的validate()方法进行校验,一种是用Struts2定义好的校验框架进行校验。前者里面的逻辑判断要自己写,而后者只需要传递相应的参数即可。不管是哪种方式,程序执行的流程都是一样的,执行流程如下:1、对表单传递过来的数据,先进行类型转换2、......
  • 上市公司绿色创新效率数据计算(text mining方法的使用)
    需求:工作中需要计算上市公司绿色创新效率数据,需要首先利用text_preprocessing对文本提取值进行预处理,然后通过Textmining方法进行转换后计算处理,最后利用效率法来进行综合计算和归类存储,用于后续的深度数据挖掘。解决:importnltkfromnltk.corpusimportstopwordsfromnltk.tok......
  • pycharm中,ideolog插件的使用方法
    我们在pycharm打印日志时,为了更方便的通过查看日志,需用通过不同改的颜色反应不同级别的日志内容我们可以使用ideolog插件来实现这种效果一、插件下载Settings-Plugins中,搜索ideolog,并安装 二、配置ideolog下载完成插件后,打开一个我们的日志文件,可以点击Configurelogform......
  • 新版Jadx 加载dex报错 jadx.plugins.input.dex.DexException:Bad checksum 解决方法
    <table><tr><tdbgcolor=orange>本文所有教程及源码、软件仅为技术研究。不涉及计算机信息系统功能的删除、修改、增加、干扰,更不会影响计算机信息系统的正常运行。不得将代码用于非法用途,如侵立删!</td></tr></table>新版Jadx加载dex报错jadx.plugins.input.dex.DexException:B......
  • 新版Jadx 加载dex报错 jadx.plugins.input.dex.DexException:Bad checksum 解决方法
    本文所有教程及源码、软件仅为技术研究。不涉及计算机信息系统功能的删除、修改、增加、干扰,更不会影响计算机信息系统的正常运行。不得将代码用于非法用途,如侵立删!新版Jadx(1.6+)加载dex报错jadx.plugins.input.dex.DexException:Badchecksum解决方法环境win10Jadx1.6......
  • sharp.js的常用方法
    sharp.js是一个用于处理图像的JavaScript库,它提供了许多方法来操作和修改图像。以下是一些常用的sharp.js方法及其详细参数说明: 1.`resize(width,height)`:调整图像的宽度和高度。  -width(Number):新的宽度。  -height(Number):新的高度。 2.`crop(left,top,widt......
  • VBS中解决路径带空格的三种方法
    方法一Setwshell=CreateObject("WScript.Shell")wshell.Run """C:\ProgramFiles\360\360se\360se.exe""",5,TrueSetwshell=Nothing 备注说明:命令格式:object.Run(strCommand,[intWindowStyle],[bWaitOnReturn])因为strComman......
  • 计算机网络自顶向下方法
    1、概论1.1、什么是Internet?1.1.1、从具体构成角度节点:主机及其上运行的应用程序;路由器、交换机等网络交换设备。边:接入网链路:主机连接到互联网的链路;主干链路:路由器间的链路。互联网是数以亿计的、互联的计算机设备:主机=端系统;运行网络应用程序。1.1.2、从......
  • RESTful 下请求方法的幂等性
    概念阐述一个HTTP方法是幂等的,指的是同样的请求被执行一次与连续执行多次的效果是一样的,服务器的状态也是一样的。换句话说就是,幂等方法不应该具有副作用(统计用途除外)。所有的safe方法也都是幂等的。幂等性只与后端服务器的实际状态有关,而每一次请求接收到的状态码不一定相同......