Let's talk about the request process of the interceptor in struts2 (simulate the general process)

Source: Internet
Author: User

Let's talk about the request process of the interceptor in struts2 (simulate the general process)
This document is used as a learning note for the Beijing shangxue struts2 course.
What is an interceptor first? What can an interceptor do?
The interceptor, as its name implies, intercepts objects and then performs operations. Who does it intercept? It is natural to intercept action. What can be done? You want the action to be able to do anything before running it, and the action itself is "unaware" of being intercepted.
This article mainly analyzes the process of the interceptor and does not involve environment retrieval and initialization. If you are interested in this part, refer
Http://www.cnblogs.com/liuling/p/2013-8-10-01.html
Before entering the source code analysis stage, I will give you a small example to help you understand.

Since we are studying interceptor, we should first write an action and interceptor to try.

package inter;public class FindNameAction {public void execute(){System.out.println("find name!");}}

Action has only one execute if it does not inherit any interface.


package inter;public interface Interceptor {void intercept(ActionInvocation invocation);}package inter;public class FirstInterceptor implements Interceptor{@Overridepublic void intercept(ActionInvocation invocation) {// TODO Auto-generated method stubSystem.out.println(1);invocation.invoke();System.out.println(-1);}}package inter;public class SecondInterceptor implements Interceptor{@Overridepublic void intercept(ActionInvocation invocation) {// TODO Auto-generated method stubSystem.out.println(2);invocation.invoke();System.out.println(-2);}}
It is easy to define an interface to be implemented by the interceptor, and then there are two specific interceptors. If there is something hard to understand in the code, it should be ActionInvocaton. Come on.
ActionInvocation is placed in it. The action class we want to perform the final operation also has various interceptors.
package inter;import java.util.ArrayList;import java.util.List;public class ActionInvocation {List
 
   interceptors=new ArrayList
  
   ();FindNameAction action=new FindNameAction();int index=-1;public ActionInvocation() {// TODO Auto-generated constructor stubinterceptors.add(new FirstInterceptor());interceptors.add(new SecondInterceptor());}public void invoke(){index++;if (index<=interceptors.size()-1) interceptors.get(index).intercept(this);else action.execute();}}
  
 
As mentioned above, there are two variables in ActionInvocation. One with all the interceptors and the other with the final action.
We will analyze the invoke method.
If the interceptor in the "Interceptor stack" does not fully work, let it continue to be called. Otherwise, it will directly do the core-final action work.
The Tangle is here. In the interceptor interface defined above, the intercept parameter of the method is an ActionInvocation, in which we call the invoke method of ActionInvocation (in the System. out. println (-1/-2) Before !!), In invoke, we extract interceptor and call its intercept (the parameter is this, that is, ActionInvocation itself ).
Round-Robin !!!
Let's look at his results.
package inter;public class Test {public static void main(String[] args) {new ActionInvocation().invoke();}}

The test results are as follows:
1
2
Find name!
-2
-1
If you have another number after the find name, and you still have questions about whether-2-1 is not-1-2. We recommend that you draw pictures in person to clarify the call process.

Next, let's look at the source code.







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.