Struts(二十八):自定义拦截器
Struts2拦截器
- 拦截器(Interceptor)是Struts2的核心部分。
- Struts2很多功能都是构建在拦截器基础之上,比如:文件上传、国际化、数据类型转化、数据校验等。
- Struts2拦截器是在访问某个Action方法之前和之后实施拦截的。
- Struts2拦截器是可插拔的,拦截器是AOP(面向切面编程)的一种实现。
- 拦截器栈(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; }
- Struts2会依次调用为某个Action而注册的每个拦截器的intercept方法
- 每次调用intercept方法时,Struts2会传递一个ActionInvocation接口的实例。
- ActionInvocation:代表一个给定Action的执行状态,拦截器可以从该类的对象里获的与该Action相关的Action对象和Result对象。在完成拦截器自己的任务之后,拦截器调用ActionInvocation对象的invoke方法前进到Action处理流程的下一个环节。
- 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(二十八):自定义拦截器的更多相关文章
- 使用Typescript重构axios(二十八)——自定义序列化请求参数
0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...
- java:struts框架3(自定义拦截器,token令牌,文件上传和下载(单/多))
1.自定义拦截器: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
- Struts(二十):自定义类型转换器
如何自定义类型转换器: 1)为什么需要自定义类型转化器?strtuts2不能自动完成字符串到所有的类型: 2) 如何定义类型转化器? 步骤一:创建自定义类型转化器的类,并继承org.apache.st ...
- 第1节 flume:15、flume案例二,通过自定义拦截器实现数据的脱敏
1.7.flume案例二 案例需求: 在数据采集之后,通过flume的拦截器,实现不需要的数据过滤掉,并将指定的第一个字段进行加密,加密之后再往hdfs上面保存 原始数据与处理之后的数据对比 图一 ...
- [原创]java WEB学习笔记74:Struts2 学习之路--自定义拦截器,struts内建的拦截器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 使用Typescript重构axios(十四)——实现拦截器
0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...
- SpringMVC系列(十二)自定义拦截器
Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口– preHandle():这个方法在业务处 ...
- struts自定义拦截器实现
示例 添加新功能:只有是登录的状态访问hello_loginSuccess才会显示登录成功. index.jsp登录成功页面 test.jsp登录页面 一.修改原代码实现 1.登录后将登录信息添加到S ...
- web开发(二十一)之自定义拦截器的使用
转自博客:http://blog.csdn.net/pkgk2013/article/details/51985817 拦截器的作用 拦截器,在AOP(Aspect-Oriented Programm ...
随机推荐
- 新装的Linux服务系统安装MySQL
目的描述:全新的腾讯云Linux服务器,系统是ubuntu 16.04.需要在上面安装mysql数据库. 使用XShell远程登录,在终端窗口中使用sudo apt-get 指令在线安装mysql. ...
- 转载NodePort,LoadBalancer还是Ingress?我该如何选择 - kubernetes
原文:http://mp.weixin.qq.com/s/dHaiX3H421jBhnzgCCsktg ClusterIP ClusterIP服务是Kuberntets的默认服务.它在集群内部生成一个 ...
- Java 并发学习笔记
并发 最近重新复习了一边并发的知识,发现自己之前对于并发的了解只是皮毛.这里总结以下Java并发需要掌握的点. 使用并发的一个重要原因是提高执行效率.由于I/O等情况阻塞,单个任务并不能充分利用CPU ...
- 读headFirst设计模式 - 工厂模式
每次写博客都不知道要怎么引入要写的主题,挺头疼的一件事.今天就直接开门见山,今天要学的就是工厂模式,工厂就是批量生产制造东西的地方.在这里,工厂就是批量生产对象的地方. 学习书上的例子 假如你现在有一 ...
- Guava常用方法
简介 Guava,中文是石榴的意思,Guava项目,是基于java的Google的开源的工具类库,包含了许多被Google的java项目广泛依赖的核心库, 例如:集合.缓存.原生类型支持.并发库.通用 ...
- Tomcat8配置tomcat-users.xml配置
默认tomcat-user.xml中的角色和用户配置示例如下: <!-- <role rolename="tomcat"/> <role rolename ...
- 关于JSP页面Property [ssid] not found on type [java.lang.String]错误的注意事项
转发于:http://blog.csdn.net/w86440044/article/details/28277177
- 笔试常考--浏览器输入一个URL点击回车之后发生了什么
解析URL:浏览器首先对拿到的URL进行识别,抽取出域名字段. DNS解析: 查询浏览器缓存(浏览器会缓存之前拿到的DNS 2-30分钟时间),如果没有找到, 检查系统缓存,检查hosts文件,这个文 ...
- Beta冲刺 第五天
Beta冲刺 第五天 1. 昨天的困难 1.昨天的困难主要是在类的整理上,一些逻辑理不清,也有一些类写的太绝对了,扩展性就不那么好了,所以,昨天的困难就是在重构上. 页面结构太凌乱,之前没有统筹好具体 ...
- 敏捷开发每日报告--day5
1 团队介绍 团队组成: PM:齐爽爽(258) 小组成员:马帅(248),何健(267),蔡凯峰(285) Git链接:https://github.com/WHUSE2017/C-team 2 ...