STRUTS2 request processing process and source code analysis

Source: Internet
Author: User
Tags config file upload

1.1 Struts2 Request Processing

1. Processing steps for a request in the STRUTS2 framework:

A) The client initializes a request to the servlet container;

b) According to the Web. XML configuration, the request is first passed through the Actioncontextcleanup filter, which is an optional filter that is useful for integration of Struts2 and other frameworks (Sitemesh Plugin), Mainly cleans up the current thread's actioncontext and dispatcher;

c) Request through the plugin filter, such as: Sitemesh, etc filter;

d) Request through the core filter filterdispatcher, execute the Dofilter method, in which the query actionmapper to determine whether the request needs to invoke an action;

e) If Actionmapper decides that an action needs to be called, Actionmapper returns a actionmapping instance (storing the configuration information for the action) and creates the Actionproxy (action Proxy) object. Submit the request to the proxy object for further processing;

f) The Actionproxy object locates the action class that needs to be called according to the configuration file of Actionmapping and Config manager asking for the framework;

g) When the Actionproxy object is created, an instance of Actioninvocation is created at the same time;

h) The Actioninvocation instance is invoked using a naming pattern that involves the invocation of the relevant interceptor (Intercepter) before and after invoking the action.

i) Once the action is executed, the Actioninvocation instance is responsible for creating and returning result based on the configuration in Struts.xml. Result is usually a template that needs to be represented by a JSP or freemarker, or it may be another action chain;

j) If you want to do something before returning to result, you can implement the Preresultlistener interface, Preresultlistener can be implemented in interceptor, or it can be implemented in action;

K) based on the result object information, generate user response information response, in the build response process can use the STRUTS2 framework inherited tags, in the process will still be involved in actionmapper;

2. Struts2 Request processing diagram:

1.2 Struts2 request processing source Analysis

When the user sends a request to STRUTS2, the Filterdispatcher DoFilter () method is called automatically, and the DoFilter () method processes the request process as follows:

1. Create a Stack object with value stacks;

2. Create an action context object;

3. To re-encapsulate the request, this package returns different objects depending on the type of content requested:

If the Multipart/form-data type, returns an object of type Multipartrequestwrapper that serves the file upload, otherwise returns an object of type Strutsrequestwrapper, Multipartrequestwrapper is a subclass of Strutsrequestwrapper, and these two classes are implementations of the HttpServletRequest interface.

4. The Actionmapping object is obtained through actionmapper.getmapping () and the configuration information for the action is stored in the Actionmapping object (the configuration information for the action: the name of the action, Namespace and the method to invoke). The relevant code is shown in the following figure:

The above code, the activity diagram is as follows:

5. If the GetMapping () method returns the Actionmapping object as NULL, Filterdispatcher considers the user request not to be an action, at which point the Filterdispatcher is parsed first:

If the request starts with/struts, the packages initialization parameter configured in the Web. xml file is automatically found, and Filterdispatcher treats the file under the Packages parameter value package as a static resource, that is, the contents of the file are displayed directly on the page.

If a user requests a resource that does not start with/struts-probably a. jsp file, or an. html file, it continues to pass through the filter chain until it reaches the requested resource.

6. If the GetMapping () method returns a valid Actionmapping object, it is considered to be requesting an action and will call Dispatcher.serviceaction (request, Response, ServletContext, mapping) method.

Above six steps, the relevant code is shown in the following figure:

The above code, the activity diagram is as follows:

7. Request to enter the Dispatcher.serviceaction (request,response,servletcontext,mapping) method:

A) Encapsulate related object information as map (e.g. HttpServletRequest, Http parameters, HttpServletResponse, HttpSession, ServletContext, Actionmapping and other object information), and deposited into the execution context map, returns the execution context map object Extramap;

b) Get information about the action namespace, Name property, method property, and so on, stored in the mapping object;

c) Load and parse the STRUTS2 configuration file, and if there is no human configuration, the default is to load Struts-default.xml, Struts-plugin.xml, struts.xml in order, the action configuration, the result configuration, Interceptor configuration, parse and deposit into Config object, return file Config object config;

d) Create a proxy object for the user action based on the execution context map, the action namespace, the Name property, the method property, and so on;

E) Execute the Action proxy object Proxy.execute () method and turn to the result;

The relevant code for the above steps, as shown in the figure:

8. Execute the Action proxy object Proxy.execute () method, which in fact calls the Invocation.invoke () method, as shown in the following figure:

9. Execute the Invocation.invoke () method, which implements the recursive invocation of the Intercept and executes the Execute () method of the action, the Code in the Defaultactioninvocation.invoke () method, as shown in the following figure:

In the above code, do not see the recursive invocation of the interceptor, in fact, whether recursive call, is controlled by the programmer, recursive call implementation is very simple:

A) First look at the Interceptor interface definition:

b) All the Intercept must implement the Intercept method, and the parameter of the method is exactly actioninvocation, so if Invocation.invoke () is called in The Intercept method, will continue to find the next intercept execution from the Intercepor list of the action, recursively calling Intercepor;

The log Interceptor Logginginterceptor in Struts2, as shown in the following figure:

c) The interceptor recursively invokes the activity diagram as follows:

10. In the Invocation.invoke () method, after executing the interceptor, action, and obtaining ResultCode, the Preresultlistener collection continues to be executed and the result object is generated. Implement the Preresultlistener interface, you can do some custom processing before returning to result, as shown in the figure

There are two common ways to implement custom processing through Preresultlistener before returning result: one implemented in interceptor and one in action, as shown in the figure:

The above two ways, we can find all through the anonymous inner class implementation, in fact, there is a way to implement the Preresultlistener interface in the interceptor, and implement the method Beforeresult method, can be. As shown in the following illustration:

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

Above 1-11 steps, the complete process analysis for STRUTS2 processing request, its related code calls the process, as shown in the following figure:

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.