STRUTS2 request processing process and source code analysis

Source: Internet
Author: User
Tags config file upload

1.1 Struts2 Request Processing

1. A request in the STRUTS2 framework of the processing steps:

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

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

c) The request through the plug-in filter, such as: Sitemesh, etc filters;

d The request passes through the core filter filterdispatcher, executes the Dofilter method, in this method, asks the actionmapper to decide whether this request needs to invoke an action;

E If Actionmapper decides that an action needs to be invoked, Actionmapper returns a actionmapping instance (which stores the configuration information for the action) and creates a Actionproxy (action Proxy) object. Handing the request to the agent object to continue processing;

f) The Actionproxy object finds the action class that needs to be invoked according to the Actionmapping and Configuration Manager asking for the framework's configuration file;

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

h) The Actioninvocation instance is invoked using a naming pattern, which involves the invocation of the associated Interceptor (Intercepter) before and after the action is invoked;

I once the action has been 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 as a JSP or freemarker, or it may be another action chain;

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

K according to the result object information, generate user response information response, in the process of generating the response can be used in the Struts2 frame inherited tags, in the process will still be involved in actionmapper;

2. Struts2 Request processing diagram:

1.2 Struts2 request processing source Analysis

When a user sends a request to STRUTS2, the Filterdispatcher dofilter () method automatically invokes the Dofilter () method to process the request, as follows:

1. Create value stack object stack;

2. Create the action context object;

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

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

4. The Actionmapping object is obtained by 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 method to invoke). The relevant code is shown in the following illustration:

The above code, the activity diagram is as follows:

5. If the GetMapping () method returns a Actionmapping object that is null, Filterdispatcher considers the user request to be not an action, at which point Filterdispatcher first analyzes:

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

If the resource requested by the user does not start with/struts-either a. jsp file or an. html file, it continues to be routed through the filter chain until the requested resource is reached.

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

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

The above code, the activity diagram is as follows:

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

(a) Encapsulate related object information as a map (such as: HttpServletRequest, Http parameters, HttpServletResponse, HttpSession, ServletContext, Actionmapping, which is stored in the execution context map, returns the execution context map object Extramap;

(b) Get information such as the action namespace, name attribute, method attribute stored in the mapping object;

C Loading and parsing Struts2 configuration files, if no human configuration, the default in order to load Struts-default.xml, Struts-plugin.xml, Struts.xml, the action configuration, result configuration, Interceptor configuration, parse and save to config object, return file Configuration object config;

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

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

The above steps are related to the code, as shown in the figure:

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

9. Executes the Invocation.invoke () method, 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, did not see the interceptor recursive call, 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 interceptors must implement the Intercept method, and the parameter of the method is precisely actioninvocation, so if Invocation.invoke () is invoked in the Intercept method, will continue to find the next intercept execution from the Intercepor list of the action, calling the Intercepor recursively;

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

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

10. In the Invocation.invoke () method, the execution of the Interceptor, action, and ResultCode is completed, and the Preresultlistener collection is continued and the result object is generated. Implement the Preresultlistener interface, and do some custom processing before returning result, as shown in the figure

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

The above two ways, we can find that through the anonymous internal class to achieve, in fact, there is a way through the interceptor in the implementation of Preresultlistener interface, and the implementation of methods Beforeresult method, you can. As shown in the following illustration:

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

The above 1-11 steps, for the complete process analysis of the STRUTS2 processing request, the 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.