First, Struts origin
Struts is earliered as part of the Apache Jakarta project, and the founders of the project hope to improve and improve the research, improvement and improvement of JavaServer Pages, Servlet, label libraries, and object-oriented technology. Struts This name is from the support metal rack used in the building and the old aircraft. The reason why this frame is called "Struts", in order to remind us to remember the basic support of our housing, architecture, bridge, and even we step on stories. This is also a wonderful description of the role that Struts played in the development web application. When establishing a physical building, construction engineers use the pillars to support each layer of the building. Similarly, software engineers use Struts to support each layer of business applications. Its purpose is to help us reduce the time to develop web applications with the MVC design model. We still need to learn and apply this architecture, but it will be able to complete some of these heavy work. If you want to mix the advantages of servlets and JSP to establish scalable applications, Struts is a nice choice.
Early SmallTalk programming uses the MVC-View -Controller mode (Patterns) to add program code elasticity, MVC mode cuts the program code to three, and the Model section is the business and application domain related logic. The object of the management status, the Controller section receives the information entered from the View and interacts with the Model part. It is a business process control (Flow Control), and the View section is responsible for displaying the information and receives the user input information. In Java applications, JFC / Swing, AWT, JSP are available as a technical specifications that can be used as a View, while JavaBean and Enterprise JavaBean specification can be used for Model program code, once the application is appropriately split by MVC mode, MODEL section program code Use in applications where different user interface appearance is repeated.
As JSP and Servlet technology is applied to web-based applications, Java developers believe that the maintenanceability and repetition of Web applications should be improved in better patterns. Early JSP specifications have enumerated two feasible JSP application architectures, respectively, named Model1 and Model 2.
In the Model 1 architecture, JSP processes requests from the web browser, and assists in the JavaBean processing application related logic. The Model 1 architecture is simple to write, but JSP in Model 1 may shoulder the View and Controller roles, and the two types of program code may be mixed without maintenance. The servlet is included in the architecture to play the front-end controller role in Model 2, and send the request to the web browser to servlet, and servlet can centrally manage the user login, permission control, multi-language conversion, etc., and then turn to demand Corresponding JSP processing. The MODEL 2 adopts better MVC mode, but has added complexity of writing.
Struts is part of the Jakarta project under the Apache Software Fund. In addition to Struts, there are other successful open source products, including Tomcat, Ant and Velocity. In 2000, Craig R. Mr. McClanahan contributed the Application Framework original program code for JSP Model 2 in which he wrote to the Apache Foundation, became the Apache Jakarta Planning Struts Framework.
The starting code foundation has been developed from May 2000 until June 2001, version 1.0 released. More than 30 developers participated in and thousands of people participated in the discussion group. The STRUTS code is managed by a volunteer Commnitter team. By 2002, the Struts Group has 9 volunteer commnitter. The main architectural design and developers of the Struts framework are Craig R. McClanahan. Craig is also the main architect of Tomcat 4, as well as major architects and implementors of Java Web Services Developer Pack. He is now the SUN JavaServer Faces (JSR-127), and the Normal Leaders of the web layer architecture of the J2EE platform. Mr. Craig R. Mr. McClanahan is one of the members of JCP ExpertGroup. He has participated in the JSP specification to prepare with Tomcat 4, so Struts Framework is widely attached importance to Java developers. Borland has supported Struts framework in the development tool JBuilder at the end of 2002.
Second, Struts installation
1, install the software required for Struts
Java Development Kit - We need Download and Install 1.2 (or after) JDK.
download link:
http://java.sun.com/j2se
Servlet Container - The usual selection is to download Tomcat (at least version 3.1, it is recommended to use a version of 4.0 or more).
download link:
http://jakarta.apache.org/tomcat
Ant Build System - If we install a package via the Struts source code, we must download 1.1 or later version of the Ant Build System. Developing our own web apps through Struts is, it is recommended to use Ant to Build our app.
download link:
http://jakarta.apache.org/ant
Servlet API Classes - To compile Structs yourself, or the application uses Struts, we need a servlet.jar package that contains the servlet and JSP API objects. Most servlet containers (such as Tomcat) have come with this file. Otherwise, we must download: http://jakarta.apache.org/builds/jakarta-servletapi
XML Parser - Structs requires an XML processor compatible with Java API for XML Parsing (JAXP) specifications, we can use Xerces.
download link:
http://xml.apache.org/xerces-j
Xalan XSLT Processor - If we construct our Structs system via the Structs source code, we must download and install 1_2_d01 or later version of the Xalan XSLT processor (Xalan in XERCES). This processor is used to convert XML-based Structs documents to HTML documents.
2, construct Structs via source code
1) Download Structs source code release package.
2) Set an ANT_HOME environment variable to point to our Ant directory.
3) Set the java_home environment variable, point to our JDK directory.
4) Set the servletapi_home environment variable, point to our servlet API directory (if our ClassPath already contains servlet.jar, you do not need to specify the directory)
5) Uninching the StruCTS source code package.
6) Enter the structs directory, run the following command :/ build.sh dist
This command will create a Struts binary release package, directory in ../ dist / structs (relative to our compilation directory).
3, install Structs via Struts
1) Download the Struts binary release.
2) Unread the Struts binary release. (If we construct Struts through the Struts source code, the result of Build is already unpacked Struts). The unproved Struts contains the following:
LIB / STRUTS.JAR - This file contains all Java objects for Struts. We need to copy it to our web-INF / lib directory for our web application.
LIB / STRUCTS.TLD - This is a "Tag Library Descriptor" file that describes the free TAG of the Struts library. Need to copy it to our web-inf directory for our web application.
WebApps / Struts-Documentation.war - This is a "Web Application Archive" file that contains all Struts documents. We can install it to the servlet container that supports Servlet API 2.2 or later (recommended using Tomcat).
WebAPPS / Struts-Example.war - This is a web application instance that extensively demonstrates many of the Struts. We can install it into a servlet container that is compatible with servlet2.2 or later and the JSP1.1 or later version of the servlet container (recommended using Tomcat). WebApps / Struts-Test.war - This web application contains many TAG test web pages supported by Struts, which can be used as an example of using Struts Tag.
4, Struts basic component package
The installed Struts has approximately 15 packs, composed of nearly 200 classes, and the quantity is still expanded. Here we cannot introduce one by one, only to list several major brief introductions. The following table describes several component packages in the current Struts API, including Action, Actions, Config, Util, Taglib, Validator. As shown in Table 1 below:
Org.apache.struts.Action basically, controls the core class of the running of the entire Struts Framework, and components are in this package, such as the controller ActionServlet we mentioned above. Action, ActionForm, ActionMapping, etc. Struts1.1 is more than 1.0 DynaActionform class. Added Dynamic Extension Generation FormBean Features Org.Apache.Struts.Actions This package is the primary role to provide a specific adapter conversion function between the client's HTTP request and business logic processing, and some of the part of the version 1.0 reproduces the class of frombean, The DynaActionForm component replaced by the ACTION package in Struts1.1 provides mapping of the configuration file struts-config.xml element. This is also a new function in StURTS1.1 org.apache.struts.util Strtuts to better support Web Application's application, such as support, such as Connection Pool and Message Source. For more information, please refer to http://jakarta.apache.org/struts/api/rg/apache/struts/util/package-summary.html org.apache.struts.taglib This is not a package, but a customer label class Collection. The following includes Bean Tags, HTML Tags, Logic Tags, Nested Tags, Template Tags, for building tag classes for building user interfaces. Org.apache.struts.validator struts 1.1 Framework adds Validator Framework for dynamic configuration of the FROM form. For more information, see http://home.earthLink.net/~dwinterfeldt/ 3, Struts Configuration and Component Introduction
About Struts Components Configuring Struts-Config.xml file Descriptions have been introduced in the previous article, here no longer say, the components and configurations in the framework are mainly introduced.
The following table lists the core class of Struts, that is, the corresponding classic MVC component duties.
Table 2 Correspondence Struts class and MVC
Class Description ActionForward user pointing or view Select ActionMApping Status Change Event ActionMApping Status Change Event ActionServlet controller, accept user request and status change, and send a view to select a part of the Action controller, the model interaction, execute status change or status query, and Tell ActionServlet Next Select View
In addition to these core classes, Struts use some configuration files and view helpers to communicate controllers and models. The following table lists the Struts profile and describes the roles they in the architecture.
Table 3 STRTUTS configuration file
File Destination ApplicationResources.properties stores localized information and labels to enable applications to international Struts-Config.xml storage controller objects, including model support, state changes, status query
To expose the Struts configuration data to the view, the frame provides a large number of assistant classes in the form of a JSP tag, as Table 4 STRTUS view assistant.
Tag Library Descriptor Struts-HTML.TLD Extension HTML Form's JSP Tag Struts-Bean.TLD Extension Processing JavaBean JSP Tag Struts-Logic.TLD Extension Test Property JSP Tag
The controller component in the configuration framework of the controller component is an org.apache.struts.Action.ActionServlet class implementation, this class is an extension of the javax.servlet.http.httpservlet class.
ActionServlet configuration:
We need to declare an actionServlet in Web.xml and configure it to be loaded. The following is an initialization parameter that can be configured:
Parameter Default Value Description Application Null Application Resource Collection Class Buffersize 4096 File Uploaded Buffer Size Config /Web-inf/Struts-Config.xml Profile Location and Name Content Text / HTML Default Content Type Debug 0 Program Debugging Level Detail 0 Program Debugging Detail level Factory NULL Message Resource Factory, Submarks 馐 ⒆ ⒆ ⒆ ⒆??>>>> 名 名 名 名 名 名 名 名 名 名 名 名 名 类 名 类 类 类 名 名 类 类 名 类.apache.struts.action.ActionForward package ActionForward object information The name locale true, will store a local object mapping org.apache.struts.action.ActionForward package ActionMapping information in a user session MaxFileSize 250M upload The maximum size of the file multipartclass org.apache.struts.Action.ActionForward Processes the NOCACHE FALSE HTTP header to set to True for the invalid information keyword to TRUE, and return NULL TEMPDIR for invalid information keywords. As a servlet parameter, the working directory processing download file is the temporary work directory VALIDATE TRUE uses the new format profile Vallidating True Validity analysis of the configuration file.
In most cases, the standard servlet can meet the needs of users.
When the URI of the first received a specific request, the ActionServlet instantiates the appropriate Action class, and the ActionServlet stores a reference in servlet in the Action class instance. When instantiated, the Action class will be temporarily stored.
Error handling
Struts provides two classes to handle errors: ActionerRRORS and ActionError, they all expand org.apache.struts.Action. ActionerRORS saves a collection of ActionError objects, each represents an independent error message. Each ActionError contains a keyword, which is mapped to the error message stored in the resource file, and this resource file is specified in the ActionServlet initialization parameter.
Four, Struts run
You can use the following steps to use Struts in our own application:
1. Copy the lib / struts.jar of the struts directory to the web-inf / lib directory of the web application.
2. Copy the lib / struts * .tld of the struts directory to the web-inflicity of the web application.
3, modify the web-inf / web.xml file of the web application directory, add a
4. Modify the web-inf / web.xml file for the web application directory so that the following Tag library definition:
5. Create a web-inf / action.xml file to define the Action mapping relationship of our web application. You can refer to the Action.xml file in the Struts example to learn more about grammar requirements.
6. Add the following Tag library definition to the JSP page using the Struts Tag library:
<@ Taglib Uri = "/ Web-INF / STRUTS.TLD" prefix = "struts"%> <@ Taglib URI = "/ Web-inf / struts-bean.tld" prefix = "bean"%> <@ taglib URI = "/ Web-inf / struts-logic.tld" prefix = "logic"%>
7. Finally, when compiling the Java programs for our web application, don't forget to include the struts.jar file in the classpath.
Five, Struts Advantages
advantage:
Struts is the same as Tomcat, Turbine, etc., is open source software, which is a great advantage. Make developers more in-depth understanding of their internal implementation mechanisms. In addition, Struts's advantages are mainly concentrated in two aspects: taglib and page navigation. Taglib is the Struts tag library, flexible, which can greatly improve development efficiency. In addition, in addition to the current Domestic JSP developers, in addition to the common tags that come with JSP, we have rarely develop their own tags, maybe Struts is a good starting point.
About page navigation, we think that will be a development direction of the future, in fact, doing so, make the system's context more clear. With a configuration file, you can grasp the contact between the entire system, which has a great advantage for later maintenance. Especially when another group of developers take over the project, this advantage is more apparent.
Disadvantages:
Taglib is a great advantage of Struts, but for beginners, it is necessary to continue to learn, and even chaos us habits, but when we are used to it, we will feel that it is really very Baton. Struts divided the MVC's Controller into three. In the media, the sorrow is worried, and the 黾 黾 低 低 低 丛 取::::: As the changes continue to join, this is sometimes very frustrating. Struts has grown more than 3-4 years from now, and it has gradually been used for commercial software. Although it is still a lot of shortcomings, it is a very good J2EE MVC implementation. If our system is ready to adopt J2EE MVC architecture, then you may wish to consider Struts.
Struts is a web-based MVC solution that must be implemented with HTML, JSP files, and servlets, Struts requires support for JSP 1.1 and Servlet 2.2 specification servlet containers. Learning, understanding and mastering Struts is a course in which we must work based on J2EE software developers, and our further deepening of enterprise J2EE technology applications.