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.

Related Topics

Struts 2 param tag

The param tag in struts is used to set values of the variable in other tags like include and bean tag. It is used to parametrize other tags. Struts param...

4 minutes read.

Struts 2 Installation and Example

Struts Installation: For developing struts applications, you need to have java installed on your machine. For verifying that Java is already installed on your machine, use the following commands: For Windows: Open Command line.Type...

4 minutes read.

Struts 2 Date Validator

In the struts web application, a date validator is used to check whether the entered date is within the specified range. Three parameters defined with date validator are: fieldName: It is...

5 minutes read.

Struts 2 Custom Interceptor

Custom Interceptor can be implemented in struts application by implementing Interceptor interface. We have to override all three life cycle methods. The Interceptor interface itself provides methods to implement custom...

4 minutes read.

Struts 2 Multiple Configuration file

In order to create large struts web applications, multiple configuration files are used. Multiple configuration files are included in struts.xml file using include sub-element. Struts- Multiple Configuration File Example- To get input...

5 minutes read.

Struts 2 execAndWait interceptor

The execute and wait interceptor is also known as execAndWait interceptor. The intermediate result is displayed with the help of this interceptor. The wait result is specified in the struts.xml...

4 minutes read.

Struts 2 url tag

The url tag in struts is used to create url as a text. It creates url as a text string. Struts Data tags- url tag Example: Create the index.jsp file showing the...

4 minutes read.

Struts 2 Database Access

The database can be connected with struts applications to store and retrieve data using JDBC. Struts Database Access example: Create the database and create the table in it: Create database stuser;  // create...

4 minutes read.

Struts 2 URL Validator

In the struts, web application, a url validator is used to check whether the entered url is valid or not. One parameter defined with url validator is: fieldName: It is used...

4 minutes read.

Struts 2 exception Interceptor

In the struts web application, an exception may occur at any point. This exception should be handled properly. The global exception handling feature is provided by struts. With the help...

4 minutes read.

Struts 2 include tag

The include tag in struts is used to include some other resource like other jsp or servlet file in the existing jsp page. In the struts web application, it is...

4 minutes read.

Struts 2 Datetimepicker tag

The datetimepicker tag in struts is used when the user needs to take date input. It displays a calendar icon. When we click on this icon, it allows us to...

4 minutes read.

Struts 2 Tutorial

Struts is mainly used to create java-based applications. It is the complete framework used in activities ranging from development to deployment and maintenance. Struts is the MVC (Model View Controller)...

4 minutes read.

Struts 2 If Else tag

In the struts, web application, the If Else tag is used to check among multiple conditions. The code along with the right condition will be executed as the result. Struts Control...

4 minutes read.

Struts 2 params Interceptor

Parameters Interceptor is also known as params interceptor implemented in struts application by implementing Interceptor interface. We have to override all three life cycle methods. The Interceptor interface itself provides...

4 minutes read.

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...

3 minutes read.

Struts 2 Generator tag

The generator tag in struts is used to iterate over the collection of elements provided by the val attribute. It allows us to iterate through every element and perform the...

4 minutes read.

Struts 2 property tag

The property tag in struts is used to get values of the fields set in the bean class. Struts Data tags- Property tag Example: Create the index.jsp file showing the use of...

4 minutes read.

Struts 2 Iterator tag

In the struts web application, the Iterator tag is used to iterate through any collection to print values present in it. Iterator tag iterate over each element in the collection...

4 minutes read.

Struts 2 prepare Interceptor

In order to implement prepare interceptor in the struts web application, the class must implement a Preparable interface and override the prepare method along with the execute method. While executing...

4 minutes read.