问题
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 (Update: This part was a major confusion on my end, and I was able to answer my question below once I got it)intercept() and can't find anything too obvious on the ActionInvocation I'm being passed.
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