Struts2 Learning (3)-interceptor

Source: Internet
Author: User

1. Interceptor: the interceptor is the core of struts2, and all functions of struts2 are implemented through the interceptor.

2. interceptor Configuration:

1) Write a class that implements the interceptor interface.

 

void destroy()
Called to let an interceptor clean up any resources it has allocated.
void init()
Called after an interceptor is created, but before any requests are processed usingintercept, Giving the interceptor a chance to initialize any needed resources.
String intercept(ActionInvocation invocation)
Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the request byActionInvocationOr to short-circuit the Processing
And just return a string return code.

The class that implements this interface is a singleton. It is created when the server is started and the init () method is called.

2) define the interceptor in the Struts. xml file.

Note: After a custom interceptor is defined, the default interceptor will become invalid. Must be manually added. And put it at the end of the interceptor.

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

3) Use interceptor in action

Note: The interceptor can only intercept action in action, and the filter can intercept all action.

 

3. When implementing the interceptor interface, whether or not the destroy and init methods are used, there must be an empty implementation. To this end, struts2 provides
Abstractinterceptor abstract class, which implements interceptor and implements empty implement for init and destroy methods.

4. execution sequence of the interceptor. See the following example:

The project structure is as follows:

The login. JSP code is as follows:

<Form action = "loginaction. Action" method = "Post"> <input type = "Submit" value = "Submit"> </form>

The Web. XML Code is as follows:

  <filter>    <filter-name>StrutsPrepareAndExecuteFilter</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>StrutsPrepareAndExecuteFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>

The code in struts. XML is as follows:

<struts><package name="struts2" extends="struts-default"><interceptors><interceptor name="interceptorTest01" class="com.interceptor.inteceptors.InterceptorTest01"></interceptor></interceptors><action name="loginAction" class="com.interceptor.action.LoginAction"><result name="success">/success.jsp</result><interceptor-ref name="interceptorTest01"><param name="name">test01</param></interceptor-ref><interceptor-ref name="interceptorTest01"><param name="name">test02</param></interceptor-ref><interceptor-ref name="interceptorTest01"><param name="name">test03</param></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></action></package></struts>

The code in interceptortest01.java is as follows:

package com.interceptor.inteceptors;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public class InterceptorTest01 implements Interceptor{private String name;@Overridepublic void destroy() {}@Overridepublic void init() {System.out.println("InterceptorTest01 init method invoked...");}@Overridepublic String intercept(ActionInvocation arg0) throws Exception {System.out.println("intercept method start...");System.out.println(name);String result = arg0.invoke();System.out.println("intercept method end...");return result;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

The code in loginaction. Java is as follows:

public class LoginAction extends ActionSupport{@Overridepublic String execute() throws Exception {System.out.println("loginAction invoked...");return SUCCESS;}}

When the server is started, it will print:

Click the submit button on the page to print it as follows:

If it is before the interception method, the configured interceptor takes effect for the user's request first. If it is after the interception method, the interceptor configured later takes effect for the user's request first.

5. Method Interceptor: intercept the specified method. You must inherit the abstract methodfilterinterceptor class, which inherits the abstract abstractinterceptor class. Intercept refers to whether the methods in the interceptor are executed.

The configuration in struts is as follows:

<Interceptor-ref name = "theinterceptor">

<Param name = "includemethods"> method name in the action to be blocked </param>

<Param name = "excludemethods"> method names that do not need to be blocked. Separate multiple methods with commas </param>

</Interceptor-ref>

 

 

 

 

 

 

 

 

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.