Nineth Chapter Interceptor

Source: Internet
Author: User
Tags abstract

Nineth Chapter Interceptor

The interceptor works as shown above, and each action request is wrapped inside a series of interceptors. The interceptor can do similar operations in the action execution line, or it can be recycled after the action is executed straight.

Each action can either transfer the action to the following interceptor, or the action can exit the operation and return to the customer's given screen.

How to customize an interceptor.

Customizing an interceptor requires three steps:

1 Customize a class that implements the Interceptor interface (or inherits from Abstractinterceptor).

2 Register the Interceptor defined in the previous step in Strutx.xml.

3 The interceptor defined above is referenced in the action that needs to be used, and the interceptor can be defined as the default interceptor for convenience, so that all actions are intercepted by the interceptor without special declarations.

The Interceptor interface declares three methods:

Public interface Interceptor extends Serializable {

void Destroy ();

void Init ();

String Intercept (actioninvocation invocation) throws Exception;

}

The Init method, after the Interceptor class is created, is called before the action image is intercepted, which is equivalent to a Post-constructor method, which allows the Interceptor class to do the necessary initial session operations.

The Destroy method is called before the Interceptor is garbage collected, and is used to reclaim resources initialized by the Init method.

Intercept is the main interception method of interceptors, if you need to invoke the subsequent action or interceptor, just call the Invocation.invoke () method in the method, before and after the method call can insert the action call before and after the interceptor needs to do the method. If you do not need to invoke subsequent methods, you can return an object of type string, such as action.success.

In addition, Abstractinterceptor provides a simple implementation of the Interceptor, which is implemented as:

Public abstract class Abstractinterceptor implements interceptor {

public void init () {

}

public void Destroy () {

}

public abstract String Intercept (Actioninvocation invocation) throws Exception;

}

When you do not need to write the Init and Destroy methods, you only need to inherit from Abstractinterceptor and implement the Intercept method.

We are trying to write a session filtering interceptor that checks to see if there is a specific attribute in the user session (login property) If it does not exist, abort the subsequent operation to navigate to login, otherwise perform the original operation, the code is:

public class Checklogininterceptor extends Abstractinterceptor {

public static final String Login_key = "LOGIN";

public static final String login_page = "Global.login";

Public String Intercept (Actioninvocation actioninvocation) throws Exception {

SYSTEM.OUT.PRINTLN ("Begin check Login interceptor!");

Don't do this interception on loginaction.

Object action = Actioninvocation.getaction ();

if (action instanceof loginaction) {

System.out.println ("Exit check login, because this is login action.");

return Actioninvocation.invoke ();

}

Verify that login is present in the session

Map session = Actioninvocation.getinvocationcontext (). GetSession ();

String login = (string) session.get (Login_key);

if (login! = null && login.length () > 0) {

In case of a subsequent operation.

System.out.println ("Already login!");

return Actioninvocation.invoke ();

} else {

Otherwise terminates the subsequent operation and returns login

SYSTEM.OUT.PRINTLN ("No login, forward login page!");

return login_page;

}

}

}

Registering interceptors

<interceptors>

<interceptor

Name= "Login"

class= "Com.jpleasure.teamware.util.CheckLoginInterceptor"/>

<interceptor-stack name= "Teamwarestack" >

<interceptor-ref name= "Login"/>

<interceptor-ref name= "Defaultstack"/>

</interceptor-stack>

</interceptors>

Set the above interceptor as the default interceptor:

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

This will be intercepted by login before all actions within the same package are executed.

Struts2 (Xwork ) provides the function description of the Interceptor:

 

Interceptor

name

Description

Alias Interceptor

Alias

Convert request parameters to different names in different requests, request content unchanged

Chaining Interceptor

Chain

Let the previous action belong to Sex can be accessed by the latter action, and is now used in conjunction with the chain type of result (<result type= "Chain" >).

CheckBox Interceptor

checkbox

Added checkb The ox automatically handles the code, setting the contents of the unchecked checkbox to False, while HTML does not submit a checkbox that is not checked by default.

Cookies Interceptor

Cookies

Use configured Name,v Alue means cookies

Conversion Error Interceptor

Conversionerror /p>

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.