Struts 2 Interceptors

Interceptors: Interceptors are the program similar to filter to do preprocessng before sending the request to action and modify the response if required after action execution.

We can implement various feature using existing interceptors: double-submit guards, type conversion, object population, validation, file upload, page preparation etc

Interceptor will get executed in the order in which they are specified.

Interceptor Name Description
Alias Interceptor alias parameters can have different names across request.
Chaining Interceptor chain Makes the previous Action's properties available to the current Action.
Checkbox Interceptor checkbox Adds automatic checkbox handling code that detect an unchecked checkbox and add it as a parameter with a default (usually 'false') value.
Cookie Interceptor cookie Inject cookie with a certain configurable name / value into action.
Conversion Error Interceptor conversionError Places error information from converting strings to parameter types into the action's field errors.
Create Session Interceptor createSession Create an HttpSession automatically, useful with certain Interceptors that require a HttpSession to work properly (like the TokenInterceptor)
DebuggingInterceptor debugging Provides several different debugging screens to provide insight into the data behind the page.
Execute and Wait Interceptor execAndWait Executes the Action in the background and then display the user with waiting page.
Exception Interceptor exception Maps exceptions to a result.
File Upload Interceptor fileUpload An Interceptor that to file upload file.
I18n Interceptor i18n Remember the locale selected for a user's session.
Logger Interceptor logger Provides logging by outputting the name of the action being executed.
Message Store Interceptor store Store and retrieve action messages / errors / field errors for action that implements ValidationAware interface.
Model Driven Interceptor modelDriven If Action implements ModelDriven then pushes the getModel Result onto the Value Stack.
Scoped Model Driven Interceptor scopedModelDriven If the Action implements ScopedModelDriven, the interceptor retrieves and stores the model from a scope and sets it on the action calling setModel.
Parameters Interceptor params Sets the request parameters onto the Action.
Scope Interceptor scope Provide mechanism for storing Action state in the session or application scope.
Servlet Config Interceptor servletConfig Provide access to Maps representing HttpServletRequest and HttpServletResponse.
Static Parameters Interceptor staticParams Sets the struts.xml defined parameters onto the action.
Timer Interceptor timer Outputs how long the Action takes to execute.
Token Interceptor token Checks for valid token presence in Action to prevents duplicate form submission.
Token Session Interceptor tokenSession Same as Token Interceptor, but stores the submitted data in session.
Validation Interceptor validation Performs validation using the validators defined in action-validation.xml
Workflow Interceptor workflow Calls the validate method in your Action class. If Action errors are created then it returns the INPUT view.


excludeMethods - method names to be excluded from interceptor processing

includeMethods - method names to be included in interceptor processing

With all these provided interceptors we have option to create our own interceptor also.

Interceptor configuration: We need to specify the interceptors in the struts.xml to get them apply for a request processing.

   <package name="ankit" extends="struts-default">
      <action name="examp" 
         <interceptor-ref name="params"/>
         <result name="success">/Output.jsp</result>

Here we have specified extends="struts-default" which means that what ever interceptors and result type specified there can be applied to any action in the configuration file.

For example: we have applied params intreceptor for ExampAction.

Possible interceptor entries in struts.xml file:

-> We can apply any existing interceptor to a action.

-> We can create our own interceptor and need to make an entry for that and then we can apply that interceptor to a action.

-> We can create stack of interceptor, so that we can simply apply that same stack to multiple actions instead of specifying the interceptors to all the actions.

-> Apply stack interceptor to a action

Code Sample for above points:

   <package name="helloworld" extends="struts-default">
   <!-- Entry for custom interceptors -->
         <interceptor name="custinterceptor"
            class="com.ankit.CustInterceptor" />

<!-- Creating stack of interceptors, so that we can
                   apply the stack to multiple actions -->	 
<interceptor-stack name="myStack">
   <interceptor-ref name="exception"/>
   <interceptor-ref name="params"/>
   <interceptor-ref name="custinterceptor" />
   <action name="examp" 
         <!-- Applying inbuilt interceptor -->
		 <interceptor-ref name="params"/>
		 <!-- Applying custom interceptor -->
         <interceptor-ref name="custinterceptor" />
         <result name="success">/Output.jsp</result>

    <action name="exampone" 
         <!-- Applying created stack of interceptor -->
         <interceptor-ref name="myStack" />
         <result name="success">/Output.jsp</result>
<!-- Apply your stack of interceptors to all actions -->
 <default-interceptor-ref name="myStack"/>