Struts principle, development and project implementation HOLEN 2002-9-121, summary 2 10, summary 11, reference 1, Abstract This article mainly tells the work 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. Keywords Struts, MVC, J2EE, TILES, FRAMEWORK3, Frameworkframework 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. The origin of Struts 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 Working Principle MVC is the abbreviation of Model-View-Controller, is a commonly used design mode. The MVC weakens the coupling between business logic interfaces and data interfaces, and makes view layers more varied. The MVC works, as shown in Figure 1: Struts is an implementation of MVC, which uses a part of the Servlet and JSP tag (belonging to J2EE specification) as part of the implementation. Struts inherits the features of the MVC and made changes and expansions according to the characteristics of J2EE. Struts Working, as shown in Figure 2: Control: You can see an XML file struts-config.xml by Figure 2, which is associated with Controller, in Struts, the Controller role in the MVC is one 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 Install Struts is relatively simple, below 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, an instance of a user registration system, the user enters the relevant 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 establishment: Before formal development, you need to establish this item in TOCMAT (my Tomcat installed in C: / Tomcat). A faster setup method is: Copy the directory TEST under C: / Tomcat / WebApps, then copy the web-inflicid under the C: / Tomcat / WebApps / Struts-Example to the Test Directory, then Test / Web The SRC and Classes directory under -Inf empty, and the contents in the struts-config.xml file can be emptied. 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"%>
Logname:
PASSWORD:
E-mail:
This JSP page is different from a normal JSP page, because it uses Taglib, which may be difficult to master for beginners, but this is one of the essence of Struts. Flexible use, will greatly improve the development efficiency.
Struts-config.xml:
TYPE = "Org.cjea.struts.example. reguserform" />
TYPE = "org.cjea.struts.example.Reguserac"
Attribute = "reguserform"
Scope = "request"
Validate = "false">
The core of Struts is Controller, namely ActionServlet, and the core of ActionServlet is struts-config.xml, and struts-config.xml sets all pages navigation definitions. For large Web projects, you can quickly grasp the context through this profile, whether it is for the early development, or later maintenance or upgrade is very beneficial. Master Struts-Config.xml is the key to master Struts.
FormBean: RegUserForm package org.cjea.Struts.example; import javax.Servlet.http.HttpServletRequest; import org.apache.Struts.action.ActionForm; import org.apache.Struts.action.ActionMapping; public final class RegUserForm extends ActionForm { private String logname; private String password; private String email; public RegUserForm () {logname = null; password = null; email = null;} public String getLogName () {return this.logname;} public void setLogName (String logname) { this.logname = logname;} public void setPassWord (String password) {this.password = password;} public String getPassWord () {return this.password;} public void setEmail (String email) {this.email = email;} public String getEmail () {return this.email;} public void reset (ActionMapping mapping, HttpServletRequest request) {logname = null; password = null; email = null;}} each FormBean must inherit ActionForm class, a page request is FormBean Package. That is, the HTTP Request package is in an object, and the point to explain is that multiple HTTP Request can share a FORMBEAN to facilitate maintenance and reuse.
ActionBean: RegUserAction package org.cjea.Struts.example; import javax.Servlet.http *; import org.apache.Struts.action *; public final class RegUserAction extends Action {public ActionForward perform (ActionMapping mapping, ActionForm form, HttpServletRequest.. Req, httpservletResponse res) {string title = req.getParameter ("Title"); string password = Req.getParameter ("password"); string email = req.getParameter ("email"); / * acquire user request, do the corresponding Database operation, slight * /}}
The generation of FORMBEAN is to provide data to ActionBean, and data in FormBean can be obtained in ActionBean. After the corresponding logic is processed, the business method completes the corresponding business requirements. Evolution of servlet: In the routine JSP, Servlet, JavaBean three-layer structure, JSP implements the functionality of the View, the servlet implementation of the Controller function, JavaBean implements Model implementation. In Struts, the servlet split in conventional cases is part of the ActionServlet, FORMBEAN, ActionBean. ActionServlet works with Struts-Config.xml, full-time completion page navigation, and no longer responsible for the specific data acquisition and corresponding logic, which is completed by FormBean and ActionBean. 8, Struts advantages and disadvantages: 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, I think that will be a development direction in 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 your habits of your webpage, but when you are used to it, you will feel it really Best. Struts divided the MVC's Controller one by three, and also increased the complexity of the system while gaining the structure clearer. Struts has been generated less than half a year from now, but it has gradually been used for commercial software. Although it still has a lot of shortcomings, it is a very good J2EE MVC implementation. If your system is ready to adopt J2EE MVC architecture, then you may wish to consider Struts. 9. Struts project implementation experience, we developed a web application based on the Struts architecture (combined with tiles). The following is some of our experiences and lessons that have accumulated in the project process, and hope to help you. 1. Project development based on the Struts architecture, first of all, there is a good overall plan, including which modules included in the entire system, each module needs how much formbean and ActionBean, etc., and it is best to be Struts-config.xml Management. The difficulty of developing Struts-based projects is configuring management, especially for Struts-Config.xml management. 2, if your project is very tight, and there is no experience in the project team, it is recommended not to take Struts. Struts needs a process that requires a skilled JSP programmer, you need to take about half a month. If combined with Titls, you take a longer time. 3, if you use Taglib in a large number in the web, your art will make some sacrifice.