Analysis and use of Struts2 interceptor

Source: Internet
Author: User

Analysis and use of Struts2 interceptor

I. Case study: AboutInterceptorWe certainly do not simply code. I will show you how to implement the interceptor by studying the source code and how to code struts2.

Ii. Case Analysis:

A) The implementation of the adapter will first implement the Interceptor interface, while in the implementation class, we will implement three methods: init (), destroy (), intercept (). The first two are not required for us. Of course, struts2 also uses the adapter mode to make the implementation class code clearer and easier to identify. Here, we will replace the Interceptor interface by inheriting the AbstractInterceptor abstract class. Therefore, we only need to override intercept. By viewing the AbstractInterceptor source code, we can find that it also implements the Interceptor interface, so in the future, the Interceptor only needs to inherit AbstractInterceptor.

B) The ActionInvocation class is the core of the interceptor.

By outputting invocation. getAction (). getClass (), we can find that both interceptors are associated with the same Action.

C) What is intercepted by the interceptor? The exact description should be that the interceptor intercepts the execute () method of the target action. To intercept custom methods, we need to: MethodFilterInterceptor ).

D) when viewing the source code, we will see a method: TextParseUtil. commaDelimitedStringToSet (...); this method intercepts the strings in the includeMethod and excludeMethod in the configuration file. In turn, we should separate the methods in the configuration file with commas (,), of course, English commas.

E) by viewing the document or the source code, we can find that intercept () exists in MethodFilterInterceptor, but we will not implement it in the Implementation class, but doIntercept (), and its role here. By analyzing the source code, we can know that it is used to determine whether the method in the configuration file needs to be intercepted. If this method is configured, it will be intercepted. If it is not configured, it will intercept p. Haha.

Iii. Experience:

A) Here we will mention the design pattern. The design pattern is a summary of the design experience for implementing code in some specific scenarios. There are about 23 types in Java. Of course, this is not to say that everyone is familiar with them. To be honest, I also know a few types of them now, and I have never made any practical application. You only need to know about it. After all, it will be good for its own encoding.

B) as long as we understand the filter, we can easily understand the Interceptor. The two are the same in terms of design principles, design patterns, and implementation methods. The difference is that the filter can filter everything, the interceptor can only intercept action, because we can only configure the interceptor on action.

C) when writing code, we need to develop the habit of writing comments. This is very important, not just to make it easier for us to view it, but also to make it easier for them to understand the code they have written. This is especially important in collaborative development. You can think about it. When you enter the company, if the company asks you to maintain a system, if the system code does not even have a comment, what do you think, or you can't help leaving, huh, huh, joke, just to let everyone know how important it is to develop the habit of writing comments. At the same time, another benefit of writing comments is generating documents. You can ask du Niang about the method and principle of generating the document. It is very simple. I will not open a blog to explain it here.

Iv. Reference projects: Because we mainly analyze the source code, the project only helps you understand and master the process. In this chapter, we will not provide a project demonstration, but there is nothing to demonstrate. Let's look at the source code and analyze the two interfaces of AbstractInterceptor and MethodFilterInterceptor.

5. happy every day:

If You Are the One, the female guest is nothing at all. You must know that Aunt su can destroy the boys' lights in the whole building!

The right to final interpretation belongs to partner aishang. Please indicate the source for reprinting.

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.