Struts2 Interceptor (1)

Source: Internet
Author: User

Interceptor, as its name implies, intercepts an interceptor before it occurs and inserts a corresponding processing process. From this point of view, the interceptor is equivalent to the filter in the sevlert specification, but the struts2 interceptor is completely independent from the servlet filter, and the two have different implementations.

In AOP, the interceptor is used to intercept a method or field before it is accessed, and then insert some operations before or after it. This is a method implemented by AOP. What about AOP? The Chinese meaning is for aspect programming. Is an eliminationCodeA repeated method is a continuation of ideas. For example, if you need to record the logs of each method in the system, this logging method is distributed in multiple classes. In this case, the most specific method is to create a base class, put the log function in it, and let these classes implement this base class. Therefore, this requirement must be put forward in the early stage. Otherwise, we need to modify a lot of hash code in the system over the weekend. This will undoubtedly increase the chance of errors and increase the difficulty of system maintenance.

I believe there are manyProgramShows some behaviors that cannot be naturally related to a single program module or a closely related program module. Such as logging, processing of context-sensitive errors, performance optimization, and design patterns. These "cross-sectional nodes" span the responsibility boundaries of a given programming model. Because the cross-cutting behavior is scattered, programmers need to apply AOP, Which is why AOP is generated. At the same time, you must modularized the cross-cutting concerns. By dividing the aspect code, the cross-cutting concerns become easy to process. We can change, delete, or insert the system's aspect during compilation, or even reuse it.

In struts2, many action operations can be performed only after logon. The simple solution is to add logon judgment operations for each action, this can be done by code-based clothing pasting. If you need to modify it later, you only need to modify it in each action method. If an interceptor is defined, you can use the user name and password to make some judgments before entering the action, and then know what permissions the user has. This is the control of permissions, you can use an interceptor to control permissions. (To be continued)

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.