Analysis and use of Struts2 interceptor

Source: Internet
Author: User

Analysis and use of Struts2 interceptor

I. Case study: The result page is successfully displayed by filling in the form submission on the loginUI. jsp page. view the console output and get a preliminary understanding of the interceptor workflow.

Ii. Case Analysis:

A) create an Interceptor (MyInterceptor. java) to implement the Interceptor interface.

After the creation is successful, three methods will be implemented: init (), destroy (), intercept ().

We mainly use the intercept () method to implement the function here.

Init () is automatically called when the server starts loading struts. xml.

Destroy () is called when the server is restarted.

B) interceptor created before struts. xml definition

The Interceptor is defined under the package, not in the action.

C) Reference interceptor in action

You only need to enter the name of the Interceptor to call, but after your interceptor, you need to call defaultStack.

D) when defining the interceptor in struts. xml, we pass the parameter. In the interceptor, we only need to define a member variable and add the setXXX () method. Struts2 will automatically set the value.

Iii. Experience:

A) For some conceptual things, you 'd better understand the process and principles, which will help you remember. At the same time, for beginners, whether it is java or other languages, do not stick to it. This learning method was created before the university to cope with the exam. Understanding language is the best way to learn.

B) the interceptor can be very important. But it is more important than struts2. Many functions of Struts2 are implemented by the interceptor. Compared with our developers, understanding and using are okay, and there are not many actual use cases. In my "aishang partner", it is only used in the permission module. So I will take you to view the source code about the Interceptor. After all, it is the core of struts2, and the source code is a must.

C) Let's talk about how to consider the problem. When you learn something, such as struts2, we should consider the problem using struts2. For example, in struts2, the value of action for the member variable is set to setXXX () method, so when we configure the interceptor parameter and get it in the interceptor, we should first think of getting it through the setXXX () method, instead of getting it through getInitParam () in the filter. Of course, this method is based on the understanding of the tool and the implementation principle of the tool, so it is really important to look at the source code.

4. Reference project: http://download.csdn.net/detail/u011638419/8198713

5. happy every day:

My girlfriend is a foodie.

Once she had a full stomach and touched her stomach and said, "instead of letting a man increase his stomach, it is better to increase his stomach ."

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.