After understanding how action works under Struts 2 framework, it's time to understand another important concept of the framework - Interceptor. Interceptor is used for separating different concerns(e.g. logging) and improve MVC designs(e.g. let action only dealing with model level functions).
First of all here is a diagram from Manning book. It mainly shows that ActionInvocation class encapsulate the execution of an action with its associated interceptors and results.
ActionInvocation class plays an essential role for interceptors. The diagram below demonstrate how the ActionInvocation controls the interceptors' execution.
From the diagram, we can observe:
- Framework calls invoke() method in ActionInvocation class.
- invoke() method starts from the first interceptor in stack.
- intercept() method from AuthenticationInterceptor is invoked
intercept() method determins whether invocation is invoked for next interceptor.
There is a state variable which records what interceptor it should execute next. So ActionInvocation is responsible for remembering the current state.
Question: why is this a recursive process? Answer is in the diagram above.
<pre><code> String foo = "bar"; </code></pre>