Struts principle, development and project implementation
HOLEN 2002-9-12
1, summary
2. Key words
3, Framework
4, Struts origin
5, struts work principle
6, Struts installation
7, one instance
8, Struts advantages and disadvantages
9, Struts project implementation experience
10, summary
11, reference
1, summary
This article mainly tells the working principle, installation, and configuration of Struts (Struts1.0.2). Then combined with an example, discussed Struts in the actual development process, and finally provides some experience accumulated during the development process for reference.
2. Key words
Struts, MVC, J2EE, TILES, FRAMEWORK
3, Framework
Framework is schema (framework), here is the software architecture method, why do you want to mention Framework? Please allow me to say from J2EE.
The J2EE system includes a number of technologies such as JSP, Servlet, EJB, and Web Service. The emergence of these technologies provides a very competitive choice for web application development of the era of e-commerce. How to combine these techniques to form a stable architecture that adapts to the project is a very important step in the project development process.
This step is generally completed by architect designers. The designer will screen for screening according to the technical needs, and take into account the role division of role in the development process, the post-operation maintenance, and system scalability. Factors, build a system architecture.
A successful software requires a successful architecture, but the establishment of the software architecture is a complex and continuous improvement process. Software developers are unlikely to do different architectures for each different project, but always try to use it. Architecture, or develop as much as possible, Struts is one of them, and Struts is a popular J2EE-based architectural solution, and other common J2EE-based architectures are also Turbine, Realmothods, etc.
4, Struts origin
Struts was earliered as an integral part of the Apache Jakarta project. The founders of the project hopes through research, improvement, and improved Java Server Pages (JSPs), servlets, label libraries, and object-oriented technology levels. The current highest release version is struts1.0.2, you can download at http://jakata.apache.org/struts.
Struts This name is from the support metal rack used in the building and the old aircraft. Its purpose is to help you reduce the time to develop web applications using the MVC design model. You still need to learn and apply this architecture, but it will be able to complete some of them. If you want to mix the advantages of servlets and JSP to establish scalable applications, Struts is a nice choice.
5, struts work principle
The abbreviation of MVC, MODEL-View-Controller is a commonly used design pattern. The MVC weakens the coupling between business logic interfaces and data interfaces, and makes view layers more varied. MVC works, as shown in Figure 1 below:
Struts is an implementation of MVC, which uses a part of the Servlet and JSP tag (belonging to J2EE specification) as an implementation. Struts inherits the features of the MVC and made changes and expansions according to the characteristics of J2EE. The working principle of Struts, as shown in Figure 2 below:
Control: You can see that there is an XML file struts-config.xml, which is associated with Controller. In Struts, the Controller role in the MVC is a servlet called an ActionServlet. ActionServlet is a universal control component. This control component provides an entry point that processes all HTTP requests sent to Struts. It intercepts and distributes these requests to the appropriate action classes (these action classes are subclasses of the Action class). Additional control components are also responsible for popping an action frombean using the corresponding request parameters, and transmits the action class (usually called ActionBean). Action class implements core business logic, which can access Java Beans or call EJB. The last action class transmits the control right to subsequent JSP files, and the latter generates a view. All of these control logics use the struts-config.xml file to configure. View: Mainly by JSP Generation Page Complete View, Struts provides a rich JSP tag library: HTML, Bean, Logic, Template, etc., which helps separate performance logic and program logic.
Model: The model exists in the form of one or more Java Beans. These beans are divided into three categories: Action Form, Action, JavaBean or EJB. Action Form is usually called FormBean, encapsulated user request information from Client, such as form information. Action is often called ActionBean, obtains the FORMBEAN from the ActionSevlet, takes out the relevant information in the FORMBEAN, and makes relevant processing, usually call Java beans or EJB.
Process: In Struts, the user's request is generally named * .do as a request service name, all * .do requests are pointed to the ActionSevlet, the ActionSevlet, according to the configuration information in Struts-Config.xml, and the user requests to encapsulate the user as a specified name. FORMBEAN, and pass this FORMBEAN to the ActionBean of the specified name, and the ActionBean completes the corresponding business operation, such as file operation, database operation, etc. Each * .do has a corresponding FormBean name and an ActionBean name, which is configured in Struts-Config.xml.
Core: The core of Struts is ActionSevlet, the core of ActionSevlet is struts-config.xml. This will also be discussed in detail later.
6, Struts installation
The Struts is installed relatively simple. The following takes Tomcat 4.0.4 as an example to describe the installation process.
First, please go to http://jakarta.apache.org/struts to download Struts, it is recommended to use the Release version, now the highest version is 1.0.2, get a zip file after downloading is a zip file.
Understand the ZIP, you can see this directory: LIB and WebApps, WebApps have some WAR files.
Suppose your Tomcat is put under C: / Tomcat, copy those WAR files to C: / Tomcat / WebApps, and restart Tomcat.
Open the browser and enter: http: // localhost: 8080 / struts-example / index.jsp, if you can see the deep blue icon of "Powered By Struts", the explanation is successful. This is an example of Struts comes with a detailed explanation documentation, which can be used as a beginner's entry tutorial. In addition, Struts also provides a system useful object: XML processing, automatically processes JavaBeans property, international tips, and messages through Java Reflection APIs.
7, one instance
A user registration system, the user enters information via the web page: Register the ID number, password, email, if the registration is successful, return successful prompt information, and the registration failed prompt information.
The following is some of the core code of the related files.
Project is established:
Before formal development, you need to establish this item in TOCMAT (my Tomcat installed in C: / Tomcat).
A faster setup method is:
New Directory TEST under C: / Tomcat / WebApps, then under C: / Tomcat / WebApps / Struts-Example
Web-INF directory copy to the TEST directory, then empty the SRC and CLASSES directory under Test / Web-INF, and clear the content in the struts-config.xml file. This way, the Struts package and related configuration files we need are all.
When developing, put the JSP file in the Test directory, the Java original file is placed under Test / Web-INF / SRC, and the compiled class file is placed under Test / Web-INF / CLASSES.
Registration page: reguser.jsp
<% @ page contenttype = "text / html; charset = uTF-8" Language = "java"%>
<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>
hEAD>