interceptor--Interceptor

Source: Internet
Author: User

First, the principle analysis

There is nothing special about the interface definition of interceptor, except the Init and Destory methods, The Intercept method is the core approach to implement the entire interceptor mechanism. and the parameter actioninvocation it relies on is the action dispatcher.
What needs to be pointed out here is a very important method of Invocation.invoke (). This is the method in Actioninvocation, and Actioninvocation is the action dispatcher, so this method has the following 2-layer meanings (see Defaultactioninvocation Source code in detail):
1. If there are other interceptor in the interceptor stack, then Invocation.invoke () will call the execution of the next interceptor in the stack.
2. If there is only action in the interceptor stack, then Invocation.invoke () invokes action execution.

Defaultactioninvocation part of the source code:

     PublicString Intercept (actioninvocation actioninvocation)throwsException {if(Interceptors.hasnext ()) {FinalInterceptormapping Interceptor =(interceptormapping) interceptors.next (); Utiltimerstack.profile ("Interceptor:" +interceptor.getname (),NewUtiltimerstack.profilingblock () { PublicString doprofiling ()throwsException {ResultCode= Interceptor.getinterceptor (). Intercept (defaultactioninvocation. This);//recursive call blocker                            return NULL;        }                    }); } Else{ResultCode=invokeactiononly (); }    }
ii. Order of execution

The order in which the code in each interceptor executes, before the action, the interceptor executes in the same order as defined in the stack, and after action and result, the interceptor executes in the opposite order as defined in the stack.

Interceptor Interception Type
From the above analysis, we know that the core part of the entire interceptor is the call location of the Invocation.invoke () function. In fact, we also formally make a distinction between interception types based on the invocation location of this code. In Struts2, the interception type of interceptor is divided into the following three categories:
1.before
Before interception refers to the code defined in the interceptor, which exists before the Invocation.invoke () code executes. These codes are executed sequentially in the order in which the interceptors are defined.
2.after
After interception refers to the code defined in the interceptor, which exists after the Invocation.invoke () code executes. The code will be executed in reverse order, according to the interceptor definition.
3.PreResultListener
Sometimes, before intercept and after intercept are not enough for us because we need to do something before the action is done, but before we go back to the view layer. STRUTS2 also supports such interception, which is achieved by registering an Preresultlistener interface in the Interceptor.
Example: Use the following code in the Interceptor, where Mypreresultlistener implements the Preresultlistener Interface and do something in the Beforeresult method and then add Action.addpreresultlistener (New Mypreresultlistener ()) to the Interceptor class;

From the source, we can see that the 4 different levels of the Action layer of the Struts2 we mentioned earlier are reflected in this method: Interceptors (Interceptor), action, Preresultlistener, and result. In this approach, the orderly invocation and execution of these levels is ensured.

interceptor--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.