STRUTS2----Interceptor

Source: Internet
Author: User

What is an interceptor:

Interceptors, which are used in AOP (aspect-orientedprogramming) to intercept a method or field before it is accessed, and then add some action before or after it. Interception is an implementation strategy of AOP.

The Chinese document in WebWork is interpreted as--interceptors are objects that dynamically intercept action calls. It provides a mechanism to enable developers to define code that executes before and after an action executes, or to prevent it from executing before an action executes. It also provides a way to extract the reusable parts of an action.

When it comes to interceptors, there's one more word you should know-the interceptor chain (Interceptor Chain, called the Interceptor Stack interceptor stackin Struts 2). The Interceptor chain is a chain that binds the interceptor in a certain order. When accessing an intercepted method or field, interceptors in the interceptor chain are called in the order in which they were previously defined.

the implementation principle of interceptors:


Most of the time, the Interceptor method is invoked in the form of proxies. The block implementation of Struts 2 is relatively straightforward. When the request arrives at the servletdispatcher of Struts 2, struts 2 looks for the configuration file, instantiates the relative interceptor object based on its configuration, and then strings it into a list, the last one to invoke the interceptor in the list. Such as:

Crosscutting (cross-cutting), pretreatment (preprocessing), post-processing (postprocessing)

1. Actioninvocation encapsulates the action and the execution of the interceptor and result associated with it


The invocation of an action is a layered process that always contains a series of interceptors that are executed before or after the action is executed. Instead of invoking the Execute method of the action directly, the framework creates an Actioninvocation object that encapsulates the action and a series of interceptors that are configured to fire before the action executes.


2, commander in chief Actioninvocation

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

An instance of this action is added to a newly created Actioninvocation object;

The framework consults the declarative architecture to discover which interceptors should be triggered and in what order;

References to these interceptors are added to the Actioninvocation object;

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


3. Interceptor principle: recursive invocation of interceptors

The framework begins the recursive invocation process by invoking the Invoke method of the Actioninvocation object for the first time;

Actioninvocation gives control to the first interceptor in the stack by invoking the Intercept method of the interceptor;

The Intercept method takes the Actioninvocation instance as a parameter, and during the interceptor's processing, it invokes the Invoke method on the Actioninvocation object to continue the recursive process of invoking the subsequent interceptor.

The calling process passes through all the interceptors until the action starts triggering when no interceptors are in the stack.


4. Interceptor instances are shared between actions

Although each request creates a new instance of the action, the Interceptor is reused, which means that the interceptor is stateless and does not store the data associated with the request currently being processed in the interceptor, which is not the role of the Interceptor. Interceptors should only apply its processing logic to the request data, and can access data that is already stored on different objects through actioninvocation.

To define the interceptor in the Struts.xml file, the Interceptor stack:

<span style= "Background-color:rgb (255, 255, 255); ><package name= "My" extends= "Struts-default" namespace= "/manage" >        <interceptors>        <!-- Defining interceptors--        <interceptor name= "Interceptor name" class= "Interceptor Implementation Class"/>        <!--defining Interceptor stacks--        < Interceptor-stack name= "Interceptor stack name" >        <interceptor-ref name= "Interceptor One"/>        <interceptor-ref name= "Interceptor Two"/ >        </interceptor-stack>        </interceptors>        ......</package></span>
using interceptors

Once you have defined the interceptor and interceptor stack, you can use this interceptor or interceptor stack to intercept the action. The Interceptor 's interception behavior will be executed before the action's Exceute method executes.

<span style= "Background-color:rgb (255, 255, 255); ><action name= "useropt" class= "org.qiujy.web.struts2.action.UserAction" >            <result name= "Success" >/success.jsp</result>            <result name= "error" >/error.jsp</result><!--using Interceptors, General configuration after result, the--><!--refer to the system's default interceptor--><interceptor-ref name= "Defaultstack"/>            <interceptor-ref Name= "Interceptor name or Interceptor stack name"/>        </action></span>

Defining Interceptors

As a framework, extensibility is essential. While STRUTS2 provides us with such a rich interceptor implementation, this does not mean that we lose the ability to create custom interceptors, but on the contrary, it is quite easy to customize interceptors in Struts 2.

Implement the Interceptor class:

All struts 2 interceptors are directly or indirectly implemented with interfaces

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

1) void Init (); After the interceptor is initialized, the system backs up the method before the interceptor performs interception. For each interceptor, this method executes only once.

2) void Destroy (); The method corresponds to the init () method. The system will callback this method until the interceptor instance is destroyed.

3) Stringintercept(actioninvocation invocation) throws Exception; This method is the interception action that the user needs to implement. The method returns a string as a logical view.

In addition, inheriting classes

Com.opensymphony.xwork2.interceptor. Abstractinterceptor is a simpler way to implement an interceptor class, because this class provides an empty implementation of the Init () and Destroy () methods, so we only need to implement the Intercept method.


STRUTS2----Interceptor

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.