What are Interceptors in Struts 2 and how do they work?
Understanding Struts 2 Interceptors concept is very important in learning the Struts 2. You should have good knowledge of different types of Interceptors in Struts 2. Interceptors performs very important work during pre and post processing of any request sent to the Struts 2 system.
In order to deploy Struts 2, it is very important to understand Interceptors in Struts 2, which are amongst the most important aspects of Struts 2 due to its diversified role in increasing the efficiency of Struts.
What are Interceptors?
To get an in depth understanding of struts 2 interceptors, let us know first, what are interceptors: "Interceptors are basically objects that intercept the Action invocations dynamically and provide the developers with an opportunity specific codes that can be executed before and/or after the execution of an action".
So, the most important feature of interceptors is their ability to execute code before and after an Action is invoked. For this reason, most of the functions of frameworks based on Struts 2 are implemented as Interceptors, which includes functions like double-submit guards, type conversion, object population, validation, file upload, page preparation and much more. Moreover, one of the major features of interceptors in struts 2 that make it even more powerful is, they are "pluggable" and hence the developer or user can decide exactly which features an Action needs to support.
Though, various interceptors come configured by default with struts 2 but depending upon your need custom interceptors can also be created and integrated to the framework. In addition to that, you can also mix and match the interceptors already bundled with the framework.
Once the request is made by the client for an action, the framework calls the action object but before executing the action, interceptors intercepts the invocation and once the action executes the invocation is intercepted again by the interceptors.
However, in some cases, Interceptors may prohibit the execution of action as a result of double submits or validation error. Moreover, Interceptors can also change the state of an Action before executing it.
How does it works?
Interceptors carry out most of the work in Struts 2 framework. Interceptors can invoke logic before processing and after processing an action. At first the object of ActionInvocation is created. It encapsulates all the action and the interceptors, invoking interceptors before calling the action. After action is called, a result is rendered. If there are any interceptors, they are called again in reverse order before the result is generated and shown to the user.
Interceptors can modify the action and can even prevent their execution.
Action is executed using the interceptor by invocation.invoke() call. Whenever invoke() method is called, ActionInvocationexecutes interceptors (if any). After all interceptors have been invoked, invoke() will execute the action.
The above diagram shows how interceptors work:
Read latest Struts 18.104.22.168 tutorials here and master the skills of web development with Struts 2.
Posted on: September 21, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles