All the interceptors in the stack will be called before the action.execute()
method is executed. In this case, the following will be the order.
foo.intercept()
bar.intercept()
baz.intercept()
action.execute()
Pergunta
Suppose there are multiple nested interceptors within a struts 2 action:
each of the interceptors is doing the following in their intercept()
method
String result = invocation.invoke();
return result;
When invocation.invoke()
is called, will this call the intercept()
method of the next interceptor in the queue, or will it actually call the action.execute()
method?
E.g if foo
does invocation.invoke()
, will this call bar.intercept()
or will it actually call action.execute()
?
If the latter, then what can I do to have bar.intercept()
or baz.intercept()
(if called from bar interceptor) be called before action.execute()
, i.e so all interceptors are run before action.execute()
is called?
Solução
All the interceptors in the stack will be called before the action.execute()
method is executed. In this case, the following will be the order.
foo.intercept()
bar.intercept()
baz.intercept()
action.execute()
Outras dicas
Action
executes only once. All the Interceptors
have a chance to fire twice: once for pre-processing (before calling invoke()
) and the other for post-processing (after calling invoke()
).
So, in your example here's what happens:
foo calls invoke() -> calls bar#intercept()
bar calls invoke() -> calls baz#intercept()
baz calls invoke() -> calls action#execute()
action#execute() returns -> baz executes lines after invoke()
baz#intercept() returns -> bar executes lines after invoke()
bar#intercept() returns -> foo executes lines after invoke()
Please, note that the Interceptors
never call each other directly. All the calls have to go through the Struts 2
framework. That's why invoke()
is called on the ActionInvocation
object which pretty much orchestrates this whole flow.