难经3:Struts2,拦截器拦不住Result?

难经3:Struts2,拦截器拦不住Result?

[问题]

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

 

[探幽]

 

在重看了Struts2的拦截器的官方文档以后,还是不明白上面的问题是为什么。地球人都知道,Struts2其实就是Webwork2,而拦截器的核心实现在XWork,利用XWork的拦截器框架,Struts2在外围通过线程上下文,绑定了Request和Response对象的包装类,哪问题到底在Struts2,还是在XWork?

 

在看到下面这张调用图,我才突然反应过来,“我真笨,真的,我只知道拦截器调用栈的最底层,是Action方法的调用,却不知道Result的调用也是在栈底调用,之后才返回给上一个拦截器,层层退出”:



        感谢这张图的作者,它简单,但有效。

 

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

 

另,为什么Result的执行不放到拦截器链的外面呢?这是我开始的直觉,有知道的朋友烦告知一声。

[解难]

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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值