Reprint-STRUTS2 Interceptor Detailed configuration process

Source: Internet
Author: User

Source: http://www.blogjava.net/zzzlyr/archive/2009/10/12/297998.html

Struts2 Interceptor Detailed configuration process

1: The Super interface of all interceptors interceptor, action to implement this interface;

Interceptor it has three methods (init (), Destroy (), Interceptor ()):

Init () Method: Load once when the server is starting, and load only once;

Destroy () Method: The method to be executed when the interceptor is destroyed;

Interceptor () Method: Inside there is a parameter invocation

Public String Intercept (actioninvocation invocation) throws Xception {

System. out. println ("interceptor!!");

String Result=invocation.invoke ();

return result;

}

Invocation.invoke () is if only one interceptor executes this method, it is returned to the view, and if there are multiple interceptors, it executes all of the interceptors sequentially before returning to the view.

2: You can specify the default parameters for the interceptor in system initialization (also including the definition of Interceptor) as follows:

In the Interceptor class, the Hello is injected into the Interceptor class as the attribute Set/get method;

<interceptors>

<!--first define the interceptor--

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

<!--Specify the 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 have only one default interceptor, so the interceptor stack will be applied to all actions by default.

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

</default-interceptor-ref>

You can also set parameters for the interceptor when you use it:

is configured below the reslut of an action as follows:

<action name= "Register"

class= "Com.zzz.struts2.action.RegisterAction" >

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

<!--result It also has a message forwarding type type= "" Remember that if you don't turn to JSP, you 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: Interceptor and Interceptor Stack is a level, which means that an interceptor stack includes many interceptors, and an interceptor stack can also include many interceptor stacks, configured as follows:

<interceptors>

<!--first define the interceptor--

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

<!--Specify the 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. use in reference;

<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, if you manually reference their interceptors, the system default interceptor stack will not work, so it is necessary to manually introduce the system interceptor stack <interceptor-ref name= " Defaultstack ">

</interceptor-ref>

If you want to change the system's default interceptor stack, you can configure this:

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

</default-interceptor-ref> where Mystack is its own definition of the interceptor stack name;

If there are multiple interceptors in the interceptor stack, the order in which the action is executed is the same as the order in which the interceptor is configured, and the order of execution after the action is reversed;

3: Abstract Interceptor Class Abstractinterceptor

1:interceptor This super Interceptor interface , There are three ways to implement , But if you don't want to use init ();

and the Destroy () method , You can go 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, if the interception for a method can inherit the class;

Its use is similar to the use of the above, just need to configure it to intercept that method, the method filter interceptor is best not to configure itself to set the default interceptor stack inside, manual configuration.

Interceptor-ref name= "MyInterceptor3" >

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

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

</interceptor-ref>

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

Among them includemethods, excludemethods is fixed notation: Includemethods contains interception of 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 executes an action method, it does something before rendering the view, allowing a class to implement the interface;

Then register the following code with the interceptor that needs it:

Public class MyInterceptor3 extends methodfilterinterceptor {

Private Static Final Long Serialversionuid = 3756655410194005443L;

@Override

protected String dointercept (actioninvocation invocation) throws Exception {

Register the listener with the interceptor;

Invocation.addpreresultlistener (new MyListener ());

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

String Result=arg0.invoke ();

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

return result;

}

}

Reprint-STRUTS2 Interceptor Detailed 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.