"Reprint" Struts Journey "1"

Source: Internet
Author: User

about Struts

Struts is an open source project sponsored by the Apache Software Foundation (ASF). It was originally a subproject in the Jakarta Project and became the top-level project for ASF in March 2004. Using javaservlet/jsp technology, it realizes the application framework of MVC design pattern based on Javaeeweb application and is a classic product in MVC Classic design pattern.

Struts Development History

Struts is part of the Apachejakarta project, and the founders of the project want to improve and improve javaserverpages, Servlets, tag libraries, and object-oriented technical standards through research on the project.

The name struts comes from supporting metal frames used in buildings and old-fashioned aircraft. The reason this framework is called "struts" is to remind us to remember the foundations underpinning our houses, buildings, bridges, and even our stilt feet. This is also an excellent description of the role struts plays in developing Web applications.

Struts means "pillars, branches," which are designed to reduce program development time, and project creators believe that the existence of Jsp,servlet can help users solve most problems, but because of their coding on the development of the project has brought a lot of inconvenience, reusability is also poor, So struts came into being to help users solve these problems in the shortest possible time. The Struts framework provides the following services:

(1) as a servlet for the controller.

(2) Provide a large number of tag libraries.

(3) Provide a framework for internationalization, using different profiles, can help users to choose the appropriate language.

(4) A JDBC implementation is provided to define the data source and database connection pool.

(5) XML parsing tool.

(6) file download mechanism.

Struts Principle

Struts is an implementation of the JSPMODEL2 design Standard, which describes the architecture and working principles of struts from the model, view, and Controller 3 sections, respectively. The invocation process is as follows (Figure 1.1).


Figure 1.1

(1) Views (view)

In struts, the view layer contains two parts, a JSP page, and a actionform.

Actionform encapsulates the user-submitted form information, in fact Actonform is essentially javabean, these javabean do not have business logic, only provide all the properties of getter and setter method, These properties correspond to the entries in the user form that are one by one. In struts, the user form information is submitted to the controller via Actionform.

JSP page is the main view component of classic MVC, which is mainly the function of information display and controller processing result display.

In addition to the above, struts also provides a powerful Struts tag library to help users resolve display logic and to use the Actonform component to pass information to the control layer.

(2) control (Controller)

In the control layer, struts provides a controller component Actionservlet, which inherits from HttpServlet, and overloads the HttpServlet doget (), DoPost () method, which can accept HTTP responses and forward, It also provides the ability to forward mapping (mappings) using XML.

(3) Model

Models represent the processing of state and business logic, and in general Web applications, use JavaBean or EJBS to implement the business logic of the system. In struts, struts provides the action object to manage calls to the business logic, helping the user separate the business logic, meaning that struts itself does not implement the business logic, but can invoke the completed business logic.

Struts Work Flow

The struts workflow is shown below (Figure 1.2).


Figure 1.2

Actionservlet is the core controller in struts, and all user requests must be handled by Actionservlet, The Struts-config.xml is the core configuration file in struts, where the user request URL and the Controller action mapping are configured in this file, and Actionservlet sends the user's request to the corresponding controller through this configuration file.

In the Struts Web application, when the Web application starts, Initializes the Actionservlet to load the Struts-config.xml configuration file when the Actionservlet is initialized, and the URL and controller mappings are stored in the Actionmapping object or other object when the load succeeds. When Actionservlet receives a user request, the user request is processed according to the following process.

(1) Actionservlet receives the user's request, will look for the matching actionmapping object according to the request URL, if the match fails, the user request URL path information is wrong, so the request path is returned invalid information, When the matching actionmapping is found, go to the next step.

(2) When Actionservlet finds a matching actionmapping object, it will determine the existence of the corresponding Actionform object according to the mapping information in the actionmapping. If the corresponding Actionform object does not exist, a new actionform correspondence is created, and the user-submitted form information is saved to the Actionform object.

(3) In Struts-config.xml, this configuration file allows you to configure whether the form requires validation, and if validation is required, call the Validate () method in Actionform to validate the form that is entered by the user.

(4) If the Actionform Validate () method returns the Actionerrors object, the validation fails, Actionservlet returns the page to the user input interface, prompting the user to re-enter. If the return value of the method is null, it indicates that the validation has passed and can proceed to the next step.

(5) Actionservlet can find the user request forwarded to which controller action according to the Actionmapping object, create the object if the corresponding action object does not exist, and invoke the action's Excute () method.

(6) The Execute () method of the business logic controller action Returns a Actionforward object, actionservlet forwards the result of the controller processing to the JSP page specified by the Actionforward object.

(7) The JSP page specified by the Actionforward object displays the results of the server processing to the user in the appropriate form based on the returned processing results, so far, the entire process of a customer request has been completed.


The above preliminary struts framework is introduced, and the principle of the simple analysis. As to how struts implements MVC, Actionservlet belongs to the controller part, the action and Actionform belong to the model layer, or the action belongs to the controller layer, Different people have different understandings of struts. Then the real application to practice, in the practice of profound experience, the principle is important, it is important to use, is able to harness and use the framework. It's like learning to drive, not overnight.

"Reprint" Struts Journey "1"

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.