Struts 2 Interceptors

Most of the processing in the framework is done by the interceptor. At the time of pre-processing and post-processing of the request, interceptors are invoked. Different functions such as exception handling, displaying intermediate results, logging and validation are done by interceptors. Interceptors are the same as the Servlet Filters.  Interceptor gives pre-processing logic prior to action and post-processing logic after the action. Interceptors are very important as they are called by controllers both before invoking the action and after invoking the action. It exists between the controller and the action. Many loopholes are also overcome by the introduction of interceptors in struts.  It is also used to catch an exception so that it can be handled. If we want to remove an entry of exception handling or validation, we have to just remove that entry from struts.xml. There is no need to redeploy an application. Struts allow us to create our own interceptors as well as there are many default interceptors in struts.

Different interceptors available in the STRUTS framework are:

  1. Custom Interceptor
  2. Params Interceptor
  3. execAndWait Interceptor
  4. prepare Interceptor
  5. modelDriven Interceptor
  6. exception Interceptor
  7. fileUpload Interceptor

Default interceptors in struts framework:

  1. alias: It is used to convert all the similar parameters with different names present in the request.
  2. autowiring: It is mainly used to autowire action classes into spring beans.
  3. chain: It is used to make properties of previous action available in the ongoing action.
  4. checkbox: It is used to handle checkboxes. It detects whether the checkbox is checked or not. It is used mainly to detect unchecked checkboxes.
  5. cookie: It is used to add a cookie. It adds a cookie with a configurable name or value.
  6. conversionError: With this interceptor, conversion errors are added to action's field error.
  7. createSession: If the HttpSession object does not exist, it is used to create HttpSession object.
  8. clearSession: It is used to unbind the HttpSession object when not needed.
  9. debugging: It provides different debugging screens to developers.
  10. externalRef: It is used to resolve external references.
  11. execAndWait: While performing any action in the background it sends an intermediatory waiting page to the user.
  12. exception: It is used to map an exception generated from action to the result and redirect to exception handling.
  13. fileUpload: It provides a facility to upload files in struts application.
  14. i18n: It provides the support of localization as well as internationalization.
  15. jsonValidation: It is used for asynchronous validation.
  16. logger: It is used to get name of the action as an output.
  17. store: For any action that implements ValidationAware interface, it is used to store and retrieve field errors and action messages.
  18. modelDriven: It is used to make another model object which acts as the object of ValueStack
  19. scopedModelDriven: It works for the action that implements ScopedModelDriven.
  20. params: It is used to add action parameters.
  21. actionMappingParams: It is used to set all the parameters from action mapping to Value Stack.
  22. prepare: If the action implements a Preparable interface it is used to do pre-processing work like establishing a database connection.
  23. profiling: It is used to add an advantage to action profiling.
  24. roles: It is used to perform actions that are based on roles.
  25. scope: The action state is stored and retrieved using this interceptor.
  26. servletConfig: It is used to perform actions with servlet-based information including HttpServletRequest and HttpServletResponse.
  27. sessionAutowiring: It is configured as a default interceptor.
  28. staticParams: Static properties are mapped with action properties using this interceptor.
  29. timer: It gives the information about how much time is required for the action to execute.
  30. token: In order to prevent duplicate form submission this interceptor provides checking of the action.
  31. tokenSession: Prevention of redundant submission of request is done, but when an invalid token is encountered, it stores it in session
  32. validation: This interceptor provides input validation
  33. workflow: It is used to implement validate method. If the action error is encountered it shows the input view.
  34. annotationWorkflow: It is used for implementing annotated methods. It supports @Before and @After annotations
  35. multiselect: It is configured as the default interceptor. It checks each parameter of the form. The default value inserted by this interceptor is a string array which is empty.