9. struts2 interceptor

Source: Internet
Author: User
Document directory
  • 1. Configure the custom interceptor and interceptor Stack
  • 3. Method Filtering
1. Introduction to interceptor

The Interceptor is the most important component of struts2, because most operations of struts2 are completed by him. The following lists some built-in interceptors:

(1) Params: intercept HTTP Request Parameters and completeType conversion;

(2) fileupload:File UploadInterceptor;

(3) conversionerror: the interceptor that handles type conversion errors;

(4) exception: the interceptor that handles exceptions. Therefore, you can throw exceptions in the execute method with peace of mind;

(5) i18n: handling international interceptors;

(6) Static-Params: used to inject action attribute values in struts. xml;

(7) validation:Input ValidationInterceptor;

(8) workflow: ProcessingManual verificationInterceptor;

The built-in interceptors are defined in the struts2-core-Xxx.jar \ struts-default.xml, so you need to enter extends = "struts-Default" when defining <package> so that these interceptors can be automatically inherited;

However, we can also define our own Interceptor. However, if we configure a custom interceptor, the built-in interceptor will disappear. Therefore, we must explicitly reference the built-in interceptor, <interceptor name = "defaultstack"/>;

Struts2 passedPluggable DesignAllows you to add or delete any interceptor, which is flexible;

Interceptor & interceptor Stack

The interceptor stack is composed of multiple interceptors. When using the stack, you can use it as a "larger Interceptor". Apart from different definitions, the stack is used in the same way;

Interceptor stack application: convenient management when multiple interceptors are used at the same time;

Interceptor Stack

Interceptor 1

Interceptor 2

Interceptor 3

 

Use of the Interceptor: the interceptor is configured in <action> to configure one or more interceptors for an action. That is, the interceptor intercepts the action and executes the interceptor method before executing the action method;

Ii. Custom interceptor

 

1. Configure the custom interceptor and interceptor Stack

 

Define the root element of the interceptor or interceptor stack as <interceptors>, and this element is a child element of <package>;


(1)Define interceptor: Sub-element of <interceptors>

<Interceptor name = "Interceptor name" class = "implementation class"/>

Note: If you need to input a parameter, it indicates the default value of the interceptor class attribute;

<Interceptor name = "Interceptor name" class = "implementation class"> <Param name = "name"> value </param> </interceptor>

(2)Reference interceptor: <Action> or <Interceptor-stack> child element

If it is a child element of <action>, an interceptor is configured in the action;

<Interceptor-ref name = "Interceptor name"/>

Note: If a parameter needs to be passed in, it indicates the class attribute value when the interceptor is used, and the parameter passed in when the interceptor is defined will be overwritten;

<Interceptor-ref name = "Interceptor name"> <Param name = "name"> value </param> </Interceptor-ref>

(3)Define interceptor Stack: Sub-element of <interceptors>

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

<Interceptor-ref name = "Interceptor name"/>

<Interceptor-ref name = "Interceptor name"/>

</Interceptor-stack>

(4)Reference interceptor Stack: Sub-element of <action>

<Interceptor-ref name = "Interceptor stack name"/>

Note: The parameter passing method here is special. Because the interceptor Stack has multiple interceptors, you must execute the parameters as follows:

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

<Param name = "interceptorname. attrname"> value </param>

</Interceptor-ref>

(5)Define default interceptor: Sub-element of <package>

<Default-interceptor-ref name = "Interceptor or interceptor stack name"/>

Note: parameters can be input here:

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

<Param name = "name"> value </param>

</Default-interceptor-ref>

Default interceptor usage time: When the interceptor is not explicitly referenced in the action, the default interceptor is used;

 

2. Define the interceptor class

Implement the interceptor interface and implementation method:

(1) Public void Init (); // initialization, usually empty

(2) Public void destroy (); // destroy, usually empty

(3) Public String Interceptor (actioninvocation Invocation) throws exception; // implement the interception function. The returned value is the return value of the execute method;

String result = invocation. Invoke () can call the execute () method of the Action intercepted by the interceptor and return to the logic view;
Public String Interceptor (actioninvocation Invocation) throws exception {

// Before calling the execute () method

String result = invocation. Invoke ();

// After the execute () method is called

Return result;

}

Note:

(1) When using a custom interceptor, the default built-in interceptor is lost. Therefore, you must explicitly reference the default interceptor Stack: <Interceptor-ref name = "defaultstack"/>

(2) If two interceptors are configured in an action, if interceptor A is referenced before interceptor B, that is:

<Action>

<Interceptor-ref name = "A"/>

<Interceptor-ref name = "B"/>

</Action>

The invocation is called in both interceptors. invoke (), but only the action method is executed once; before the action method is executed, interceptor A is executed first than interceptor B; after the action method is executed, interceptor B is executed first than interceptor;

The result is:

A interceptor statement

B interceptor statement

Execute

B interceptor statement

A interceptor statement

Code example:

Helloaction. Java

package org.xiazdong.action;import com.opensymphony.xwork2.ActionSupport;public class HelloAction extends ActionSupport {public String execute()throws Exception{System.out.println("execute...");return SUCCESS;}}

Interceptor01.java

Package Org. xiazdong. interceptor; import COM. opensymphony. xwork2.actioninvocation; import COM. opensymphony. xwork2.interceptor. interceptor; public class interceptor01 implements interceptor {private string name; Public String getname () {return name;} public void setname (string name) {This. name = Name ;}@ overridepublic void destroy () {}@ overridepublic void Init () {}@ overridepublic string intercept (actioninvocation Invocation) throws exception {system. out. println ("name attribute:" + name); system. out. println ("before the execute method... "); string result = invocation. invoke (); system. out. println ("after the execute method... "); return result ;}}

Struts. xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.devMode" value="true" /><package name="default" namespace="/" extends="struts-default"><interceptors><interceptor name="interceptor01" class="org.xiazdong.interceptor.Interceptor01"><param name="name">xiazdong</param></interceptor><interceptor-stack name="stack"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="interceptor01"></interceptor-ref></interceptor-stack></interceptors><action name="hello" class="org.xiazdong.action.HelloAction"><interceptor-ref name="stack"></interceptor-ref><result>/index.jsp</result></action></package></struts>

3. Method Filtering

By default, the custom interceptor intercepts all action methods. For example, if the login () method and regist () method are defined in the action, two methods are intercepted by default, if you only want to intercept the login () method, perform the following steps:

(1) inherit the methodfilterinterceptor class;

(2) override Public String dointercept (actioninvocation Invocation) throws exception. This method and intercept method have the same functions, but their names are different;

(3) Configure parameters in struts. XML for filtering: in the sub-element of <Interceptor-ref>, set:

<Param name = "excludemethods"> method 1 that is not filtered, method 2 that is not filtered </param>, and method to be filtered </Param name = "includemethods"> </param>

Note: If a method is listed in the includemethods and excludemethods parameters at the same time, it will still be blocked;


Code example: if the login () and regist () methods are provided in the action, the following configuration in struts. XML can be used to prevent login () from being intercepted by the Interceptor: struts. xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.devMode" value="true" /><package name="default" namespace="/" extends="struts-default"><interceptors><interceptor name="interceptor01"class="org.xiazdong.interceptor.Interceptor01"><param name="name">xiazdong</param></interceptor><interceptor-stack name="stack"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="interceptor01"><param name="excludeMethods">login</param></interceptor-ref></interceptor-stack></interceptors><action name="hello" class="org.xiazdong.action.HelloAction"><interceptor-ref name="stack"></interceptor-ref><result>/index.jsp</result></action></package></struts>
Supplement: The preresultlistener application also introduced this class in the introduction of action, which is called after the action method is executed and before being transferred to the physical view;
This class can also be added to the interceptor class, so as long as the action of the interceptor is applied, the steps are as follows:
(1) define mypreresultlistener:
Public class mypreresultlistener implements preresultlistener {
Public void beforeresult (actioninvocation invocation, string resultcode );
} (2) Add invocation. addpreresultlistener (New mypreresultlistener () in the intercept method ());
You can;
Code:
Mypreresultlistener. Java
package org.xiazdong.interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.PreResultListener;public class MyPreResultListener implements PreResultListener {@Overridepublic void beforeResult(ActionInvocation arg0, String arg1) {System.out.println("beforeResult...");}}

Interceptor01.java

Package Org. xiazdong. interceptor; import COM. opensymphony. xwork2.actioninvocation; import COM. opensymphony. xwork2.interceptor. interceptor; import COM. opensymphony. xwork2.interceptor. methodfilterinterceptor; public class interceptor01 extends methodfilterinterceptor {@ overridepublic string dointercept (actioninvocation Invocation) throws exception {invocation. addpreresultlistener (New mypreresultlistener (); system. out. println ("before the execute1 method... "); string result = invocation. invoke (); system. out. println ("after execute1 method... "); return result ;}}

The result is as follows:

Before the execute1 method...
Regist...
Beforeresult...
After the execute1 method...

It can be seen that this method triggers the preresultlistener after the action method is executed, which is executed first than the interceptor;


The last part is the end of the image:

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.