Three configuration methods for Struts2 Interceptor Interceptor __struts2

Source: Internet
Author: User
Tags object object
1, the STRUTS2 Interceptor interceptor three kinds of configuration method


Method 1. General Collocation method

<struts> <package name= ' struts2 ' extends= ' Struts-default ' > <in terceptors> <interceptor name= "Myinterceptor" class= "Edu.hust.interceptor.MyInterceptor" ></interc 
            eptor> </interceptors> <action name= "register" class= "Edu.hust.action.RegisterAction" > 
             
            <result name= "Input" >/register.jsp</result> <result>/result.jsp</result> <!--when you customize interceptor and ref it, the system overrides the default Interceptor-stack (Defaultstack) to ensure that the default defaultstack of the system is not  Impression, 
We need to explicitly introduce it into the--> <!--note the order of two interceptor-ref, the order is different, the execution effect is different: first configured first to perform/after the configuration of the first exit (advanced out) --> <interceptor-ref name= "Defaultstack" ></interceptor-ref> <interceptor-ref Name= "Myinterceptor" ></interceptor-ref> </action> </package> </struts>

Method 2. Configure the Interceptor stack (an element that will be concatenated with multiple interceptor). The interceptor stack is then introduced into the <action>.

<struts> 
    <package name= "struts2" extends= "Struts-default" > 
         
        <interceptors> 
            < Interceptor Name= "Myinterceptor" class= "Edu.hust.interceptor.MyInterceptor" ></interceptor> 
         
            < Interceptor-stack name= "Myinterceptorstack" > 
                <interceptor-ref name= "Myinterceptor" ></ interceptor-ref> 
                <interceptor-ref name= "Defaultstack" ></interceptor-ref> 
            </ interceptor-stack> 
        </interceptors> 
         
        <action name= "register" class= " Edu.hust.action.RegisterAction "> 
            <result name=" input ">/register.jsp</result> 
            <result >/result.jsp</result> 
             
            <interceptor-ref name= "Myinterceptorstack" ></interceptor-ref> 
        </action> 
    </package> 
</struts>


Method 3. Modify the default interceptor to define the custom interceptor stack as the default interceptor for STRUTS2.

<struts> <package name= "struts2" extends= "Struts-default" > <interceptors> 
            <interceptor name= "Myinterceptor" class= "Edu.hust.interceptor.MyInterceptor" ></interceptor> <interceptor-stack name= "Myinterceptorstack" > <interceptor-ref name= "myinterceptor" ></in terceptor-ref> <interceptor-ref name= "Defaultstack" ></interceptor-ref> </i Nterceptor-stack> </interceptors> <!--This default interceptor is for all action--> <!--if Interceptor is introduced in an action, this default interceptor is invalidated in this action--> <default-interceptor-ref name= "Myinterceptorstac K "></default-interceptor-ref> <action name=" register "class=" Edu.hust.action.RegisterAction "> <result name=" input ">/register.jsp</result> <result>/result.jsp</resu Lt> </actIon> </package> </struts> 

2. Interceptor's Role Object

(1) Intercept the target object (the proxy object), where the target object is action;

(2) Interceptor (a class that dynamically inserts some methods into the target object's before, after);

(3) The (dynamic) proxy object generated by the target object (the Object object Method + Interceptor method is synthesized by the internal method of the proxy objects). The program ultimately executes the proxy of the target object, which has been inserted into the interceptor.  

Interceptor action: intercept action. Interceptor is equivalent to an entry and exit, through interceptor into the action, execution of the action code and then through the interceptor out.

Here's a struts2-interceptor (Interceptor How to write) the first two examples of this article are configured, as follows

<struts> <package name= "struts2" extends= "Struts-default" > <interceptors> 
            <interceptor name= "Myinterceptor" class= "Edu.hust.interceptor.MyInterceptor" ></interceptor> <interceptor name= "MyInterceptor2" class= "Edu.hust.interceptor.MyInterceptor2" ></interceptor> & Lt;interceptor-stack name= "Myinterceptorstack" > <interceptor-ref name= "myinterceptor" ></inter ceptor-ref> <interceptor-ref name= "MyInterceptor2" ></interceptor-ref> ; Interceptor-ref name= "Defaultstack" ></interceptor-ref> </interceptor-stack> </int  
         
        erceptors> <default-interceptor-ref name= "Myinterceptorstack" ></default-interceptor-ref> <action name= "register" class= "edu.hust.action.RegisterAction" > <result name= "in Put ">/register.jsp</result> <result>/result.jsp</result> </action> </package&gt 
; </struts>


The console will get the following print output

Intercept start
intercept2 start
2008-9-19 19:42:06 Com.opensymphony.xwork2.validator.ActionValidatorManagerFactory <clinit>
Information: Detected Annotationactionvalidatormanager, initializing it ...
Intercept2 finish
intercept finish

Related: Struts2 working principle (diagram) detailed 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.