Introduction
One of the biggest scams in computer programming is trying to understand the concepts by looking at their literal meaning. Here, the case is not at all different from others. Eventually, the literal meaning of the term interceptors has got nothing to do with its importance in the field of computer science, or more specifically, struts.
Let us learn more about it in the subsequent sections.
Interceptors
Interceptors are nothing more than an object that is created at the time of pre-processing or the post-processing of a request. In Struts 2, the interceptor is used to perform operations such as validation, exception handling, internationalization, displaying intermediate results etc.
There are many interceptors provided by the struts 2 frameworks. We have the option to create our interceptors. The struts 2 default interceptors are as follows:
Default Interceptors
S.No | Default interceptors | Description |
1 | Alias | It permits the use of various name aliases for parameters across requests. |
2 | Checkbox | It adds a parameter value of false for check boxes that are not checked to help manage checkboxes. |
3 | ConversionError | It inserts error data from string to parameter type conversion into the action's errors field. |
4 | CreateSession | If no HTTP session is present, one is automatically created. |
5 | Debugging | gives the developer a variety of debugging windows. |
6 | Timer | reveals basic profiling data in the form of the execution time of an action. |
7 | ExecAndWait | It sends the user to a waiting page in between the action being completed in the background. |
8 |
ServletConfig |
gives the action access to a variety of servlet-based data. |
9 | Profile | enables the logging of basic profiling data for actions. |
10 | Validation | It provides support to input validation. |
11 | Token | The action is checked for a valid token to avoid multiple form submissions |
12 | Scope | the status of the action is saved and retrieved in the session or application scope. |
13 | Prepare | Usually, this is used for pre-processing tasks like setting up database connections. |
14 | Exception | enables automatic exception handling through redirection by mapping exceptions that are thrown from an action to a result. |
15 | Params | sets the action's request parameters. |
16 |
fileUpload |
allows for simple file uploading. |
17 | i18n | keeps track of the user's session's chosen locale. |
These were some of the default interceptors which are already present in the struts2.
Now let us see an example of interceptors in use.