STRUTS2 Interceptor Specific configuration process

Source: Internet
Author: User

Interceptors almost spread out in every program, so the specific interceptor configuration process, I hope to help everyone.

STRUTS2 Interceptor Specific configuration process

<interceptors>

<!--first define the interceptor--

<interceptor name= "Myinterceptor" class= "Com.zzz.struts2.interceptor.MyInterceptor" >

<!-- Specifies the number of parameters that the system initializes to the interceptor.

<param name= "Hello" > Zhang </param>

</interceptor>

<!-- added to your own set of interceptor stacks .

<interceptor-stack name= "Mystack" >

<interceptor-ref name= "Myinterceptor" >

</interceptor-ref>

<interceptor-ref name= "Defaultstack" ></interceptor-ref>

</interceptor-stack>

</interceptors>

<!-- Change the system's default interceptor to its own default interceptor , and a system can only have a default interceptor , This will apply the interceptor stack to all actions by default.

<default-interceptor-ref name= "Mystack" >

</default-interceptor-ref>

It is also possible to set a parameter to the interceptor when it is used : It is on the reslut of an action such as the following :

<action name= "register" class= "Com.zzz.struts2.action.RegisterAction" >

<result name= "Success" >/success.jsp</result>

<!--result It's another message forwarding type type= "" Remember, suppose you don't turn to the JSPand turn to the chart. Can change

Type= "" value -

<result name= "Input" >/register.jsp</result>

<interceptor-ref name= "Myinterceptor" >

<param name= "Hello" >welcome</param>

</interceptor-ref>

<interceptor-ref name= "Mystack" ></interceptor-ref>

</action>

2. The relationship between the Interceptor , the interceptor stack, and the default interceptor

1: interceptors and interceptors stack is a level , which means that an interceptor stack contains many interceptors , and an interceptor stack can contain many interceptor stacks , configuration such as the following methods :

<interceptors>

<!-- First define the Interceptor --

<interceptor name= "Myinterceptor" class= "Com.zzz.struts2.interceptor.MyInterceptor" >

<!-- Specifies the number of parameters that the system initializes to the interceptor.

<param name= "Hello" > Zhang </param>

</interceptor>

<!-- added to your own set of interceptor stacks .

<interceptor-stack name= "Mystack" >

<interceptor-ref name= "Myinterceptor" >

</interceptor-ref>

<interceptor-ref name= "Defaultstack" ></interceptor-ref>

</interceptor-stack>

</interceptors>

Use of interceptors : 1. Define First ; 2. in reference use ; 

<interceptor name= "Myinterceptor" class= "Com.zzz.struts2.interceptor.MyInterceptor" >

<interceptor-ref name= "Myinterceptor" >

</interceptor-ref>

2:struts2 has a system default interceptor stack is defaultstack, Assuming you manually reference your own interceptors , the system default interceptor stack will not work ; This requires manual introduction of the system's interceptor stack

<interceptor-ref name= "Defaultstack" > </interceptor-ref>

Suppose you want to change the default interceptor stack for the system, you can configure this :

<default-interceptor-ref name= "Mystack" > </default-interceptor-ref>

The mystack is a custom interceptor stack name ; Suppose there are multiple interceptors in the interceptor stack , in the Run Action the previous order is consistent with the order in which the interceptors are configured , and in Action then the order of the runs is reversed ; 

3: Abstract Interceptor class Abstractinterceptor

1:interceptor This Super Interceptor interface , There are three methods to implement , but assume that you do not want to use init (); and destroy ()

Method , the ability to inherit this abstract interceptor class ; its use is no different from the top. ;

4: method Filter Interceptor methodfilterinterceptor

1: The upper interceptor is for the entire action , Assuming that the interception of a method can inherit the class ;

Its use is almost the same as the usage above , just need to configure it to intercept the method , filter The Interceptor

Good do not configure yourself to set the default interceptor stack inside , You manually configure .

<interceptor-ref name= "MyInterceptor3" >

<param name= "Includemethods" >execute</param>

<param name= "Excludemethods" >execute</param>

</interceptor-ref>

<interceptor-ref name= "Defaultstack" ></interceptor-ref>

Among includemethods, Excludemethods is a fixed notation : Includemethods includes intercepting those methods ,  Multiple methods need to be separated by "," ; Excludemehtods is the method of excluding interception ;

5: listener preresultlistener interface

1: Its listening point after the interceptor has run through an action method , do something before rendering the view, and let a class implement the interface; Then register it with the interceptor that needs it. For example, the following code:

Publicclass MyInterceptor3 extends Methodfilterinterceptor {

Privatestaticfinallongserialversionuid = 3756655410194005443L;

@Override

Protected String dointercept (actioninvocation invocation) throws Exception {

//

Register the detector with the Interceptor ;

Invocation.addpreresultlistener (New MyListener ());

System.out.println ("My Interceptor3");

String Result=arg0.invoke ();

System.out.println ("My Interceptor3 finshed!");

return result;

}

}


STRUTS2 Interceptor Specific configuration process

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.