Struts2–自定义拦截器三种方式(实现Interceptor接口、继承抽象类AbstractInterceptor、继承MethodFilterInterceptor)「建议收藏」

Struts2–自定义拦截器三种方式(实现Interceptor接口、继承抽象类AbstractInterceptor、继承MethodFilterInterceptor)「建议收藏」实现自定义拦截器在实际的项目开发中,虽然Struts2的内建拦截器可以完成大部分的拦截任务,但是,一些与系统逻辑相关的通用功能(如权限的控制和用户登录控制等),则需要通过自定义拦截器实现。本节将详细讲解如何自定义拦截器。1.实现Interceptor接口在Struts2框架中,通常开发人员所编写的自定义拦截器类都会直接或间接地实现com.opensymphony.xwork2.in…

大家好,又见面了,我是你们的朋友全栈君。

实现自定义拦截器

在实际的项目开发中,虽然 Struts2 的内建拦截器可以完成大部分的拦截任务,但是,一些与系统逻辑相关的通用功能(如权限的控制和用户登录控制等),则需要通过自定义拦截器实现。本节将详细讲解如何自定义拦截器。

1.实现Interceptor接口

在 Struts2 框架中,通常开发人员所编写的自定义拦截器类都会直接或间接地实现 com.opensymphony.xwork2.interceptor.Interceptor 接口。Interceptor 接口中的主要代码如下所示:

public interface Interceptor extends Serializable{ 
   
    void init();
    void destroy();
    String intercept(ActionInvocation invocation) throws Exception;
}

从上述代码中可以看出,该接口共提供了以下三个方法。

1)void init()
该方法在拦截器被创建后会立即被调用,它在拦截器的生命周期内只被调用一次。可以在该方法中对相关资源进行必要的初始化。

2)void destroy()
该方法与 init() 方法相对应,在拦截器实例被销毁之前,将调用该方法释放和拦截器相关的资源,它在拦截器的生命周期内,也只被调用一次。

3)String intercept(ActionInvocation invocation)throws Exception

该方法是拦截器的核心方法,用于添加真正执行拦截工作的代码,实现具体的拦截操作,它返回一个字符串作为逻辑视图,系统根据返回的字符串跳转到对应的视图资源。每拦截一个动作请求,该方法就会被调用一次。

该方法的 ActionInvocation 参数包含了被拦截的 Action 的引用,可以通过该参数的 invoke() 方法,将控制权转给下一个拦截器或者转给 Action 的 execute() 方法。

2.继承抽象类AbstractInterceptor

AbstractIntercepter 类实现了 Interceptor 接口,并且提供了 init() 方法和 destroy() 方法的空实现。使用时,可以直接继承该抽象类,而不用实现那些不必要的方法。AbstractInterceptor 类中定义的方法如下所示:

public abstract class AbstractInterceptor implements Interceptor{ 
   
    public void init(){ 
   }
    public void destroy(){ 
   }
    public abstract String intercept (ActionInvocation invocation) throws Exception;
}

AbstractInterceptor 类已经实现了 Interceptor 接口的所有方法,一般情况下,只需继承 AbstractInterceptor 类,实现 interceptor() 方法就可以创建自定义拦截器。

需要注意的是,只有当自定义的拦截器需要打开系统资源时,才需要覆盖 AbstractInterceptor 类的 init() 方法和 destroy() 方法。与实现 Interceptor 接口相比,继承 AbstractInterceptor 类的方法更为简单。

3.继承MethodFilterInterceptor

MethodFilterInterceptor提供了一个doIntercept方法供我们实现拦截器功能。


public abstract class MethodFilterInterceptor extends AbstractInterceptor { 
   
    /** * Subclasses must override to implement the interceptor logic. * * @param invocation the action invocation * @return the result of invocation * @throws Exception */
    protected abstract String doIntercept(ActionInvocation invocation) throws Exception;
    
}

示例:

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

//继承:MethodFilterInterceptor 方法过滤拦截器
//功能: 定制拦截器拦截的方法.
// 定制哪些方法需要拦截.
// 定制哪些方法不需要拦截
public class MyInterceptor3 extends MethodFilterInterceptor{ 
   

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception { 
   
		//前处理
		System.out.println("MyInterceptor3 的前处理!");
		//放行
		String result = invocation.invoke();
		//后处理
		System.out.println("MyInterceptor3 的后处理!");
		
		return result;
	}

}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146536.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月14日 下午11:00
下一篇 2022年5月14日 下午11:20


相关推荐

  • Windows系统Hadoop下载安装

    Windows系统Hadoop下载安装Windows 系统 Hadoop 下载安装 Hadoop 下载 Hadoop 安装 Hadoop 环境配置 Hadoop 服务启动 Hadoop 下载官网下载地址 https mirrors tuna tsinghua edu cn apache hadoop common 请求官网下载地址 点击 hadoop 2 7 7 链接点击 hadoop 2 7 7 tar gz 链接点击下载按钮进行下载 Hadoop 安装解压 hadoop 2 7 7 tar gz 解压 hadooponwind ma

    2026年3月18日
    2
  • python3.3使用tkinter实现猜数字游戏代码

    python3.3使用tkinter实现猜数字游戏代码发布时间:2014-06-18编辑:www.jbxue.com原文地址:http://www.jbxue.com/article/python/22152.htmlpython3.3使用tkint

    2022年7月6日
    24
  • 处理:/var/redis/run/redis_6379.pid exists, process is already running or crashed「建议收藏」

    处理:/var/redis/run/redis_6379.pid exists, process is already running or crashed「建议收藏」命令;serviceredisstart/var/redis/run/redis_6379.pidexists,processisalreadyrunningorcrashed引起这类问题一般都是强制关掉电源或断电造成的,也是没等linux正常关机科学的处理办法2种1:可用安装文件启动redis-server/etc/re

    2022年6月7日
    36
  • laravel框架中使用QueryList插件采集数据

    laravel框架中使用QueryList插件采集数据

    2021年10月31日
    45
  • keil5安装教程简单易上手

    keil5安装教程简单易上手keil5安装教程简单易上手首先对于开发ARM系列MCU来说常用MDK来进行开发废话不多说。文章最后含F0-F7系列pcak包链接1.下载keil5安装包(内含pack包)链接:https://pan.baidu.com/s/1wUH3K-0bxkLhKcDaL92X5A提取码:mnjb2.安装keil5(1).下载打开安装内容分别为如下如果没看见破解机请解压里面的key压缩包(2).打开安装包界面如下(3).先勾选协议在点击NEXT(4).建议不要安装系统盘自行

    2022年5月23日
    68
  • C++ gbk与utf8互转

    C++ gbk与utf8互转本文代码已在 vs2017 上验证 gbk 转 utf8 容易出现中文乱码 有的时候在 x8632 位编译环境下中文显示正常 但切换到 x6464 位编译环境下会乱码 本文所示的代码在 32 位和 64 位编译环境下均不会出现乱码 使用例子见 include iostream include stdlib h include string include string h include windows h usingnames windows h string h string stdlib h iostream

    2025年6月6日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号