在Interceptor.intercept()内部,我如何知道该动作是否已经执行? [英] Inside Interceptor.intercept(), how do I know if the Action has already been executed?

查看:116
本文介绍了在Interceptor.intercept()内部,我如何知道该动作是否已经执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用拦截器在基于Struts的应用程序中实现一些功能,并且对其生命周期的工作方式感到困惑.根据Struts的文档("Interceptors" 写拦截器" ),它应该像这样工作:

I'm implementing some stuff in my Struts-based application using interceptors, and I'm getting confused about how their lifecycle works. According to the Struts docs ("Interceptors", "Writing interceptors" and "Big picture"), it should work something like this:


FirstInterceptor
  NextInterceptor
    LastInterceptor
      Action
      Result
    LastInterceptor
  NextInterceptor
FirstInterceptor

很有道理,但我在如何区分动作之前执行的拦截器调用和呈现结果之后执行的拦截器调用之间感到困惑(我在这里跳过了PreResultListener). 如果我启动调试器,则会收到两个对intercept()的调用,并且在通过的ActionInvocation上找不到任何明显的内容. ( Update :这部分是我的主要困惑,一旦知道,我就可以在下面回答我的问题)

which makes sense, but I'm stumbling on how to distinguish an interceptor call executing before the action from one executing after the result is rendered (I'm skipping PreResultListeners here). If I fire up a debugger, I get two calls to intercept() and can't find anything too obvious on the ActionInvocation I'm being passed. (Update: This part was a major confusion on my end, and I was able to answer my question below once I got it)

大图景" 页面有点话混淆之前"和之后". 子句"被称为,但我不知道该怎么做:

The "Big picture" page talks somewhat confusingly of a "before" and "after" "clause" that are called, but I don't know what to make of it:

[...]

[...]

这包括在调用Action本身之前调用所有拦截器(before子句).

This includes invoking any Interceptors (the before clause) in advance of invoking the Action itself.

[...]

拦截器将再次执行(以相反的顺序,调用after子句).

Interceptors are executed again (in reverse order, calling the after clause).

[...]

(更新:这两个句子仍然很糟糕)

(Update: These two sentences are still bad, though)

推荐答案

拦截器没有两个调用:

public class MyInterceptor implements Interceptor {

    public String intercept(ActionInvocation invocation) {
        /*
        This is before Action.execute(),
        and before all interceptors down the stack
        */

        String code = invocation.invoke();

        /*
        This is after Action.execute(),
        the result rendering and all
        interceptors down the stack,
        but before the interceptors
        higher up in the stack.
        */

        return code;
    }

}

(请注意,我在调试器中看到的两次拦截调用"是我没有注意到的不太明显的重定向的结果.这让我很困惑.)

这篇关于在Interceptor.intercept()内部,我如何知道该动作是否已经执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆