First, what is a Struts framework is reusable, semi-finished application, can be used to generate a dedicated custom program. As long as you carefully study the real app, you will find that the program is approximately composed of two types of different types of nature, and a class of specific transactions to the program is closely related, we may wish to call them business components; another type is application service. For example: a tax collection and management system and a book management system have great differences in handling their business, and the components of these direct processing services may be reused in different systems due to different business properties, while others may be reused in different systems. If the control, the input verification, error handling, and label library, etc., such as the program flow, the error, and label libraries, such as the program-related components, can be reused well in different systems. It will naturally want to extract some things that are common in different applications, make a semi-finished program, such semi-finished products are the so-called program framework, do not have to start with a new thing, but can be On this basis began to build. In fact, some large software companies choose to build such a framework. But most of the small and medium-sized software companies or other organizations have no conditions to create a framework. As an open original application framework, Struts has been rapidly developed in recent years. It is very extensive in JSP web application development, and some literature says it has become the factual standard for JSP web application frameworks. So what is Struts? To answer this question, you have to have two basic structural patterns from JSP web applications: Model 1 and Model 2, in order to give readers some real help, and try to make the learning curve flat, I want to adopt examples The driver is gradually answered and answering questions, because the best way to learn a technology is too much in practice, in practice, gradually deepen understanding and grasp of its spiritual essence, rather than introducing A new concept of a lot of new concepts makes everyone feels an obey, or the death of a big rebap is facing a true actual demand. As, even if he has become a doctor in the book, as long as he doesn't go to the water, I think he is also unlikely to really swim. Model 1 Structure As shown in Figure 1: Figure 1 MODE1 1 is a mode-centric pattern in JSP file, which is not only responsible for performance logic in this mode, but also controls logic. Professional books are called logical coupling in the page, this way of handling, some small items such as: a simple message book, not too bad, in fact, people began to contact some to themselves When you are new, for example, when you access the database with JSP, you often like to provide a single JSP page that contains all this, because this can grasp the overall in a page, which is easy to understand. However, when developing large-scale with Model 1 mode, the program flows to the page determined by some mutually perceived pages. When the page is much more clear, it will be a very complicated thing to grasp the flow direction, and when you modify a page, you may affect the related The page, the big feelings are very difficult, making the program's modification and maintenance is extremely difficult; there is a problem is that the program logic development and page design are entangled together, which is inconvenient for division of labor. Such a procedure is not good in robustness and scalable. GRADY BOOCH et al. In the UML User Guide, it emphasizes the importance of modeling, playing a fangs, private residential, and buildings to explain the reasonable way to use by people to deal with different sizes. People should also use different models for applications for different sizes.
In order to overcome the defects of Model 1, people introduced Model 2, as shown in Figure 2: Figure 2 It introduces the "controller" concept, the controller is generally served by servlet, the client's request is no longer sent to a processing business The logical JSP page, but is given to this controller, and then call different transaction logic based on the specific request, and return the processing result to the appropriate page. Therefore, this servlet controller provides an application for a central-backed-processed hub. On the one hand, it provides a suitable entry point for inputting data, identity authentication, log, and international programming; on the other hand, it also provides possible possible possibilities to peeling business logic from JSP files. After the business logic is separated from the JSP page, the JSP file has changed into a simple completion of the display task, which is often the view. The independent transaction logic becomes the MODEL that people often say, plus the controller Control itself, constitutes the MVC mode. Practice has proved that MVC model provides huge convenience to the development and maintenance of large programs. In fact, the MVC is not a mode proposed for the web application. The traditional MVC requires M to report its status change to V, but it is difficult to do this because the web browser works in a typical pull mode rather than push mode. So some people will refer to MVC2 for web applications. As mentioned above, the MVC is a model, of course, there is a variety of different specific implementations, including the program framework that reflects the MVC thinking, Struts is a program framework for implementing MVC2. Its general structure is shown in Figure 3: Figure 3 Figure 3 Basically outlined a structure of Struts-based applications, from left to right, respectively, of its representation layer, control layer (controller), and model layers (Model). Its representation is built using the Struts tag library. All requests from the customer need unified by the framework by the servlet received by the ActionServlet (ActionServlet Struts has been written for us, as long as you apply nothing special requirements, it basically meets your requirements), according to the received request ActionMApping in the struts-config.xml, give the request to the appropriate action to process, solve the problem of anyone who does, and together constitute the Struts controller. Action is a real-working component in the Struts application. Developers generally take a lot of time here, it solves what problems, it completes the application's business, business, business, business, business, business, business, business Components solve the problem of how to do, and return the results of the execution to a JSP (or action) of the JSP (or action) of the desired depiction response to the ActionServlet to present the response to the customer. The process is as shown in Figure 4: Figure 4 Here to specifically explain: This is the class, which has already said it is a real work in Struts, which is also worthy of our high attention.
However, there is two different opinions about it, there are two different opinions, and there is two different opinions. One is considered to be a model layer, such as: "JSP Web Programming Guide; other think it belongs to the control layer," Programming JAKARTA STRUTS "," Mastering Jakarta Struts ", and" Struts Kick Start ", etc., it is considered to be part of the controller, and some other books such as" Struts in Action "also recommends avoiding the business logic in the Action class, that is Say, the contents of the brackets in the ACTION in Figure 3 should be removed from it, but in fact, there are some systems that will be relatively simple and not intended to be reused in the action, so it is also shown in the figure. Obviously, it is conducive to its reuse from the ACTION, and also enhances the aggressiveness and design of the application. Therefore, it can actually be regarded as the adapter of Controller and Model. If you are hard to return it to that section, the author is more inclined to the latter view, that is, it is part of the Controller, in other words, it should not contain too many Business logic, but should simply collect the data required by the business method and pass to the business object. In fact, its main responsibility is: check premise or declares that the business logic method you need to detect or handle other error routing controls to the relevant view, the beginner may feel unacceptable, the following is a comparison Specific examples to further help us understand. Such as: Suppose, we do an e-commerce program, and now the operation task to complete is to submit the order and return to the customer. This is about what is done, but it should be completed by the Action class, but specifically how to get a database connection, Insert the order data to the database table, how to get this order number from the database table (usually data from the data column), this series of complex issues, this is what is solved, and it should be by one ( Assume the ORDERBO) Business object is MODEL to complete. ORDERBO may do this with a method of returning an integer value, Action is the first verification order data is correct, so as not to say that the garbage is transferred to the garbage; if it is correct, simply call the SubmitOrder method for ORDERBO. Come get the order number; it also has to handle any errors in the calling process; finally returns different results to customers according to different situations. Second, why should Struts framework say that since the beginning of this article, you can build this framework, why should I use Struts? These reasons I would like to be obvious: First, it is built in this recognized good pattern of MVC, and Struts is involved in M, V, but it is mainly providing a good controller. On a set of custom label libraries, that is, its focus on C and V, therefore, it has a series of advantages brought by MVC, such as: structural level, high reusability, increase The robustness and scalability of the program facilitates development and design division of labor, providing centralized unified privilege control, check, internationalization, log, etc. Second, it is an open source project to receive its inventors Craig R. Mcclanahan Some programs and masters of programs and masters have been careful, and have experienced actual tests, making their functions becomes more powerful, and the system is daily. Finally, it shows good fusements for other technologies and frameworks.
For example, now, it has been integrated with Tiles, you can look forward to it quickly and JSF will be combined. Of course, like any other technique, it is not a perfect, such as: it looks at the class and some attributes, the parameters of the parameters are somewhat random, bringing some inconvenience to the use; there is only one an actionform parameter that can only receive an actionform parameter in the Action class Execute method. Wait. But the loss is not covered, these have no effect it is widely used. Third, Struts installation and basic configuration We mainly explain the Struts1.1 version, which is assumed here that the reader has configured the Java runtime environment and the corresponding web container. This example is J2SDK and Tomcat4.1.27. Below, the basic portion is introduced in a manner similar to step by step. Install struts to http://jakarta.apache.org/ Download the Struts installation file, this example is version 1.1. Next you are going to perform the following steps to complete the installation: 1. Unzip the downloaded installation file to your local hard disk 2, generate a new web application, assume the root directory of the application we generated in the / webApps / mystruts directory. Create a name / mystruts for the app in the server.xml file
3. Copy the following JAR file from the file unzipped from step 1
/ WebApps / MyStruts / Web-INF / LIB directory, the main files are as follows.
Struts.jar
Commons-beanutils.jar
Commons-Collects.jar
Commons-dbcp.jar
Commons-digester.jar
Commons-logging.jar
Commons-pool.jar
Commons-Services.jar
Commons-Validator.jar
4. Create a web.xml file, which is a deployment description file required by servlet-based web applications, a Struts web application, in nature is also a servlet-based web application, which cannot be exception.
Struts has two components to configure in this file, which are: ActionServlet and label library. Here is a configuration list:
XML Version = "1.0" encoding = "UTF-8"?>
// en "" http://java.sun.com/dtd/web-app_2_3.dtd ">
init-param>
init-param>
servlet>
servlet-maping>
taglib>
taglib>
taglib>
web-app>
Above We completed the basic configuration of servlet and tag libraries in Web.xml, and more framework components are configured in Struts-Config.xml:
5. Create a basic struts-config.xml file and put it on
In / webapps / mystruts / web-inf / directory, this file is a configuration description file based on the Struts application, which combines components in the MVC structure, and the development process will continue to enrich it and change it. At Struts1.0, an app can only have one such file, bringing some inconvenience to division of labor. When Struts1.1, there can be multiple files, and overcome the above disadvantages. The components that need to be configured in this file are: Data-Sources
Global-Execptions
Form-beans
Global-Forwards
Action-mapings
CONTROLLER
Message-resources
Plug-in
The list is as follows:
XML Version = "1.0" encoding = "UTF-8"?>
// en "" http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">
Struts-Config>
To this end, we have provided the various components required to complete a simple Struts application. As mentioned earlier, we will constantly enrich and modify the above two configuration description files during the development process. Below we will actually make a very simple application to experience the true process of the Struts application development, in order to have a real understanding. After completing the introduction of the basic part, the author will give some examples that are often difficult to use in actual development and let beginners feel difficult. Finally, the relationship between Struts and other frameworks and examples of combining them generate applications will be described. Author: Luo Kuaibo Dangyang IRS Information Center lhbf@sina.com contact him