How does struts customize an interceptor?

Source: Internet
Author: User
Tags i18n

How to customize an interceptor.

It takes three steps to customize an interceptor:

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 Refer to the above defined interceptor in the action that needs to be used, and for convenience The interceptor can also be defined as the default interceptor, so that all action is intercepted by the interceptor without special declaration.

The Interceptor interface declares three methods:

Public interface Interceptor extends Serializable {

void Destroy ();

void Init ();

String Intercept (actioninvocation invocation) throws Exception;

}

The Init method is called after the Interceptor class is created, before intercepting the action mirror, which is equivalent to a Post-constructor method, which can be used to do the necessary initial action for the Interceptor class.

The Destroy method is invoked before the interceptor is garbage collected to reclaim the resources initialized by the Init method.

Intercept is the interceptor's main interception method, if you need to invoke a subsequent action or interceptor, you only have to call the Invocation.invoke () method in the method, you can insert before and after the method call the action called the interceptor need to do before and after 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 interceptor implementation, which is:

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 try to write a session filter interceptor that looks at the user session for a specific attribute (login property) If it does not exist, aborts the subsequent operation to login, otherwise performs 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!");

Do not do the interception for Loginaction

Object action = Actioninvocation.getaction ();

if (action instanceof loginaction) {

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

return Actioninvocation.invoke ();

}

Confirm if login exists in session

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

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

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

In the presence of the following operation.

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

return Actioninvocation.invoke ();

} else {

otherwise terminate the subsequent operation, return 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 blocked by login until all the action within the same package is executed.

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

 

Intercepting Device

name

Description

Alias Interceptor

Alias

Request parameters are converted between different requests in different name parts, and the request content is unchanged

Chaining Interceptor

Chain

The previous Action property 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 checkbox automatic Processing code to set the contents of unselected checkbox to False, and HTML does not commit unchecked checkbox by default.

Cookies Interceptor

Cookies

Use of Name,value to refer to cookies

Conversion Error Interceptor

Conversionerror

Adds an error from Actioncontext to the property field of the action.

Create Session Interceptor

CreateSession

Automatic creation of HttpSession to serve interceptors that require the use of httpsession.

Debugging Interceptor

Debugging

Provide different debugging pages to show the internal data situation.

Execute and Wait Interceptor

Execandwait

Performs an action in the background while bringing the user to a waiting page in the middle.

Exception Interceptor

exception

To position an exception to a screen

File Upload Interceptor

FileUpload

Provide file Upload function

I18N Interceptor

i18n

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.