博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2--自定义拦截器三种方式(实现Interceptor接口、继承抽象类AbstractInterceptor、继承MethodFilterInterceptor)
阅读量:2442 次
发布时间:2019-05-10

本文共 2456 字,大约阅读时间需要 8 分钟。

实现自定义拦截器

在实际的项目开发中,虽然 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; }}

转载地址:http://lasqb.baihongyu.com/

你可能感兴趣的文章
pixel2pixel_Pixel 2的视觉核心是什么?
查看>>
更改用户账户设置自动更改_您应该更改的5个SimpliSafe设置
查看>>
excel中转换为数值_如何在Microsoft Excel中转换货币
查看>>
netflix怎么上_如何在Netflix上隐藏电视节目和电影
查看>>
opera_从Opera快速拨号页上删除混乱
查看>>
apple pencil_如何在iPad Pro的Apple Pencil上双击动作
查看>>
如何在PowerPoint中将自定义模板设置为默认模板
查看>>
linux使用命令重命名_如何在Linux上使用重命名命令
查看>>
xcloud下载_Project xCloud是Microsoft在流Xbox游戏上的赌博
查看>>
gpu驱动程序_如何从错误的GPU驱动程序更新中恢复
查看>>
esp now_Google带回Google Now(内部)排序助手
查看>>
如何防止视频在Chrome中自动播放
查看>>
如何使用Synology NAS下载文件(并避免在夜间开启计算机)
查看>>
使用批处理脚本上传ftp_通过批处理脚本将文件上传到FTP站点
查看>>
linux下如何更改主机名_如何在不重新启动的情况下更改Linux主机名
查看>>
pxe网络启动引导程序_如何使用PXE设置网络可引导实用程序光盘
查看>>
凌乱的yyy_如何清理凌乱的Internet Explorer上下文菜单
查看>>
Laravel Eloquent:API资源
查看>>
在React中使用Font Awesome 5
查看>>
React Hooks入门
查看>>