The cornerstone of Struts 2-Interceptor (Interceptor)

Source: Internet
Author: User
Tags aop

If you want to develop quickly, a simple and convenient web framework like Struts 2 is essential. We are using struts 1.2.8 to develop demos, and there is no El (expression language), resulting in countless pages that resemble "<%= ((Integer) request.getattribute (" xx "). Intvalue () %6%> "code. The trouble with the Struts 1.x form Bean is that some colleagues directly use Request.getparameter (String arg) and then introduce another kind of trouble. Such issues, such time-critical projects in the demo highlight the inability of struts 1.x to develop quickly. But there's no way, because a few of our senior employees in our project, in addition to struts 1.x, seem less interested in other web frameworks.

To get to the chase, Interceptor (the Interceptor) is a powerful tool for struts 2, and there are many features (feature) that are built on top of it, such as internationalization, converters, checksums, and so on.

What is a interceptor?

Interceptors, which are used in AOP (aspect-oriented programming) to intercept a method or field before it is accessed, and then add some action before or after. Interception is an implementation strategy for AOP.

The Chinese document in WebWork is interpreted as-the interceptor is the object that dynamically intercepts the action call. It provides a mechanism for developers to define code that executes before and after an action is executed, or to block execution of an action before it is executed. It also provides a way to extract the reusable parts of the action.

When it comes to interceptors, there's a word that everyone should know--the interceptor chain (Interceptor Chain, called the Interceptor stack interceptor stack in struts 2). The Interceptor chain is a chain that connects the interceptor in a certain order. When you access an intercepted method or field, the interceptor in the interceptor chain is invoked in the order in which it was previously defined.

Implementation principle

The interceptors for Struts 2 are relatively simple to implement. When the request arrives at the servletdispatcher of Struts 2, struts 2 looks for the configuration file and instantiates the relative interceptor object based on its configuration, then strings it into a list (list), and the last one invokes the interceptor in the list, as shown in Figure 1.

Figure 1 Interceptor call sequence diagram

Existing interceptors

Struts 2 has provided you with a rich and versatile, full-featured interceptor implementation. You can go to the Struts2-all-2.0.1.jar or Struts2-core-2.0.1.jar package struts-default.xml to view the configuration of the default interceptor and Interceptor chain.

Used in this article is the latest release of Struts 2 2.0.1. To download friends please click the following link:

Http://apache.justdn.org/struts/binaries/struts-2.0.1-all.zip

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.