Review struts2 interceptor

Source: Internet
Author: User

1. What is an interceptor:
Interceptor, used in AOP (Aspect-Oriented Programming) to intercept a method or field before it is accessed, and then add some operations before or after it. Interception is an implementation policy of AOP.

2. Struts2 default interceptor Stack:

 

Interceptor

Name

Description

Alias Interceptor

Alias

The request parameters are converted in different name parts between different requests, and the request content remains unchanged.

Chaining Interceptor

Chain

This allows the attribute of the previous Action to be accessed by the next Action. It is now used in combination with the result (<result type = "chain">) of the chain type.

Checkbox Interceptor

Checkbox

When the checkbox automatic processing code is added, the content of the unselected checkbox is set to false. By default, html does not submit the unselected checkbox.

Cookies Interceptor

Cookies

The configured name and value are used to indicate cookies.

Conversion Error Interceptor

ConversionError

Add the error from ActionContext to the Action attribute field.

Create Session Interceptor

CreateSession

Automatically create an HttpSession to serve the interceptor that needs to use HttpSession.

Debugging Interceptor

Debugging

Different debugging pages are provided to display internal data conditions.

Execute and Wait Interceptor

ExecAndWait

Execute the Action in the background and take the user to a waiting page in the middle.

Exception Interceptor

Exception

Locate the exception to a screen

File Upload Interceptor

FileUpload

Provides the file upload function.

I18n Interceptor

I18n

Record the selected locale

Logger Interceptor

Logger

Name of the output Action

Message Store Interceptor

Store

Stores or accesses messages, errors, and field errors in the Action class that implements the ValidationAware interface.

Model Driven Interceptor

Model-driven

If a class implements ModelDriven, place the result of getModel in the Value Stack.

Scoped Model Driven

Scoped-model-driven

If an Action implements ScopedModelDriven, the interceptor extracts the model from the corresponding Scope and calls the setModel method of Action to put it into the Action.

Parameters Interceptor

Params

Set the parameters in the request to Action.

Prepare Interceptor

Prepare

If Acton implements Preparable, the interceptor calls the prepare method of the Action class.

Scope Interceptor

Scope

A simple method to save the Action status to the session and application.

Servlet Config Interceptor

ServletConfig

Provides methods to access HttpServletRequest and HttpServletResponse, and uses Map.

Static Parameters Interceptor

StaticParams

From the struts. xml file, set the content in <action> <param> to the corresponding Action.

Roles Interceptor

Roles

Determine whether the user has a Role specified by JAAS. Otherwise, the Role is not executed.

Timer Interceptor

Timer

Time when the output Action is executed

Token Interceptor

Token

Use Token to avoid double-clicking

Token Session Interceptor

TokenSession

Similar to Token Interceptor, however, when double-clicking, the request data is stored in the Session.

Validation Interceptor

Validation

Validate submitted data using the content defined in the action-validation.xml file.

Workflow Interceptor

Workflow

Call the validate method of Action. If an error is returned, locate the INPUT screen again.

Parameter Filter Interceptor

N/

Remove unnecessary parameters from the parameter list

Profiling Interceptor

Profiling

Activate profile through Parameters

 

3. Implementation of custom Interceptor:
Although Struts 2 provides us with such rich interceptor implementations, it cannot meet our needs under certain circumstances. For example, when a user accesses an action, check whether the user has logged on. If the user has not logged on, the user will be intercepted before the action is executed. In this case, we need to customize the interceptor;

3. 1. Implement interceptor class

All the interceptors of Struts 2 directly or indirectly implement the com. opensymphony. xwork2.interceptor. Interceptor interface. This interface provides three

Methods:

1) void init (); after the interceptor is initialized, the system calls back this method before the interceptor is blocked. For each interceptor, this method is executed only once.

2) void destroy (); this method corresponds to the init () method. The system calls back this method before the interceptor instance is destroyed.

3) String intercept (ActionInvocation invocation) throws Exception; this method is the interception action that the user needs to implement. This method returns a string as the logical view.

 


In addition, the inherited class com. opensymphony. xwork2.interceptor. AbstractInterceptor is a simpler way to implement the interceptor class,

This class provides empty implementations of the init () and destroy () methods, so we only need to implement the intercept method.

 


. Register a custom interceptor

The custom interceptor class is implemented. Now we need to register this interceptor in struts;

3.2.1. register the interceptor and register the interceptor in the package in struts. xml.

<Interceptors>

<! -- Name: Interceptor name, class: class of the custom interceptor -->

<Interceptor name = "interceptor name" class = "Custom interceptor class path"/>

</Interceptors>
3.2.1. Use the interceptor and define the following code in the action that requires the custom Interceptor:
<Action>
<Interceptor-ref name = "interceptor name"/>

</Action>

Note:

Many functions of struts2 are implemented based on the Interceptor. If you only use a custom interceptor here, many core functions of struts2 will be lost; therefore, you need to define an interceptor stack (composed of one or more interceptors)

3.2.2. interceptor Stack

<Interceptor-stack name = "interceptor stack name">

<! -- Note that the default interceptor stack of the system should be placed in front, and a custom interceptor should be added; -->

<Interceptor-ref name = "defaultState"/>

<Interceptor-ref name = "name of the custom interceptor"/>

</Interceptor-stack>

3.2.3. Use stack in action
<Action>
<Interceptor-ref name = "stack name or interceptor name"/>

......


</Action>

3.2.3. If all actions need to use a custom interceptor, a default interceptor is defined.

<Default-interceptor-ref name = "permissionStack"/>

NOTE: If another interceptor is used in an action, the default interceptor will be invalid. To ensure that the default interceptor can be used, you need to add other interceptors, you can add other interceptors to the action.

3.2.3. Configuration File

<! DOCTYPE struts PUBLIC

"-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"

Http://struts.apache.org/dtds/struts-2.0.dtd>

<Struts>

<Package name = "my" extends = "struts-default">

<Interceptors>

<! -- Define the permission control interceptor -->

<Interceptor name = "permissions"

Class = "com. perm. interceptor. PermissionsInterceptor"/>

<! -- Define an interceptor stack containing permission control -->

<Interceptor-stack name = "c">

<Interceptor-ref name = "defaultStack"/>

<Interceptor-ref name = "permissions"/>

</Interceptor-stack>

</Interceptors>

<! -- Define the default interceptor -->

<Default-interceptor-ref name = "permissionsStack"/>

 

<! -- Define global processing result -->

<Global-results>

<! -- The result named error is mapped to the/message. jsp page. -->

<Result name = "error">/message. jsp </result>

</Global-results>

 

<Action name = "listall"

Class = "com. perm. action. UserAction"

Method = "listAllUser">

<Result name = "success">/listall. jsp </result>

</Action>

</Package>

</Struts>

 

Author: "wuchongchang's blog"
 

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.