STRUTS2 Basic Learning (v)-interceptors

Source: Internet
Author: User

I. Overview 1. First Knowledge Interceptor

Interceptor interceptors are similar to the previously learned filters, which are code that can be executed before and after the action, and are the techniques we often use for web development. For example: Permission control, log, and so on. We can also connect multiple interceptor together to form the interceptor stack.

Struts2 interceptors, each interceptor class has only one instance of the object, that is, in singleton mode, all actions that refer to this interceptor share an instance of this interceptor class, so if you use class variables in interceptors, be aware of the problem of synchronization.

Attention:

(1) Interceptors implement interception before or after accessing an action.

(2) Interceptors are pluggable and are an implementation of AOP.

(3) Some of the public processing code for the action can be placed in the interceptor, such as permission control, log, and so on.

(4) Interceptor Stack

A: The interceptor stack is a chain that connects the interceptors in a certain order.

B: When accessing the intercepted method or field, the interceptor ambulance in the interceptor chain is called in the order defined earlier.

(5) Principle of realization

Step one: The Struts2 Interceptor implementation principle is relatively simple, when requested STRUTS2 action, STRUTS2 will find the configuration file.

Step two: Struts2 the corresponding Interceptor object according to the configuration, and then string the interceptor into a list, the last one in the invocation list.

The interceptor adopts the responsibility chain design pattern, in the responsibility chain pattern, many objects are connected by each object's reference to its next-generation chain, each node of the chain of responsibility can continue to invoke the second node, or it can prevent the process from continuing execution.

2. The difference between interceptors and filters

(1) Interceptors are based on the Java reflection mechanism.

Filters are based on function callbacks.

(2) The Interceptor is part of the STRUTS2 framework and can only intercept the action and cannot intercept requests to the JSP.

Filters are part of the Web container and can filter all requests, including JSP, Servlet, Action, HTML, and so on.

(3) in the life cycle of the action, the interceptor can be called multiple times.

Filters can only be called once when the container is initialized.

Second, Interceptor interface

(1) Each interceptor is a Java class that implements the Com.opensymphony.xwork2.interceptor.Interceptor interface.

Public interface Interceptor extends Serializable {    void Destroy ();    void Init ();    String Intercept (actioninvocation invocation) throws Exception;}
Init: This method is called immediately after the Interceptor is created and is called only once during the life cycle of the interceptor.

Destroy: This method is called before the Interceptor is destroyed, and it is called only once during the life cycle of the interceptor.

Intercept: This method will be called once for each request action that is intercepted.

(2) Struts invokes the Interecept method of each interceptor registered for an Action in turn.

(3) Each time the Interecept method is called, Struts passes an instance of the Actioninvocation interface.

(4) Actioninvocation: Represents the execution state of a given action, and the interceptor can obtain the action object and the Result object associated with the action from the object of the class. After completing the interceptor's own task, the interceptor advances to the next part of the Action processing process by invoking the Invoke method of the Actioninvocation object.

(5) You can also call the Addpreresultlistener method of the Actioninvocation object to "hang" one or more Preresultlistener listeners for the Actioninvocation object. The listener object can do something before the action is finished and before the action result is executed.

(6) The Abstractinterceptor class implements the Interceptor interface. And for Init, destroy provides a blank implementation.

(7) Struts2 comes with an interceptor

Iii. custom Interceptors 1. Writing interceptors

The Interceptor interface needs to be implemented to implement the three methods in the interface, but the Interceptor interface has many implementation classes, and the simplest way to write is to inherit the Abstractinterceptor class.

public class Testinterceptor extends abstractinterceptor{@Overridepublic String intercept (actioninvocation invocation ) throws Exception{system.out.println ("Interceptor execution 1 ....."); Invocation.invoke (); System.out.println ("Interceptor execution 2 ...."); return null;}}

2. Configuring interceptors

The interceptor configuration is required in the struts.xml.

<package name= "user" extends= "struts-default" ><!--configuring interceptors--><interceptors><interceptor name= " Testinterceptor "class=" Com.kiwi.action.TestInterceptor "></interceptor></interceptors><action Name= "Interceptaction" class= "com.kiwi.action.InterceptorTestAction" method= "save" ><!--reference interceptors-->< Interceptor-ref name= "Testinterceptor"/><!--reference the default interceptor stack--><interceptor-ref name= "Defaultstack"/> <result name= "Success" >/success.jsp</result></action></package>

Attention:

(1) If the custom interceptor is referenced, the default interceptor will not work.

(2) Default Interceptor in Struts-default.xml, the default interceptor is configured, and when the default interceptor is configured, the default interceptor will work if the interceptor is not referenced.

(3) When the action interceptor is relatively long, multiple interceptors can be placed in an interceptor stack.

<package name= "user" extends= "struts-default" ><!--configuring interceptors--><interceptors><interceptor name= " Testinterceptor "class=" com.kiwi.action.TestInterceptor "></interceptor><!--Configure an interceptor stack that can contain references to multiple interceptors-- ><interceptor-stack name= "Mystack" ><interceptor-ref name= "Testinterceptor"/><interceptor-ref Name= "Defaultstack"/></interceptor-stack></interceptors><action name= "interceptAction" class= " Com.kiwi.action.InterceptorTestAction "method=" save "><!--reference Interceptor Stack--><interceptor-ref name=" Mystack "/ ><result name= "Success" >/success.jsp</result></action></package>

STRUTS2 Basic Learning (v)-interceptors

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.