- 浏览: 1194038 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (434)
- js (20)
- struts2 (77)
- spring (11)
- hibernate (17)
- ssh整合 (9)
- 程序例子 (4)
- 正则表达式 (8)
- JSP (20)
- IDE (15)
- 数据库 (10)
- 工作经验技巧感悟 (21)
- 程序员的幽默 (4)
- windows设置 (4)
- 名词解释 (4)
- 技术入门 (4)
- jFreeChart (16)
- OGNL (5)
- java基础 (46)
- dwr (4)
- portal (1)
- 示例用到的附件 (0)
- 优秀程序员45个习惯 (1)
- webService (1)
- shell (3)
- ibatis (3)
- 开发工具 (1)
- OS (3)
- xmlbean (2)
- design_pattern (0)
- error (1)
- testng (2)
- python (18)
- hadoop (21)
- mapreduce (9)
- Hive (0)
- HBase (0)
- ubuntu (22)
- 多线程 (7)
- 自我改进 (6)
- 设计模式 (1)
- ssh (2)
- ant (4)
- guake (2)
- 云计算 (9)
- hdfs (3)
- 大数据 (1)
- 电信业务 (1)
- maven (2)
- svn (5)
- UML (1)
- 待处理问题 (2)
- log4j (1)
- css (1)
- LevelDb (0)
- thrift (6)
- 辅助工具 (3)
- 算法 (1)
- tornado (0)
- twisted (1)
- jvm (0)
- 图书 (1)
- 其他 (1)
- oracle (2)
- mvn (1)
- 私人信息 (0)
- nio (1)
- zookeeper (1)
- JavaIO (3)
- mongodb (1)
- java-高级 (6)
- spring-深入 (1)
- tomcat (1)
- quartz (1)
- 面试题 (1)
- redis (3)
- EJBCA (0)
- spring-3.0 (1)
- memcache (3)
- 性能检测 (1)
- android (0)
- 开源项目 (1)
- 将博客搬至CSDN (0)
- 架构之路 (1)
最新评论
-
pjwqq:
“子类对象”都拥有了“父类对象的一个实例的引用”我晕,应该是“ ...
java继承--父类属性的存放位置 -
gwgyk:
关于楼主最后的问题,我想可能是这样:InputFormat默认 ...
eclipse 运行hadoop wordcount -
fanjf:
上述7点基本具备,可惜现在因为项目需要,做管理方面工作!
如何判断自己是否具有成为一名优秀程序员的潜质 -
摸爬滚打NO1:
链接已经失效
Eclipse添加JSEclipse 插件(js插件) -
younglibin:
看了以上7条, 自己 也不知道是否能够对上, 感觉没一点都有那 ...
如何判断自己是否具有成为一名优秀程序员的潜质
Interceptor的接口定义没有什么特别的地方,除了init和destory方法以外,intercept方法是实现整个拦截器机制的核心方法。而它所依赖的参数ActionInvocation则是我们之前章节中曾经提到过的著名的Action调度者。
public abstract class AroundInterceptor extends AbstractInterceptor { /* (non-Javadoc) * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation) */ @Override public String intercept(ActionInvocation invocation) throws Exception { String result = null; before(invocation); // 调用下一个拦截器,如果拦截器不存在,则执行Action result = invocation.invoke(); after(invocation, result); return result; } public abstract void before(ActionInvocation invocation) throws Exception; public abstract void after(ActionInvocation invocation, String resultCode) throws Exception; }
在这个实现类中,实际上已经实现了最简单的拦截器的雏形。或许大家对这样的代码还比较陌生,这没有关系。我在这里需要指出的是一个很重要的方法invocation.invoke()。这是ActionInvocation中的方法,而ActionInvocation是Action调度者,所以这个方法具备以下2层含义:
1. 如果拦截器堆栈中还有其他的Interceptor,那么invocation.invoke()将调用堆栈中下一个Interceptor的执行。
2. 如果拦截器堆栈中只有Action了,那么invocation.invoke()将调用Action执行。
所以,我们可以发现,invocation.invoke()这个方法其实是整个拦截器框架的实现核心。基于这样的实现机制,我们还可以得到下面2个非常重要的推论:
1. 如果在拦截器中,我们不使用invocation.invoke()来完成堆栈中下一个元素的调用,而是直接返回一个字符串作为执行结果,那么整个执行将被中止。
2. 我们可以以invocation.invoke()为界,将拦截器中的代码分成2个部分,在invocation.invoke()之前的代码,将会在Action之前被依次执行,而在invocation.invoke()之后的代码,将会在Action之后被逆序执行。
由此,我们就可以通过invocation.invoke()作为Action代码真正的拦截点,从而实现AOP。
Interceptor拦截类型
从上面的分析,我们知道,整个拦截器的核心部分是invocation.invoke()这个函数的调用位置。事实上,我们也正式根据这句代码的调用位置,来进行拦截类型的区分的。在Struts2中,Interceptor的拦截类型,分成以下三类:
1. before
before拦截,是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之前。这些代码,将依照拦截器定义的顺序,顺序执行。
2. after
after拦截,是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之后。这些代码,将一招拦截器定义的顺序,逆序执行。
3. PreResultListener
有的时候,before拦截和after拦截对我们来说是不够的,因为我们需要在Action执行完之后,但是还没有回到视图层之前,做一些事情。Struts2同样支持这样的拦截,这种拦截方式,是通过在拦截器中注册一个PreResultListener的接口来实现的。
Struts2能够支持如此多的拦截类型,与其本身的数据结构和整体设计有很大的关系。
我们可以看到,Struts2对于整个执行的划分,从Interceptor到Action一直到Result,每一层都职责明确。不仅如此,Struts2还为每一个层次之前都设立了恰如其分的插入点。使得整个Action层的扩展性得到了史无前例的提升
整个执行顺序更加能够被理解。我们可以看到,递归调用保证了各种各样的拦截类型的执行能够井井有条。
请注意在这里,每个拦截器中的代码的执行顺序,在Action之前,拦截器的执行顺序与堆栈中定义的一致;而在Action和Result之后,拦截器的执行顺序与堆栈中定义的顺序相反。
/** * @throws ConfigurationException If no result can be found with the returned code */ public String invoke() throws Exception { String profileKey = "invoke: "; try { UtilTimerStack.push(profileKey); if (executed) { throw new IllegalStateException("Action has already executed"); } // 依次调用拦截器堆栈中的拦截器代码执行 if (interceptors.hasNext()) { final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next(); UtilTimerStack.profile("interceptor: "+interceptor.getName(), new UtilTimerStack.ProfilingBlock<String>() { public String doProfiling() throws Exception { // 将ActionInvocation作为参数,调用interceptor中的intercept方法执行 resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this); return null; } }); } else { resultCode = invokeActionOnly(); } // this is needed because the result will be executed, then control will return to the Interceptor, which will // return above and flow through again if (!executed) { // 执行PreResultListener if (preResultListeners != null) { for (Iterator iterator = preResultListeners.iterator(); iterator.hasNext();) { PreResultListener listener = (PreResultListener) iterator.next(); String _profileKey="preResultListener: "; try { UtilTimerStack.push(_profileKey); listener.beforeResult(this, resultCode); } finally { UtilTimerStack.pop(_profileKey); } } } // now execute the result, if we're supposed to // action与interceptor执行完毕,执行Result if (proxy.getExecuteResult()) { executeResult(); } executed = true; } return resultCode; } finally { UtilTimerStack.pop(profileKey); } }
从源码中,我们可以看到,我们之前提到的Struts2的Action层的4个不同的层次,在这个方法中都有体现,他们分别是:拦截器(Interceptor)、Action、PreResultListener和Result。在这个方法中,保证了这些层次的有序调用和执行。由此我们也可以看出Struts2在Action层次设计上的众多考虑,每个层次都具备了高度的扩展性和插入点,使得程序员可以在任何喜欢的层次加入自己的实现机制改变Action的行为。
发表评论
-
struts2 学习资料
2013-10-08 12:23 922struts2.0 系列文章 http://www.blo ... -
javaweb项目RuntimeException不能默认打印到控制台
2012-11-26 15:46 3016前几天搭建了一个框架: 配置 struts2 spring2 ... -
ajax和js实现下拉列表级联的两种方式(ajax和dwr)
2010-01-19 21:54 5803今天遇到一个问题: ... -
jsp页面用s:datetimepicker不显示时间下拉框(提示dojo未定义错误)
2010-01-15 09:49 5760在写时一定要加上:form 标签外加上<s:head t ... -
struts2中struts.xml 文件详解(中文版翻译)入门篇
2009-12-21 11:15 2081<!DOCTYPE struts PUBLIC &quo ... -
Struts2中的拦截器的内部结构
2009-12-10 14:44 1479让我们再来回顾一下之前我们曾经用过的一张Action Life ... -
struts2 中Action的生命周期
2009-12-10 14:40 1795接下来,我们再来看看Struts2中的Action的生命周期: ... -
Struts2的Action并不带有任何Web容器相关的对象,Action又是如何工作在Web容器中
2009-12-10 14:39 2107虽然Struts2的Action只是一 ... -
Struts2中的Action 定义
2009-12-10 14:38 1319多数的MVC框架中的Control层,都是一个Java对象。按 ... -
struts2中Action的五大元素
2009-12-10 14:23 2592在大概了解了Struts2的Action后,我们来重点研究一下 ... -
jsp 向Action传值(不提交committ 也能传值)
2009-12-05 09:45 3792今天发现 当药提交表单是 页面的 字段 在Action接收不 ... -
struts2 Tooltip和radio 还有关系吗?
2009-10-12 17:19 1746今天遇到一个奇怪的问题,拿来大家看看! 我用stru ... -
jsp向action传值的几个错误一点
2009-09-23 08:36 15861. 在jsp页面首次请求时正确!通过查询可以正确显示数 ... -
struts2 标签展示 map
2009-09-21 17:59 2232从数据库中查找出来的 ... -
struts2 标签展示 map
2009-09-21 17:48 2669在jsp页面用iterator 迭代时,如果 list中放的是 ... -
struts.properties 配置解析
2009-09-11 10:45 2019### struts.properties这是一 ... -
Struts1和Struts2的区别和对比
2009-09-11 10:05 1557? Struts1要求Action类继承 ... -
el表达式函数
2009-09-11 09:48 1243<%@ taglib prefix="fn&q ... -
struts2 手动校验原理图解
2009-09-02 10:37 16931、Struts 2的类型转换器负责对客户端的字 ... -
struts2 校验器——正则表达式校验器(regex)(内建校验器)
2009-09-02 10:19 5079正则表达式校验器的名称:regex,他检查被校验字段是否匹配一 ...
相关推荐
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
struts2 Interceptor拦截器 http://whitewolf.0fees.net
STRUTS2:拦截器Interceptor
应用拦截器 优化增删改查
ssh2-interceptor拦截器(权限管理).
简单的拦截器例子,适合初学者学习,也是一个备忘录
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
其中,init和destroy方法会在程序开始和结束时各执行一遍,不管使用了该拦截器与否,只要在struts.xml中声明了该Struts2拦截器就会被执行。 intercept方法就是拦截的主体了,每次拦截器生效时都会执行其中的逻辑。
Struts2_interceptor_和_filter区别
struts2 拦截器_教程.zip
Struts2.3.6实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34086699
在struts2中使用拦截器(Interceptor)控制登录和权限[汇编].pdf
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.
详细介绍struts2 interceptor
创建自己struts2拦截器,手工创建自己的拦截器实现
Struts2在com.opensymphony.xwork2.interceptor.annotations包中定义了3个拦截器注解类型。
Struts2_自定义拦截器 struts2_3500_my_interceptor
本篇文章主要介绍了详解Retrofit Interceptor(拦截器) 拦截请求并做相关处理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
综合使用servlet filter与struts2 interceptor对权限进行验证。
NULL 博文链接:https://weizhilizhiwei.iteye.com/blog/1005210