The realization principle and source code analysis of Struts2 Interceptor

Source: Internet
Author: User
Tags abstract

This article is from: Cao Shenhuan blog column. Reprint Please specify source:http://blog.csdn.net/csh624366188

Interceptors (Interceptor) are one of the most powerful features of Struts2, and can be said to be the core of struts2, which allows you to do some processing before or after action and result are executed. At the same time, interceptors allow you to modularize generic Code and act as a reusable class. Many of the features in Struts2 are done by interceptors. Interception is an implementation strategy of AOP. The Chinese document in WebWork is interpreted as: Interceptors are objects that dynamically intercept action calls. It provides a mechanism to enable developers to define code that executes before and after an action executes, or to prevent it from executing before an action executes. It also provides a way to extract the reusable parts of an action. When it comes to interceptors, there's one more word you should know-the interceptor chain (Interceptor Chain, called the Interceptor stack interceptor stack in struts 2). The Interceptor chain is a chain that binds the interceptor in a certain order. When accessing an intercepted method or field, interceptors in the interceptor chain are called in the order in which they were previously defined.

A. The implementation principle of interceptors:

Most of the time, the Interceptor method is invoked in the form of proxies. The block implementation of Struts 2 is relatively straightforward. When the request arrives at the servletdispatcher of Struts 2, struts 2 looks for the configuration file, instantiates the relative interceptor object based on its configuration, and then strings it into a list, the last one to invoke the interceptor in the list. In fact, we are able to use interceptors so flexibly,fully attributed to the use of dynamic agents。 A dynamic agent is a proxy object that makes different processing based on the customer's needs. For the customer, just know a proxy object on the line. In that Struts2,How does an interceptor be called by a dynamic proxy?。 When the action request arrives, an action proxy object is generated by the agent of the system, which invokes the action's execute () or the specified method, and finds the interceptor corresponding to the action in Struts.xml. If there is a corresponding interceptor, these interceptors are called before the action's method executes, and if there is no corresponding interceptor, the action method is executed. The system's call to the Interceptor is implemented by Actioninvocation. The code is as follows:
[Java]View plain Copy if (Interceptors.hasnext ()) {Interceptor interceptor= (Interceptor) Interceptors.next ();   ResultCode = Interceptor.intercept (this);   } else {if (Proxy.getconfig (). Getmethodname () = null) {ResultCode = Getaction (). Execute ();   } else {ResultCode = Invokeaction (Getaction (), Proxy.getconfig ()); }   }


You can see that the action is not directly associated with the interceptor, but rather that the agent works with the interceptor in the organization action. The following figure:


two. Interceptor Execution Analysis

As we all know, there is nothing special about the interface definition of interceptor, except the Init and Destory methods, The Intercept method is the core method to implement the whole interceptor mechanism. And it relies on the parameter actioninvocation is the famous action dispatcher. Let's take a look at a typical interceptor abstract implementation class: [Java] view Plain Copy public abstract class Aroundinterceptor extends Abstractin Terceptor {

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.