在Interceptor.intercept()内部,我如何知道Action是否已经被执行?

发布于 2024-09-26 23:27:54 字数 1177 浏览 3 评论 0原文

我正在使用拦截器在基于 Struts 的应用程序中实现一些内容,但我对它们的生命周期如何工作感到困惑。根据Struts文档(“拦截器”"编写拦截器""Big picture"),它应该像这样工作:

FirstInterceptor
  NextInterceptor
    LastInterceptor
      Action
      Result
    LastInterceptor
  NextInterceptor
FirstInterceptor

这是有道理的,但我正在绊倒如何区分在操作之前执行的拦截器调用和在结果呈现之后执行的拦截器调用(我在这里跳过 PreResultListener )。 如果我启动调试器,我会收到两次对 intercept() 的调用,并且在我正在传递的 ActionIn Vocation 上找不到任何太明显的内容。< /strike> 更新:这部分对我来说是一个很大的困惑,一旦我得到它,我就能够回答下面的问题)

"Big picture" 页面谈论了一些令人困惑的“之前”和“之后”被称为“子句”,但我不知道该怎么做:

[...]

这包括在调用操作本身之前调用任何拦截器(before 子句)。

[...]

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

[...]

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

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

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:

[...]

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

[...]

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

[...]

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉服软 2024-10-03 23:27:54

没有对拦截器的两次调用:

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;
    }

}

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

There's no two calls to the interceptor:

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;
    }

}

(Note that the "two calls to intercept" I was witnessing in the debugger were a result of a less obvious redirect that I failed to notice. This confused me a lot.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文