Struts explains the interceptor in detail

Source: Internet
Author: User

1. Interceptor: The STRUTS2 Interceptor will have an action or action method. Before or after intercepting the field, and the Struts2 interceptor is pluggable, an implementation of the Interceptor AOP.

WebWork: Interceptors are objects that dynamically intercept action calls.

It provides a mechanism to enable developers to define code that runs before and after an action is run. It is also able to prevent it from running before an action is run.

At the same time, it provides a way to extract reusable code from an action.



Interceptor Stack (Interceptor stack): The Struts2 Interceptor Stack is a chain that connects interceptors in a certain order. When visiting an intercepted method or field, interceptors in the Struts2 interceptor chain are called in the order in which they were previously defined.

2. The role of interceptors:
(1). Dry principle: Dont ' t Repeat yourself.
(2). The advantages of interceptors in design and program structure:
Interceptors can separate the very functions from the action and scatter them into different interceptors. The code for the action is lowered.

In this way, the interceptors and the action itself are more functional.

When the generic function code is encapsulated inside the Interceptor (code modularity), it is possible to configure the appropriate interceptor for different actions, depending on the function required. It improves the reusability of the functions implemented by interceptors, and implements the architecture of assembly and pluggable in disguise. Makes the entire system structure more flexible.
A>. Simplifying the implementation of an action
B> more than a single function
c> generic code modularity
D> improve reusability

Attention:

The interceptor adopts the chain of responsibility Model:
(1). In the responsibility chain model, very many objects are linked together by each object's next reference to form a chain.


(2). Responsibility chain each node is able to continue calling the next node. Can also prevent the process from continuing to run

3. The difference between the interceptor and the filter (to be understood deeply):
(1). Interceptors are based on Java reflection and dynamic proxy mechanisms, and filters are based on method callbacks.
(2). The filter is dependent on the SERVLETAPI, and the interceptor is not dependent on SERVLETAPI.
(3). Interceptors can only function on action requests. Filters, however, can work on nearly all requests.
(4). The interceptor is able to access the action context, the object in the value stack. And the filter cannot .

4.struts Interceptor (pre-defined interceptor: in the Struts-default.xml file)

Interceptor Demo Example 1:


Interceptor Demo Example 2:


5.struts2 operation schematic diagram:



Explanations for the schematic diagram:

(1). client sends a request;
(2). The request passes through a series of filters: the optional filter actioncontextcleanup, which helps Struts2 and other frameworks to integrate.

For example: Sitemesh Plugin.
(3). Then Filterdispatcher is called, Filterdispatcher asks Actionmapper to determine if the request needs to invoke an action.


(4). If Actionmapper decides to call a action,filterdispatcher to give actionproxy the processing of the request.
(5). Actionproxy asks the configuration file of the framework through the Config manager to find the action class that needs to be called.


(6). Actionproxy Create an instance of Actioninvocation.
(7). The Actioninvocation instance invokes the action before and after it involves a call to the relevant interceptor (Intercepter).
(8). Once the action is complete, actioninvocation is responsible for finding the corresponding return result based on the configuration in Struts.xml. The return result is a JSP or other page (which can also be a different action chain). JSP page presentation can use tags in the STRUTS2 framework (this process involves actionmapper).



All objects (Action, interceptors, results, and so on) in the above process are created by objectfactory in the Xwork container.

6. Each interceptor is implemented with Com.opensymphony.xwork2.interceptor. Java class for the Interceptor interface:
init: This method is called immediately after the Interceptor is created, and it is called only once during the life cycle of the interceptor. Be able to initialize the relevant resources in this method as necessary
interecept: Every action request is intercepted and the method is called once.
Destroy: This method is called before the Interceptor is destroyed, and it is called only once during the life cycle of the interceptor.

7.Intercepter interface

The Struts2 invokes the Interecept method of each interceptor that the program ape has registered for an Action.
Each time the Interecept method is called, Struts passes an instance of the Actioninvocation interface.
Actioninvocation: Represents the running state of a given action, and the interceptor is able to get the action object and the Result object associated with the action from the object of the class. After completing the interceptor's own task, the interceptor advances to the next step of the Action processing process by invoking the Invoke method of the Actioninvocation object.
You can also call the Addpreresultlistener method of the Actioninvocation object to "hang" one or more Preresultlistener listeners for the Actioninvocation object. The listener object can do something before running the action result after the action is completed
The Abstractinterceptor class implements the Interceptor interface. And for Init, destroy provides a blank implementation

8. Define the Interceptor Steps yourself:

(1). Define the Interceptor class yourself
(2). Configure your own defined interceptors in the Struts.xml file

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Struts explains the interceptor in detail

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.