Detailed configuration process of Struts2 interceptor

Source: Internet
Author: User

The Interceptor is deployed in almost every program. Therefore, the detailed process of the interceptor configuration is posted, hoping to help you.

Detailed configuration process of Struts2 interceptor

Zhang zhaozhao

You can also set parameters when using the Interceptor: configure the following in the reslut of an action:

/Success. jsp

/Register. jsp

Welcome

2. Relationship between interceptor stack and default interceptor

1: The interceptor stack and interceptor Stack are at the same level. That is to say, an interceptor stack contains many interceptors. An Interceptor stack can also contain many interceptor stacks. The configuration is as follows:

Zhang zhaozhao

Use of interceptor: 1. Define first; 2. Use in reference;

2: In struts2, the default interceptor stack of the system is defaultStack. If you manually reference your interceptor, the default interceptor stack of the system does not work. In this way, you must manually introduce the system interceptor stack.

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

Here, myStack is the name of the interceptor stack defined by myself. If the interceptor stack contains multiple interceptors, the sequence before the action is executed is the same as that before the interceptor is configured, the execution order after the action is the opposite;

3: AbstractInterceptor

1: Interceptor, a super Interceptor interface, has three methods to implement, but if you do not want to use init (); and destroy ()

Method, You can inherit this abstract interceptor class; its usage is no different from the above;

4: Method filter interceptor MethodFilterInterceptor

1: The interceptor above applies to the entire action. If it intercepts a method, it can inherit this class;

The method is similar to the method used above. You just need to configure it to intercept the method and filter the interceptor.

Do not configure it in the default interceptor stack. manually configure it.

Execute

Execute

Specifically, includeMethods and excludeMethods are fixed syntax: includeMethods contains interception methods. Multiple methods must be separated by commas (,). excludeMehtods is the method for blocking exclusion;

5: Listener PreResultListener Interface

1: its listening point is to do something before rendering the view after the interceptor executes an action method. Let a class implement this interface; register the following code with the interceptor that requires it:

Publicclass MyInterceptor3 extends MethodFilterInterceptor {

PrivatestaticfinallongserialVersionUID = 3756655410194005443L;

@ Override

Protected String doIntercept (ActionInvocation invocation) throws Exception {

//

Register the listener to intercept;

Invocation. addPreResultListener (new MyListener ());

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

String result = arg0.invoke ();

System. out. println ("my interceptor3 finshed! ");

Return result;

}

}


Related Article

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.