Interceptor的接口定义没有什么特别的地方,除了init和destory方法以外,intercept方法是实现整个拦截器机制的核心方法。而它所依赖的参数ActionInvocation则是我们之前章节中曾经提到过的著名的Action调度者

我在这里需要指出的是一个很重要的方法invocation.invoke()。这是ActionInvocation中的方法,而 ActionInvocation是Action调度者,所以这个方法具备以下2层含义(详细看DefaultActionInvocation源代 码):
1. 如果拦截器堆栈中还有其他的Interceptor,那么invocation.invoke()将调用堆栈中下一个Interceptor的执行。

2. 如果拦截器堆栈中只有Action了,那么invocation.invoke()将调用Action执行。

3.

DefaultActionInvocation部分源代码:

if (interceptors.hasNext()) {
       final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
       UtilTimerStack.profile("interceptor: "+interceptor.getName(),
         new UtilTimerStack.ProfilingBlock<String>() {
       public String doProfiling() throws Exception {
           resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);//递归调用拦截器
           return null;
       }
       });
      } else {
       resultCode = invokeActionOnly();
      }

每个拦截器中的代码的执行顺序,在Action之前,拦截器的执行顺序与堆栈中定义的一致;而在Action和Result之后,拦截器的执行顺序与堆栈中定义的顺序相反。

Interceptor拦截类型

从上面的分析,我们知道,整个拦截器的核心部分是invocation.invoke()这个函数的调用位置。事实上,我们也正式根据这句代码的调用位置,来进行拦截类型的区分的。在Struts2中,Interceptor的拦截类型,分成以下三类:

1. before

before拦截,是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之前。这些代码,将依照拦截器定义的顺序,顺序执行

2. after

after拦截,是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之后。这些代码,将一招拦截器定义的顺序,逆序执行

PreResultListener
有的时候,before拦截和after拦截对我们来说是不够的,因为我们需要在Action执行完之后,但是还没有回到视图层之前,做一些事情。
Struts2同样支持这样的拦截,这种拦截方式,是通过在拦截器中注册一个PreResultListener的接口来实现的。

如:在拦截器中使用如下代码,其中MyPreResultListener实现了PreResultListener 接口并在beforeResult方法中做了一些事情然后在拦截器类中加入action.addPreResultListener(new MyPreResultListener());

从源码中,我们可以看到,我们之前提到的Struts2的Action层的4个不同的层次,在这个方法中都有体现,他们分别是:拦截器
(Interceptor)、Action、PreResultListener和Result。在这个方法中,保证了这些层次的有序调用和执行

问题

使用Struts2作为
web框架,知道它的拦截器(Interceptor)机制,类似与Filter和Spring的AOP,于是实现了一个为Action增加自定义前置
(before)动作和后置动作(after)的拦截器(曰:WInterceptor),不过用一段时间发现,在WInterceptor的after
中,对Action对象的属性修改在页面看不到,对请求对象的属性设置也无效。为什么在调用了Action之后(invokeAction())之
后,request就不能使用了呢,拦截器不能改变Action的Result么?

问题的关键在于,在调用
actionInvocation.invoke()的之后,不仅执行类Action,也执行类Result。因而,等退回到拦截器的调用代码
时,Result已经生成,View已经确定,这时你再修改模型(Action的属性)或请求对象的属性,对视图不会有任何影响。

解决办法:

方法一:使用现成的PreResultListener监听器事件

搞清楚原因,卷起袖子干吧,只要让WInterpretor的after事件,放在Result的生成之前就行了。

看看XWork的拦截器接口注入的actionInvocation,其实就提供增加Result执行的前置监听事件-PreResultListener:

  1. * Register a {@link PreResultListener} to be notified after the Action is executed and
  2. * before the Result is executed.
  3. * <p/>
  4. * The ActionInvocation implementation must guarantee that listeners will be called in
  5. * the order in which they are registered.
  6. * <p/>
  7. * Listener registration and execution does not need to be thread-safe.
  8. *
  9. * @param listener the listener to add.
  10. */
  11. void addPreResultListener(PreResultListener listener);

/** * Register a {@link PreResultListener} to be notified after the
Action is executed and * before the Result is executed. * <p/> *
The ActionInvocation implementation must guarantee that listeners will
be called in * the order in which they are registered.
* <p/> * Listener registration and execution does not need to be
thread-safe. * * @param listener the listener to add. */ void
addPreResultListener(PreResultListener listener);

因此,让拦截器实现这个接口,就可以自然实现Action执行after事件了。

方法二,实现自己的 ActionInvocation ,手动分离Action和Result的执行

本来前面的方法已经很好了,可是,可是啊,在addPreResultListener里的异常,不会被Struts的框架捕获,而且,addPreResultListener接口不能传递自己的上下文参数,难道动用ThreadLocal传参?

研究了一下XWork的ActionInvocation 接口默认实现类DefaultActionInvocation,
写了一个包装类,将Action的执行和Result的生成完全分开,或许有人用的着,放上来,见附件
(ActionInvocationWrapper),如有不妥之处请告知。

exeucteAction是执行Action,executeResult是执行Result

Struts2.0中ActionInvocation使用的更多相关文章

  1. struts2.0中struts.xml配置文件详解

    先来展示一个配置文件 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration ...

  2. 深入struts2.0(七)--ActionInvocation接口以及3DefaultActionInvocation类

    1.1.1       ActionInvocation类 ActionInvocation定义为一个接口.主要作用是表现action的运行状态.它拥有拦截器和action的实例.通过重复的运行inv ...

  3. struts2.0中ognl栈的解析

    ongl详解: ValueStack是Struts2的一个接口,字面意义为值栈,OgnlValueStack是 ValueStack的实现类,客 户端发起一个请求,struts2架构会创建一个acti ...

  4. 关于struts2.0 中 struts.xml设置了struts.devMode 的值为TRUE后仍然不起作用的分析

    首先确认jdk 和tomcat的环境变量是否配置正确. 下面是配置方式 jdk的环境变量配置步骤: 安装j2sdk以后,需要配置一下环境变量,在我的电脑->属性->高级->环境变量- ...

  5. Struts2.0 封装请求数据和拦截器介绍

    1. Struts2 框架中使用 Servlet 的 API 来操作数据 1.1 完全解耦合的方式 Struts2 框架中提供了一个 ActionContext 类,该类中提供了一些方法: stati ...

  6. Struts2.0笔记二

    Mvc与servlet 1.1   Servlet的优点 1.  是mvc的基础,其他的框架比如struts1,struts2,webwork都是从servlet基础上发展过来的.所以掌握servle ...

  7. struts2.0整合json

    框架:struts2.0+hibernate2+spring 今天写代码时,需要用到json,我就直接加了两个jar包:json-lib-2.1-jdk15.jar,struts2-json-plug ...

  8. 注意在<s:if test="#session.user.power==0">中不能用 <s:if test=$sessionScope.user.power==0">

    获取封装在session的对象 用#session.对象名,可以获取对象 用#session.对象名.属性,可以获取属性. 注意在<s:iftest="#session.user.po ...

  9. 请求在Struts2框架中的处理步骤

    上图来源于Struts2官方站点,是Struts 2 的整体结构. 一个请求在Struts2框架中的处理大概分为以下几个步骤 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2 ...

随机推荐

  1. xml中Node和Element的区别

    本文转载自:http://blog.csdn.net/wcydiyi/article/details/4432636点击打开链接 1.元素(Element)和结点(Node)的区别:         ...

  2. Python基本数据类型及使用

    # 基本数据类型分类 - int 整数 - float 小数 - bool 布尔值 - str 字符串 ## int 整数 - 包括正整数和负整数 - 与java.c等语言相比并没有位数限制,理论上可 ...

  3. tcl之过程/函数-proc

  4. select into from 和 insert into select

    select into from 和 insert into select都是用来复制表, 两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建. inser ...

  5. Teaching Is a Fruitful Way to Learn【教学是一种有效的学习方式】

    Teaching Is a Fruitful Way to Learn For thousands of years, people have known that the best way to u ...

  6. openwrt(三) 固件的烧录

    导航: 方法1: tftp: 方法2: 在线升级 方法3: BIOS烧录 方法1:TFTP 这应该是最万能的一种方法了.TFTP是一种依靠网口传送数据的一种通信协议,没错,只是传输数据,并不是烧录,所 ...

  7. P1182 数列分段Section II

    P1182 数列分段Section II 题目描述 对于给定的一个长度为N的正整数数列A[i],现要将其分成M(M≤N)段,并要求每段连续,且每段和的最大值最小. 关于最大值最小: 例如一数列4 2 ...

  8. Spring mvc+hibernate+freemarker(实战)

    Spring mvc+hibernate+freemarker(实战) 博客分类: Spring Spring mvchibernatefreemarkerwebjava  今天我为大家做了一个 sp ...

  9. 剑指Offer - 九度1372 - 最大子向量和(连续子数组的最大和)

    剑指Offer - 九度1372 - 最大子向量和(连续子数组的最大和)2013-11-23 16:25 题目描述: HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天JOBDU测试组开完会后, ...

  10. nginx清除反向代理缓存

    nginx重启无法清除反向代理的缓存,可以清空安装目录下的proxy_cache文件夹里的内容来清除.