Detailed description of struts interceptor and struts interceptor

Source: Internet
Author: User

Detailed description of struts interceptor and struts interceptor
1. Interceptor: The Struts2 interceptor intercepts a field before or after accessing an Action or Action. The Struts2 interceptor is pluggable and is an implementation of AOP.

Explanation of WebWork Chinese document: the interceptor is the object for dynamically intercepting Action calls. It provides a mechanism for developers to define the code to be executed before and after an Action is executed, or to prevent it from being executed before an action is executed. It also provides a way to extract code that can be reused in an Action.

Interceptor Stack: The Struts2 Interceptor Stack connects the Interceptor into a chain in a certain order. When accessing intercepted methods or fields, the interceptor in the Struts2 interceptor chain will be called in the order defined previously.

2. Role of interceptor:
(1). DRY principle: Dont't Repeat Yourself.
(2) Advantages of interceptor in design and program structure:
The interceptor can separate many functions from the Action and distribute them into different interceptors, reducing the code of the Action. In this way, the interceptor and Action functions are more simple. When the common function code is encapsulated in the Interceptor (Code modularization), you can configure the interceptor of the corresponding function for different actions according to the function requirements. The reusability of the functions implemented by the interceptor is improved, and the fabricated and pluggable architecture is realized in disguise, making the entire system structure more flexible.
A>. Simplify the implementation of Action
B>. simpler functions
C>. Common Code Modularization
D>. improve reusability

Note:

The interceptor adopts the responsibility chain mode:
(1). In the responsibility chain mode, many objects are connected by each object's reference to the next one to form a chain.
(2). Each node in the responsible chain can continue to call the next node or stop the process from being executed.

3. Differences between interceptor and filter (to be deeply understood ):
(1) The Interceptor is based on the java reflection and dynamic proxy mechanisms, while the filter is based on method callback.
(2). The filter depends on the servletAPI, And the interceptor does not rely on the servletAPI.
(3) the interceptor can only act on Action requests, while the filter can act on almost all requests.
(4). the interceptor can access objects in the Action context and value stack, but the filter cannot.

4. struts Interceptor (predefined Interceptor: In the struts-default.xml file)

Interceptor Example 1:


Interceptor Example 2:


5. Struts2 execution schematic:



Schematic diagram:

(1). the client sends a request;
(2) The request goes through a series of filters: the Optional Filter ActionContextCleanUp helps Struts2 integrate with other frameworks. For example, SiteMesh Plugin.
(3) FilterDispatcher is called, and FilterDispatcher asks ActionMapper to determine whether the request needs to call an Action.
(4) If ActionMapper decides to call an Action, FilterDispatcher will hand over the request processing to ActionProxy.
(5). ActionProxy queries the framework Configuration file through Configuration Manager and finds the Action class to be called.
(6). ActionProxy creates an ActionInvocation instance.
(7). Before and after the ActionInvocation instance calls the Action, it involves the call of the relevant Interceptor (Intercepter.
(8). Once the Action is executed, ActionInvocation is responsible for finding the corresponding return result based on the configuration in struts. xml. The returned result is a JSP or other page (or another Action chain ). You can use tags in the Struts2 framework to display JSP pages (this process involves ActionMapper ).

All objects (actions, Interceptors, and Results) in the preceding process are created by the ObjectFactory in the xwork container.

6. Each Interceptor is a Java class that implements the com. opensymphony. xwork2.interceptor. Interceptor interface:
Init: This method will be called immediately after the interceptor is created. It is called only once within the lifecycle of the Interceptor. You can perform necessary initialization for related resources in this method.
Interecept: This method is called once every time an action request is intercepted.
Destroy: This method will be called before the interceptor is destroyed. It is also called only once in the lifecycle of the interceptor.

7. Intercepter Interface

Struts2 calls the interecept method of each interceptor registered by the programmer for an Action in sequence.
Every time you call the interecept method, Struts will pass an instance of the ActionInvocation interface.
ActionInvocation: indicates the execution status of a given Action. The interceptor can obtain the Action object and Result object associated with the Action from the object of this class. after the interceptor completes its own task, the interceptor will call the invoke method of the ActionInvocation object to the next step of the Action processing process.
You can also call the addPreResultListener method of the ActionInvocation object to upload one or more PreResultListener listeners to the ActionInvocation object. The Listener object can do something before the action is executed and the result is executed.
The AbstractInterceptor class implements the Interceptor interface and provides a blank implementation for init and destroy.

8. Custom interceptor steps:

(1). Custom interceptor class
(2) configure the custom interceptor in the struts. xml file.


What are the main application methods of the interceptor in struts?

It is mainly used to block some illegal operations. For example, if a user does not log on to your system and directly enters the URL of a module in your system, the module can be opened, which leads to problems, if you use an interceptor to intercept the log on, the logon page is directly opened instead of the module page.
Interceptor can be used in many aspects

How to disable an interceptor in struts2?

Re-configure an interceptor stack of your own, and adjust the interceptor in the order of the interceptor in the struts-default stack to remove what you don't want, replace the default interceptor stack of the package.
View the part of the custom interceptor stack in the tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.