Struts2 request processing process and source code analysis

Source: Internet
Author: User

Source: http://my.oschina.net/xianggao/blog/75514

1.1 Struts2 request processing

1. Steps for processing a request in the Struts2 framework:

A) The client initializes a request pointing to the Servlet container;

B) According to the Web. in xml configuration, the request first passes through the ActionContextCleanUp filter, which is an optional filter. This filter is very helpful for integrating Struts2 with other frameworks (SiteMesh Plugin) and mainly cleans the ActionContext and Dispatcher of the current thread;

C) requests are filtered by plug-ins, such as SiteMesh and etc;

D) The request goes through the core filter FilterDispatcher and executes the doFilter method. In this method, ask ActionMapper to determine whether the request needs to call an Action;

E) If ActionMapper decides to call an Action, ActionMapper will return an ActionMapping instance (storing the configuration information of the Action) and create an ActionProxy object, send the request to the proxy object for further processing;

F) The ActionProxy object queries the Configuration file of the Framework Based on ActionMapping and Configuration Manager and finds the Action class to be called;

G) When an ActionProxy object is created, an ActionInvocation instance is created at the same time;

H) The ActionInvocation instance uses the naming mode for calling. Before and after the Action is called, the call of the relevant Interceptor (Intercepter) is involved;

(I) Once the Action is executed, the ActionInvocation instance is responsible for creating the Action according to the configuration in struts. xml and returning the Result. Result is usually a JSP or FreeMarker template to be expressed, or another Action chain;

J) If you want to do something before returning the Result, You can implement the PreResultListener interface. PreResultListener can be implemented in Interceptor or in Action;

K) generate user response information response based on the Result object information. During the response generation process, you can use the inherited tags in the Struts2 framework. In this process, ActionMapper will be involved again;

2. Struts2 request processing:

1.2 Struts2 request processing source code analysis

When a user sends a request to Struts2, The doFilter () method of FilterDispatcher is called automatically. The doFilter () method processes the request as follows:

1. Create a value stack object stack;

2. Create an Action context object;

3. Re-encapsulate the request. This encapsulation returns different objects based on the type of the request content:

If the value is multipart/form-data, an object of the MultiPartRequestWrapper type is returned. This object serves File Upload. Otherwise, a StrutsRequestWrapper type object is returned. MultiPartRequestWrapper is a subclass of StrutsRequestWrapper, both classes are implemented by the HttpServletRequest interface.

4. Obtain the ActionMapping object through actionMapper. getMapping (). The configuration information of the Action is stored in the ActionMapping object (Action configuration information: Action name, namespace, and method to be called ). Shows the related code:

The above code shows the activity diagram:

5. If the returned ActionMapping object of the getMapping () method is null, FilterDispatcher considers that the user request is not an Action. In this case, FilterDispatcher first analyzes:

If the request starts with "/struts", it is automatically located on the web. for the packages initialization parameter configured in the xml file, FilterDispatcher treats the file under the packages parameter value package as a static resource, that is, the file content is displayed directly on the page.

If the requested resource does not start with/struts-may be a. js file or a. html file, the filter chain is used for further transmission until the requested resource is reached.

6. If the getMapping () method returns a valid ActionMapping object, it is considered to be requesting an Action. The Dispatcher. serviceAction (request, response, servletContext, mapping) method is called.

The code for the above six steps is shown in:

The above code shows the activity diagram:

7. request to enter the dispatcher. serviceAction (request, response, servletContext, mapping) method:

A) encapsulate related object information into Map (for example, HttpServletRequest, Http parameters, HttpServletResponse, HttpSession, ServletContext, ActionMapping, and other object information), and coexist in the execution context Map, returns the execution context Map object extraMap;

B) Obtain information such as the action namespace, name attribute, and method attribute stored in the mapping object;

C) load and parse the Struts2 configuration file, if there is no human configuration, by default load struts-default.xml, struts-plugin.xml, struts. xml: parses and saves action configuration, result configuration, and interceptor configuration to the config object, and returns the config object of the file configuration;

D) Create a proxy object for user action based on the execution context Map, Action namespace, name attribute, and method attribute;

E) execute the proxy.exe cute () method of the actionproxy object and turn to the result;

The code for the above steps ,:

8. Execute the proxy.exe cute () method of the actionproxy object. The execution of this method actually calls the invocation. invoke () method, as shown in:

9. execute the invocation. invoke () method to implement recursive calling of the interceptor and execute () method for executing the Action. The code in the defaactionactioninvocation. invoke () method is shown in:

In the above Code, we do not see the recursive call of the Interceptor. In fact, whether or not the recursive call is controlled by the programmer is easy to implement:

A) First, let's take a look at the Interceptor interface definition:

B) All intercept interceptors must implement the intercept method, and the parameters of this method are exactly ActionInvocation. Therefore, if invocation is called in the intercept method. invoke () will continue to find the next interceptor in the Intercepor list of the Action and call Intercepor recursively;

LoggingInterceptor in Struts2, as shown in:

C) The screenshot of the interceptor's recursive call activity is as follows:

10. in invocation. in the invoke () method, after the interceptor and action are executed and the resultCode is obtained, the PreResultListener set is executed and the Result object is generated to implement the PreResultListener interface. Before the Result is returned, do some custom processing,

Before the Result is returned, you can use the PreResultListener to implement custom processing. There are two common methods: one is implemented in Interceptor, and the other is implemented in Action ,:

The above two methods can be found to be implemented through anonymous internal classes. In fact, there is also a way to implement the PreResultListener interface in the interceptor and implement the beforeResult method. As shown in:

11. Finally, the user response is completed by generating the Result;

Steps 1-11 above are the complete process for Struts2 to process requests. The relevant Code call process is shown in:

 

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.