Struts Getting Started

xiaoxiao2021-04-08  290

Struts installation: 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.1, 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 handles JavaBeans property, international tips, and messages through Java Reflection Apis: A user registration system, users enter information via web page: Registration ID number, password , Email, if the registration is successful, return successful prompt information, and the registration failed prompt information will appear. 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"%>

Reguser

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:

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 The generation of database operations, slightly * /}} The generation of FORMBEAN is to provide data to ActionBean, and data in the formaan 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. Struts Advantages: Struts is the same as Tomcat, Turbine, etc., is open source software, which is a big 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.

转载请注明原文地址:https://www.9cbs.com/read-132926.html

New Post(0)