Struts2 Work flow chart, principle and source code analysis---Perfect

Source: Internet
Author: User
Tags control label
Struts2 Working principle STRUTS2 Request Response Process:

In STRUTS2 applications, a number of components such as Controller, Actionproxy, actionmapping, Configuration Manager, and so on, are included in the process of returning the corresponding response to the server from the user request to the client. Actioninvocation, Inerceptor, Action, result and so on. Let's take a look at how these components relate to each other and how they work together.



(1) clients (client) send a request to the action (request)

(2) container The Web.xml mapping request and obtains the name of the controller (Controller)

(3) the container (Container) calls the controller (Strutsprepareandexecutefilter or Filterdispatcher). Call Strutsprepareandexecutefilter after Struts2.1 before calling filterdispatcher,struts2.1

(4) the controller (Controller) obtains the action information through the Actionmapper

(5) Controller (Controller) call Actionproxy

(6) Actionproxy reads Struts.xml files to obtain information about action and interceptor stack.

(7) Actionproxy requests the request pass to Actioninvocation

(8) Actioninvocation call action and interceptor in turn

(9) Generating result according to the configuration information of the action

(ten) result information returned to Actioninvocation

(11) Generating a httpservletresponse response

(12) The resulting response behavior is sent to the customer service side.




# # #阐述struts2的执行流程.

The Struts 2 framework itself can be roughly divided into 3 parts: Core controller Filterdispatcher, business controller action, and user-implemented enterprise business logic components.

The core controller Filterdispatcher is the foundation of the Struts 2 framework, which includes the control process and processing mechanism within the framework. The Business controller action and business logic components are required to be implemented by the user themselves. While developing action and business logic components, users also need to write related configuration files for use by the core controller filterdispatcher. The Struts 2 workflow is simple compared to struts 1 and is essentially the same as the webwork framework, so Struts 2 is an upgraded version of WebWork.

The basic summary process is as follows:

1, the client browser issued an HTTP request.

2, according to the Web.xml configuration, the request is filterdispatcher receive.

3. According to the Struts.xml configuration, find the action class and method that needs to be invoked, and inject the value into Aciton through the IOC way.

4. The action calls the business logic component to handle the business logic, which includes form validation.

5, action execution completed, according to the configuration in Struts.xml find the corresponding return results result, and jump to the appropriate page.

6, return HTTP response to the client browser.

Working principle

The process in the STRUTS2 framework is probably divided into the following steps

1 Client Initializes a request to a servlet container (for example, Tomcat)

2 This request passes through a series of filters (filters that have an optional filter called Actioncontextcleanup, which is useful for STRUTS2 integration with other frameworks such as: Sitemesh Plugin)

3 then Filterdispatcher is called and Filterdispatcher asks Actionmapper to decide whether or not you need to call an action

4 if Actionmapper decides to call a action,filterdispatcher to give actionproxy the processing of the request

5 actionproxy through Configuration Manager to ask the framework's configuration file to find the action class that needs to be invoked

6 Actionproxy Create an instance of Actioninvocation.

7 Actioninvocation instances are invoked using the naming pattern, which involves calls to the associated interceptor (Intercepter) before and after the action is invoked.

8 Once the action is completed, Actioninvocation is responsible for finding the corresponding return result according to the configuration in Struts.xml. The return result is usually (but not always, or perhaps another action chain) a JSP or freemarker template that needs to be represented. You can use the label inherited in the Struts2 frame during the presentation. In this process needs to involve actionmapper

Work flow

1, the client browser issued an HTTP request.

2, according to the Web.xml configuration, the request is filterdispatcher receive

3. According to the Struts.xml configuration, find the action class and method that needs to be invoked, and inject the value into Aciton via IOC way.

4. The action calls the business logic component to handle the business logic, which includes form validation.

5, action execution completed, according to the configuration in Struts.xml find the corresponding return results result, and jump to the corresponding page

6. Return HTTP response to client browser

See the user's Struts2 principle summary, I also summed up for follow-up interview, the following is my question

1, customer service to launch a request, through the HTTP protocol point to the Tomcat container, Tomcat got the request what she did.

2, we web.xml configuration

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

We can see Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter from the Web configuration file, what's the use of this filter class.

This/* is to intercept all requests and he intercepts the request for processing. <url-pattern>/*</url-pattern>

3. Our Struts.xml Configuration

<struts>

<package name= "Default" namespace= "/" extends= "Struts-default" >

<action name= "Hello" >

<result>

/hello.jsp

</result>

</action>

</package>

</struts>

Who is responsible for finding the action classes and methods that need to be invoked according to the Struts.xml configuration?

4. Finally, according to the configuration in Struts.xml, find the corresponding return result, in the return HTTP response to the client browser

In fact, with the simplest seven steps:

1: The client enters a URL address in the browser.

2: This URL request is sent to Tomcat via the HTTP protocol.

3:tomcat according to the URL to find the corresponding items within the Web.xml file.

4: In the Web.xml will find a struts2 configuration.

5: The struts2 corresponding struts.xml configuration file will then be found.

6: Resolve the Struts.xml configuration file according to the URL will find the corresponding class.

7: Call finished class returns a string that returns to the corresponding JSP according to Struts.xml.

Struts2 Label Categories:

1. The Form UI label

2. Non-form UI label

3. Control label

4. Data labels

Why use Struts2.

1. Open source

2.MVC Frame

3. Pure Pojo Action

4. Better Labeling Characteristics

5. Easy Test

6. Easy to expand



Source analysis, the formation of the sequence diagram, to help understand the source code:




Relatively good documentation: http://pan.baidu.com/disk/home#path=%252F%25E7%2594%25B5%25E5%25AD%2590%25E4%25B9%25A6%252F%25E6%258A% 2580%25e6%259c%25af%25e6%2596%2587%25e6%25a1%25a3

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.