Struts2 ---- interceptor

Source: Internet
Author: User

Struts2 ---- interceptor
What is an interceptor:

Interceptor, used in AOP (Aspect-OrientedProgramming) to intercept a method or field before it is accessed, and then add some operations before or after it. Interception is an implementation policy of AOP.

In the Chinese Document of Webwork, 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 reusable parts of an action.

When talking about Interceptor, we should also know the Interceptor Chain, which is called the Interceptor Stack in Struts 2 ). The interceptor chain is to link the interceptor into a chain in a certain order. When accessing intercepted methods or fields, the interceptor in the interceptor chain will be called in the order defined previously.

How interceptor works:

 

Most of the time, the interceptor method is called by proxy. The interceptor Implementation of Struts 2 is relatively simple. When a request arrives at the ServletDispatcher of Struts 2, Struts 2 searches for the configuration file, instantiates the relative interceptor object based on the configuration, and then Concatenates the object into a list ), finally, the interceptor in the list is called one by one. For example:

Cross-cutting, preprocessing, and postprocessing)

1. ActionInvocation encapsulates the action and the associated interceptor and result execution.

 

The call of an action is a layered process, which always contains a series of interceptors to be executed before or after the action is executed. Instead of calling the execute method of an action directly, the framework creates an ActionInvocation object, which encapsulates the action and a series of interceptors configured to be triggered before the action is executed.

 

2. General Command ActionInvocation

When the framework receives a request, it must first decide which action the URL maps;

An instance of this action will be added to a newly created ActionInvocation object;

The framework consulted the declarative architecture to discover the sequence in which the interceptor should be triggered;

References pointing to these interceptors are added to the ActionInvocation object;

The framework implements sequential execution of all interceptors through the invoke method of ActionInvocation;

 

3. interceptor principle: recursive call of interceptor

The framework starts the recursive call process by calling the invoke method of the ActionInvocation object for the first time;

ActionInvocation gives control to the first interceptor in the stack by calling the intercept method of the interceptor;

The intercept method takes the ActionInvocation instance as a parameter. During the process of the interceptor, it will call the invoke method on the ActionInvocation object to continue calling the recursive process of the subsequent interceptor.

The call process goes down through all the interceptors until there is no interceptor in the stack.

 

4. interceptor instances share actions

Although each request will create a new instance of the action, the interceptor will be reused. That is to say, the interceptor is stateless and should not store data related to the request being processed in the interceptor, this is not the role of the interceptor. The interceptor should only apply its processing logic to the request data. It can access the data already stored on different objects through ActionInvocation.

 

Define the interceptor and interceptor stack in the struts. xml file:

         
          
           
           
            
            
             
       
      
     
            
   
  ......
 
Use interceptor

Once the interceptor and interceptor Stack are defined, the interceptor or interceptor stack can be used to intercept Action. The interception behavior of the interceptor will be executed before the exceute method of the Action is executed.

            
 
  
/Success. jsp
             
 
  
/Error. jsp
 
 
 
             
          
  
 

Define interceptor

As a "framework", scalability is indispensable. Although Struts2 provides us with such rich interceptor implementations, this does not mean that we lose the ability to create custom interceptors. On the contrary, it is quite easy to define the interceptor in Struts 2.

Implement interceptor class:

All the interceptors of Struts 2 directly or indirectly implement interfaces.

Com. opensymphony. xwork2.interceptor. Interceptor. This interface provides three methods:

1) void init (); after the interceptor is initialized, the system calls back this method before the interceptor is blocked. For each interceptor, this method is executed only once.

2) void destroy (); this method corresponds to the init () method. The system calls back this method before the interceptor instance is destroyed.

3) String intercept (ActionInvocation invocation) throws Exception; this method is the interception action that the user needs to implement. This method returns a string as the logical view.

In addition, the inheritance class

Com. opensymphony. xwork2.interceptor. abstractInterceptor is a simpler way to implement the interceptor class, because this class provides empty implementations of the init () and destroy () methods, so we only need to implement the intercept method.


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.