The early "simple" development early, it is only a pure HTML page and browser effects. Since there is no new data operation and commercial application, there is also a lot of trouble. But such "simple" is just a cloud smoke, and now we have to use many technologies for complex Web-based commercial applications.
This article will show how to use Struts for application development processes. Struts is an open source framework for developing an Model-Viewing-Controller (Controller) (MVC) mode application architecture is a very useful technology that uses Java Servlet and JSP to build web applications.
Reading this article Requires readers with the following development experience: JSP, servlet, custom label library (Custom Tag Library) and XML. If the reader wants to make up a self-defined label library, you can refer to the author's previous article on this. This article is also to describe how to use the upper part of the Struts series, this series is temporarily divided into two parts.
Novice on the road consideration
Struts is an MVC framework based on Sun J2EE platform, mainly to be implemented by servlet and JSP technology. Its initial sprouting in Craig Mcclanahan's conception, it is still a year in the past year. Now, Struts is part of the Jakarta project group of the Apache Software Foundation, and its official website is http://jakarta.apache.org/struts. Since Struts can fully meet the needs of application development, it is easy to use, agility and rapid, and is very concerned in the past year. Struts integrates servlet, jsp, custom labels, and information resources into a unified framework. Developers do not have to encode their own MVC mode when developers using them, and make time saving time, so Struts It is a very good application framework.
The current Struts 1.0 revision includes a complete document, which can be said to be a user document is a development instruction document. If the reader is a JSP newer, or if the MVC design mode is not too familiar, it may be slower when I first go up, but I don't have to worry, I believe I will catch up with you as soon as possible :)
In addition, it should be noted that although the current Struts is just a version 1.0, it is already quite stable, the author applies from Struts 0.9 in a large-scale project (recently upgraded to version 1.0), has not encountered any trouble problem. In fact, Struts has greatly shortened development time to our team in this project to develop complex user interfaces, and sincerely thank all the developers of the Struts project team.
Start going up!
The Struts framework can be divided into the following four main parts, three of which are closely related to the MVC mode:
1, model (model), in essence, MODEL in Struts is an Action class (this will be discussed in detail later), the developer implements its business logic, and the user request is the forwarding process of the Controller to Action. Based on the configuration information described by the struts-config.xml file.
2, view (view), View is made up of a set of JSP custom label libraries working with the controller servlet, using them to quickly establish interface interfaces.
3, the controller, essentially a servlet, forwarding the client request to the corresponding Action class.
4, a bunch of toolkits for XML file parsing, Struts is using XML to describe how to automatically generate some JavaBean properties, and Struts also uses XML to describe user prompt information in international applications (so The multilingual support of the application system is realized).
Ok, let's take a look at how to make each part of this framework and how to work in each other! Configure
Before using Struts, we must set up the JSP server first so that the server knows how to transfer the request for the specified suffix to the corresponding Controller-Struts ActionServlet processing, of course, these configuration information is generally passed on the server. Web.xml file read. We can define multiple ControllLoer in Web.xml, define one for each application. A typical web.xml file is configured as follows, with corresponding comments, very well-understood, when discussing Action, we will mainly analyze strutc-config.xml.
Controller is the role of "Traffic Police" in this framework. When the client is interactive with the server, it is controlled by her. Controller requests the HTTP package and forwards to the corresponding object in the frame, which may be a JSP page or an action. Controller is set to org.Apache.struts.Action.ActionServlet in Web.xml, in this case, this instance is an OREILLYACTIONSERVLET. In a complete control process, when processing an HTTP request, at the beginning of the control process, this servlet will get the request and control action to the corresponding configuration information from a configuration file struts-config.xml, which we will Detailed over detail, Controller makes the HTTP request forwarding through these configuration information, and these Actions realize real business logic after receiving the forward request. We must pay attention to a very important thing is that the Action object is to be able to call this ActionServlet. Through this powerful feature, when the Action object forwards the request to other Action objects during the control (initial request is ActionServlet Get, forward to the Action object, and the Action object can also forward to another object), we can put some data objects that need to be shared by calling some ways to put in this servlet in some standard containers in this servlet.
Model (Model)
The so-called model is an object that truly handles the user request and saves the result of the user's request and saves the processing result. In the entire process, we generally use JavaBean to save some information to pass between individual objects. Because in the frame, the Model object is an object that is really handling the business logic function, so it is the largest part of the application requirements in the framework. In the implementation of Struts, the specific expression of Model is the Actionform object and the Action object corresponding to it. Check the data of the user to the form, and even preprocess data can be done in the actionform. In the usual application, it is generally a model object and a relationship corresponding to a request page, but it is also possible to correspond to multiple page requests with a Model object. If the struts-config.xml configuration file does not specify an Action corresponding to a Model object, the controller will directly put (complete data package through the Model object) to a View object.
Struts-config.xml
The Struts-Config.xml configuration file mentioned earlier is the main bone of the entire frame. The web.xml file defines where a request should be forwarded, and the work behind will be controlled by Struts-Config.xml management. It can be said that struts-config.xml is the "把" of the entire Struts framework. Only the "boss" knows all the request and action mapping relationship, if he doesn't get or not cool, the whole "socio" is also Not flat :) Today's application, XML forms of configuration files are increasing, and if the entire system uses such profiles, then the modular and maintainability of the entire system is very easy. Use the configuration file to describe the request-action control process and interrelationships, rather than writing the call relationship between the objects in the code, then the application system has changed, we only use the modified configuration file, not again Re-build the release program.
Controller configures the configuration information of the struts-config.xml file to determine that object should be called to handle it, from the point of efficiency, which is read in the system when the system is started. Below we will explain a very short Struts-config.xml file, defined a login action corresponding to the login request, and will be forwarded to com.OrEilly.ui.authentication.Actions.LoginAction after the request arrives, The result of the object processing determines the page returned to the user. This example also also demonstrates an Action object forwarding to another Action object, and the other returned object in an example is a View object, that is, the login.jsp page we see. XML Version = "1.0" Encoding = "UTF-8"?> >>>
View (View)
View objects generally refer to the JSP page. The STRUTS framework actually does not have a real JSP requirements specification, but provides a lot of label libraries to integrate JSP into the Struts framework for developers. The JSP page in Struts can be stored in a JavaBean through the data input by the label definition method in Struts, which is the Actionform Bean we mentioned earlier. Call (automatically or manually) the auditing method of the Action class to check the data input data, if the illegal data is found, the error message returns the error message to the user with a general mechanism of Struts. The STRUTS framework provides several tag libraries that have their own different purposes. Since these libraries can also be used separately from the Struts framework so that we can also try to use these labels in other systems, including:
* Struts-HTML - This tag library is used to create dynamic HTML pages and forms.
* Struts-bean - provides similar or more than the functionality.
* Struts-logic - Control for conditions, cyclic and other processes when the page outputs text messages.
* Struts-template - used to generate a dynamic JSP page template with a common style.
In addition, the customizable label library has a large use of the Struts. It is very simple to implement the multilingual features of the application through the resource file. If you apply Struts, if you want to change the user's interaction information in the system, it is very simple. Replace a different resource file.
Everyone starts to apply Struts!
The Struts framework may be a relatively new technology for most developers. But we can now see Struts's figure in a lot of application, and we can use the Struts framework in new applications or developed JSP projects.
For example, in a large database application system developed by the author now, business logic is implemented by EJB, and the user interface is a JSP page. The user input form and the corresponding Action class are defined in the struts-config.xml file. When a request occurs, that is, the user data is submitted to the action in the form of ACTIONFORM, the Action first calls the ActionForm's verification method, data check school After the pass, the action calls the method in the corresponding EJB to complete the data operation, the result of the operation is returned in XML, and then the XML resolution is then placed in the JavaBean - ActionForm to return to the JSP page to return to the JSP page.
The entire control process (including different return results after Action call) is all in Struts-Config.xml, which is very convenient for application processes. No matter whether it is a servlet or a JSP page (even in some N-layer application architecture), you do not need to write code for display data.
Since the author has been developed by the author, there are many JSP pages and the Actionform type to be submitted, so I found a trouble of Struts, that is: We want to develop a corresponding action for such a multi-page and ActionForm Class to complete control because we are currently JSP and ActionForm and Action are one-on-one relationship. However, if you have more works in the previous analysis and design of the project, make more perfect design, such a situation can be avoided, of course, in the development of new products, think of all needs It is impossible to understand that it is impossible. Do we have this experience? When the application system in the development is step by step, the update and more clear demand will be proposed. However, the system of this use Struts has developed six months, it is really rare, huh, huh. Remove these non-technical factors and do not talk, the Struts framework saves us a lot of time for us, and the development system is quite stable, which can be said to be a mature product. In the second part of this series, we will integrate each small code, complete an instance of a complete Struts application, I hope everyone will continue to learn struts with the author!
Note: Sue Spielman is an editor of onjava.com, mainly in JSP and Servlet technology, she is also the president and senior technical consultant of Switchback Software LLC.