Struts2 Interceptor

Interceptor is one of the most powerful features of Struts2, it is a mechanism that allows users to perform some functional processing before Action execution and after Result execution. Many of the functions provided in the Struts2 framework are implemented using interceptors, including exception handling, file upload, lifecycle callbacks and validation, etc.

The advantages of using interceptors:
1. Simplify the implementation of Action. Interceptors can separate many functions from Action, which greatly reduces the code of Action.
2. Single Responsibility. Separate the more specific functions from the Action and distribute them into different interceptors, so that the function of each interceptor and the function of the Action itself are more single.
3. Improve code reusability. Java is an object-oriented language. When the code with the same function is encapsulated in the interceptor, the same interceptor can be configured for different Actions according to the functional needs.

The calling sequence of interceptors: The
interceptors are executed in the order of configuration, that is to say, whoever is configured in the front will execute first. But there is one point that must be noted: Struts2 default interceptors need to be configured before custom interceptors.

Some important interceptors of the Struts2 framework:

No. Interceptors and Instructions
1 alias: Allows parameters to use different aliases between requests.
2 checkbox: Assists in managing checkboxes by adding parameter value false for unchecked checkboxes.
3 conversionError: The error message that converts the string to the parameter type is placed in the error field of the action.
4 createSession: Automatically create an HTTP session if it doesn't already exist.
5 debugging: Provides developers with some different debugging screens.
6 execAndWait: When the action is executed in the background, send the user to the waiting page in the middle.
7 exception: Maps exceptions thrown from actions to results, allowing exceptions to be handled automatically by redirection.
8 fileUpload: facilitates file uploading.
9 i18n: Track selected regions during a user session.
10 logger: Provides simple logging by outputting the name of the action being executed.
11 params: set the request parameters on the action
12 prepare: This is typically used to perform preprocessing work, such as setting up database connections.
13 profile: Allows logging of simple profiling information for the action.
14 scope: Store and retrieve the action's state within the session or application scope.
15 ServletConfig: Provides access to various actions based on servlet information.
16 timer: Provides simple analysis information in the form of action execution time.
17 token: Check the validity of the action to prevent repeated submission of the form.
18 validation: Provides validation support for actions.

Create a custom interceptor

In fact, it mainly implements the Interceptor interface, which has three methods: init(), destroy(), intercept(ActionInvocation arg0). intercept(ActionInvocation arg0) is the real interceptor.

About the difference between interceptor and Filter
1. Interceptor is based on java reflection mechanism, while filter is based on function callback.
2. The filter depends on the servlet container, while the interceptor does not depend on the servlet container.
3. Interceptors can only work on Action requests, while filters can work on almost all requests.
4. Interceptors can access objects in the Action context and value stack, while filters cannot.
5. In the life cycle of Action, the interceptor can be called multiple times, while the filter can only be called once when the container is initialized.
Interceptor can be used in other environments, while Filter can only be used in Web environment.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389990&siteId=291194637