Struts2 running flow and how it works

Source: Internet
Author: User
Tags tomcat tomcat server

Struts2 is a very good Web application framework that is elegant, powerful, and simple to use. It can be said that Struts2 is a very mature MVC architecture.

when we learn Struts2, it is better to learn its running process, the core concept, to be inspired, improve themselves, rather than learn how to use it.

first, the system architecture

Struts2 's official documentation comes with a STRUTS2 architecture diagram. From this picture can be very good to understand Struts2


about the key in the diagram:

Servlet Filters: The filter chain, where all requests from the client are processed by the filter chain. The core of Struts Core:struts2, but Struts2 has done it for us, and we don't need to do this interceptors,struts2 interceptor. STRUTS2 provides a number of default interceptors that can do most of the work of daily development, while our custom interceptors are used to implement the functionality required by the actual customer business. User Created, created by developers, includes Struts.xml, Action, and Template, which are mandatory for everyone who uses STRUTS2 to develop.


1.FilterDispatcher is the dispatch center of the entire Struts2, which is the C (Control Center) in MVC, based on the results of Actionmapper to decide whether to process the request, If actionmapper indicates that the URL should be Struts2 processed, it will perform action processing and stop the filter on the filter chain that has not yet been executed. 2.ActionMapper Determines whether the request should be Struts2, and if Struts2 processing is required, Actionmapper returns an object that describes the Actioninvocation information for the request. 3.ActionProxy, which creates a actioninvocation instance between action and Xwork, gives us a chance to introduce more implementations in the future, such as through WebService. 4.ConfigurationManager is the xwork configuration of the management center, you can think of it as struts.xml this configuration file in memory correspondence. 5.struts.xml is a place for developers to visit. Is the application configuration file of Stuts2, which is responsible for the configuration of mapping relationship between URL and action, and the result configuration of page jump after execution. 6.ActionInvocation: Actually invokes and executes the action, which has an action instance and an interceptor instance on which the action depends. The actioninvocation executes these interceptors, action, and corresponding result in the order specified. Interceptor (Interceptor): Is the cornerstone of STRUTS2, similar to the Javaweb filter, interceptors are stateless classes, interceptors can automatically intercept action, They provide developers with the opportunity to execute some functional code before the action runs or after the result runs. 7.Action: Used to process requests, encapsulate data.
second, the operation process
1. When a user makes a request, such as http:localhost:8080/struts2/helloworld/helloworldaction.action, the request will be received by Tomcat, The Tomcat server chooses the Web application that handles this request, which is the Web project by HelloWorld to handle the request. The 2.Web container reads the web. XML of the HelloWorld project, matches it in Web. XML, but finds that it is handled by the STRUTS2 filter (i.e.Strutsprepareandexecutefilter), depending on the configuration of the filter, locate the Filterdispatcher (STRUTS2 Dispatch center)3. Then get the Filterdispatcher instance and then callback the Dofilter method for real processing Ps:filterdispatcher is required to be configured for any STRUTS2 application, typically When there are other filters in the Web. xml file, the Filterdispatcher is placed at the end of the filter chain, and if a special filter such as Sitemesh appears before Filterdispatcher, Struts2 Actioncontex must be referenced before Sitemesh Tcleanup Filter
the architecture diagram for the corresponding Struts2 is as follows


4. Filterdispatcher then forwards the request to actionmapper. Actionmapper is responsible for identifying whether the current request requires STRUTS2 to be processed. Actionmapper is similar to the company's security, to identify whether the current customer is not my company's people
the architecture diagram for the corresponding Struts2 is as follows



5. If Struts2 processing is required, Actionmapper notifies filterdispatcher that the request needs to be processed, and Filterdispatcher stops the subsequent portion of the filter chain (which is why, Filterdispatcher should appear in the filter chain for the last reason). It then establishes an Actionproxy instance, which acts as the middle layer between the action and the Xwork, and proxies the action's running process.

the architecture diagram for the corresponding Struts2 is as follows



when the 6.ActionProxy object is created, it does not know which action to run, it has only the URL of the request that was received from Filterdispatcher. and really know which action to run is the ConfigurationManager. Because it's the only way to read our strtus.xml.
(When the server is started, ConfigurationManager will read all the information in the Struts.xml into memory, and cache, when actionproxy with the URL to ask him what action to run, you can directly match, Find and Answer)

the architecture diagram for the corresponding Struts2 is as follows

-

7.ActionProxy knows what to do (which action to run, the associated interceptor, and all the possible result information), and then immediately establishes the Actioninvocation object. The Actioninvocation object describes the entire process in which the action runs.

Note: The full call procedure for the action is handled by the Actioninvocation object
the architecture diagram for the corresponding Struts2 is as follows




8. Before the Execute method, it seems that the parameters in the URL request have been assigned to the Action property, which is our "Lei Feng"-Interceptor.

the operation of the interceptor is divided into two parts, one running before the action, the other running after the result, and the order is just the reverse. That is, in the Order before action execution, such as Interceptor 1, Interceptor 2, Interceptor 3, then run the result, once again run the interceptor, the order becomes the Interceptor 3, Interceptor 2, Interceptor 1.

This is like, you want to go to Grandma's house, need to through the water park Liangshan---spider web Cave, Somalia, to Grandma's home, see Grandma back when, you must through Somalia-spider web Cave, water park Liangshan.

Therefore, the Actioninvocation object executes in a number of complex procedures, in order to specify the interceptor sequence.


the architecture diagram for the corresponding Struts2 is as follows





9. Go to Grandma's house and execute the action method





10. Then according to the result returned by the Execute method (result), go to Struts.xml to select the next page




11. After finding the page based on the results (result), on the page (with many templates provided by Struts2), you can access the required data through the STRUTS2 tag library and generate the final page

Note: The client was not answered at this point, but the page was generated




12. Finally, Actioninvocation object reverse execution Interceptor, back from Grandma's house




after the completion of the 13.ActionInvocation object, the Response object (HttpServletResponse) has been obtained, and finally the response is shown to the client by filtering in the reverse order of the filter configuration definition.


get the full Struts2 architecture diagram



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.