Inside Interceptor.intercept(), how do I know if the Action has already been executed?

ぐ巨炮叔叔 提交于 2019-12-20 03:18:33

问题


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)


回答1:


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.)



来源:https://stackoverflow.com/questions/3907801/inside-interceptor-intercept-how-do-i-know-if-the-action-has-already-been-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!