• Struts2拦截器

  1. 拦截器(Interceptor)是Struts2的核心部分。
  2. Struts2很多功能都是构建在拦截器基础之上,比如:文件上传、国际化、数据类型转化、数据校验等。
  3. Struts2拦截器是在访问某个Action方法之前和之后实施拦截的。
  4. Struts2拦截器是可插拔的,拦截器是AOP(面向切面编程)的一种实现。
  5. 拦截器栈(Interceptor Stack):将拦截器按一定的顺序联合在一条链,在访问被拦截的方法时,Struts2拦截器栈中的拦截器就会按照之前定义的顺序被调用。
  • Interceptor接口

每个拦截器都必须实现com.opensymphony.xwork2.interceptor.Interceptor接口

package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import java.io.Serializable; public interface Interceptor extends Serializable { /**
* Called to let an interceptor clean up any resources it has allocated.
*/
void destroy(); /**
* Called after an interceptor is created, but before any requests are processed using
* {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving
* the Interceptor a chance to initialize any needed resources.
*/
void init(); /**
* Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
* request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.
*
* @param invocation the action invocation
* @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.
* @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.
*/
String intercept(ActionInvocation invocation) throws Exception; }
  1. Struts2会依次调用为某个Action而注册的每个拦截器的intercept方法
  2. 每次调用intercept方法时,Struts2会传递一个ActionInvocation接口的实例。
  3. ActionInvocation:代表一个给定Action的执行状态,拦截器可以从该类的对象里获的与该Action相关的Action对象和Result对象。在完成拦截器自己的任务之后,拦截器调用ActionInvocation对象的invoke方法前进到Action处理流程的下一个环节。
  4. AbstractInterceptor类实现了Interceptor接口,并为init,destory提供了空白的实现。
  • 用法示例:

定义一个PermissionInterceptor拦截器

package com.dx.struts2.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class PremissionInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L; @Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("Before execute ActionInvocation.invoke()");
String result = actionInvocation.invoke();
System.out.println("After execute ActionInvocation.invoke()"); return result;
}
}

在struts.xml中声明拦截器并在testToken action中引用该拦截器

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="i18n"></constant>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="premission"
class="com.dx.struts2.interceptor.PremissionInterceptor"></interceptor>
</interceptors> <action name="testToken" class="com.dx.struts2.actions.MemberAction">
<interceptor-ref name="premission"></interceptor-ref>
<interceptor-ref name="tokenSession"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>/success.jsp</result>
</action>
</package>
</struts>

 注意事项:

如果不调用String result = actionInvocation.invoke();或者返回的结果不是调用 actionInvocation.invoke()结果(比如:return "success";),那么,后续的拦截器及Action将不会被调用,而是直接去渲染result.

根据上边的特性,我们可以在实际项目中实现权限控制等操作。

Struts(二十八):自定义拦截器的更多相关文章

  1. 使用Typescript重构axios(二十八)——自定义序列化请求参数

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  2. java:struts框架3(自定义拦截器,token令牌,文件上传和下载(单/多))

    1.自定义拦截器: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  3. Struts(二十):自定义类型转换器

    如何自定义类型转换器: 1)为什么需要自定义类型转化器?strtuts2不能自动完成字符串到所有的类型: 2) 如何定义类型转化器? 步骤一:创建自定义类型转化器的类,并继承org.apache.st ...

  4. 第1节 flume:15、flume案例二,通过自定义拦截器实现数据的脱敏

    1.7.flume案例二 案例需求: 在数据采集之后,通过flume的拦截器,实现不需要的数据过滤掉,并将指定的第一个字段进行加密,加密之后再往hdfs上面保存 原始数据与处理之后的数据对比 图一  ...

  5. [原创]java WEB学习笔记74:Struts2 学习之路--自定义拦截器,struts内建的拦截器

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. 使用Typescript重构axios(十四)——实现拦截器

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  7. SpringMVC系列(十二)自定义拦截器

    Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口– preHandle():这个方法在业务处 ...

  8. struts自定义拦截器实现

    示例 添加新功能:只有是登录的状态访问hello_loginSuccess才会显示登录成功. index.jsp登录成功页面 test.jsp登录页面 一.修改原代码实现 1.登录后将登录信息添加到S ...

  9. web开发(二十一)之自定义拦截器的使用

    转自博客:http://blog.csdn.net/pkgk2013/article/details/51985817 拦截器的作用 拦截器,在AOP(Aspect-Oriented Programm ...

随机推荐

  1. java中一个引人深思的匿名内部类

    前两天去面试javaweb问到一个问题,在你的项目中有没有用到线程,我特么的一想,这东西不是在c层面的吗,所以说我不了解线程..... 后来回去想啊想啊,我操这特么的不是再问我事物的控制,消息队列的回 ...

  2. linux常用命令汇总(更新中...)

    文本查看与编辑 1.文本编辑命令 vi/vim 2.查看文件内容命令 命令 说明 命令格式 参数 cat 将一个文件的内容连续输出在屏幕上 cat [-option]  文件名 -n:将行号一起显示在 ...

  3. 【Python】 更多数据类型collections&简易数据文件shelve

    ■collections collections在python内建的数据类型基础上新增一些实用的数据类型,其目的在于增加代码的可读性?(虽然我自己没怎么用过..) ① deque 双端队列 q = d ...

  4. p-value

    p-value p-value翻译为假定值,假设几率.我们在生物信息中通常使用p值方法(P-Value, Probability, Pr)来做检验.那么p-value是什么呢?其实P-value就是一 ...

  5. Redis——主从同步原理

    刚接触到Redis,首先对Redis有一个初步的了解. 开源,免费,遵守BSD协议,key-value数据库. 可以将内存中的数据保存在磁盘中,重启的时候可以再次加载使用. 多种key-value类型 ...

  6. TensorFlow实现Softmax Regression识别手写数字中"TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败”问题

    出现问题: 在使用TensorFlow实现MNIST手写数字识别时,出现"TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应 ...

  7. Python upper()方法

    描述 Python upper() 方法将字符串中的小写字母转为大写字母. 语法 upper()方法语法: str.upper() 参数 NA. 返回值 返回小写字母转为大写字母的字符串. 实例 以下 ...

  8. 视频聊天插件:AnyChat使用攻略之iOS开发指南

    AnyChat使用攻略之iOS开发指南 这套攻略主要指导刚开始使用AnyChat SDK For iOS的同学,快速搭建SDK环境,和实现音视频开发流程. (需要工程案例文件可联系我们) 在iOS平台 ...

  9. 201421123042 《Java程序设计》第8周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码: 答:查找 ...

  10. SWFUpload文件上传详解

    SWFUpload是一个flash和js相结合而成的文件上传插件,其功能非常强大. SWFUpload的特点: 1.用flash进行上传,页面无刷新,且可自定义Flash按钮的样式; 2.可以在浏览器 ...