Struts principle, development and project implementation

xiaoxiao2021-03-06  39

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"%>

reguser </ title></p> <p><HTML: BASE /></p> <p></ hEAD></p> <p><body bgcolor = "white"></p> <p><html: errors /></p> <p><html: form action = "/ reguration" FOCUS = "logname"></p> <p><Table Border = "0" width = "100%"></p> <p><tr></p> <p><TH align = "Right"></p> <p>Logname:</p> <p></ tH></p> <p><TD align = "left"></p> <p><HTML: Text Property = "Logname" size = "20" maxlength = "20" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><th align = "right"> Password:</p> <p></ tH></p> <p><TD align = "left"></p> <p><HTML: Password Property = "Password" size = "20" maxlength = "20" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><TH align = "Right"></p> <p>E-mail:</p> <p></ tH></p> <p><TD align = "left"></p> <p><HTML: Password Property = "email" size = "30" maxlength = "50" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><TD align = "right"></p> <p><html: Submit property = "submit" value = "submit" /></p> <p></ td></p> <p><TD align = "left"></p> <p><html: reset /></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ html: form></p> <p></ body></p> <p></ html: html></p> <p>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.</p> <p>Struts-config.xml:</p> <p><Struts-config></p> <p><form-beneans></p> <p><form-bean name = "reguserform"</p> <p>TYPE = "Org.cjea.struts.example. reguserform" /></p> <p></ form-beans></p> <p><action-mappings></p> <p><action path = "/ reguration"</p> <p>TYPE = "org.cjea.struts.example.Reguserac"</p> <p>Attribute = "reguserform"</p> <p>Scope = "request"</p> <p>Validate = "false"></p> <p><forward name = "failure" Path = "/ messagefailure.jsp" /></p> <p><forward name = "success" path = "/ messageagesuccess.jsp" /></p> <p></ action></p> <p></ action-mappings></p> <p></ Struts-Config></p> <p>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</p> <p>Package org.cjea.struts.example;</p> <p>Import javax.servlet.http.httpservletRequest;</p> <p>Import org.apache.struts.Action.actionform;</p> <p>Import org.apache.struts.action.actionmapping;</p> <p>Public final class reguserform extends an actionform {</p> <p>PRIVATE STRING LOGNAME;</p> <p>PRIVATE STRING Password;</p> <p>PRIVATE STRING Email;</p> <p>Public reguserform () {</p> <p>Logname = NULL;</p> <p>Password = NULL;</p> <p>Email = NULL;</p> <p>}</p> <p>Public string getLogname () {</p> <p>Return this.logname;</p> <p>}</p> <p>Public void setLogname (string logname) {</p> <p>THIS.LOGNAME = Logname;</p> <p>}</p> <p>Public void setpassword (string password) {</p> <p>this.password = password;</p> <p>}</p> <p>Public string getpassword () {</p> <p>Return this.password;</p> <p>}</p> <p>Public void setemail (String email) {</p> <p>THIS.EMAIL = Email;</p> <p>}</p> <p>Public string getemail () {</p> <p>Return this.Email;</p> <p>}</p> <p>Public void reset (actionMapping mapping, httpservletRequest request)</p> <p>{</p> <p>Logname = NULL;</p> <p>Password = NULL;</p> <p>Email = NULL;</p> <p>}</p> <p>}</p> <p>Every FormBean must inherit the Actionform class, and the FormBean is a package requested to page. 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.</p> <p>ActionBean: ReguSerction</p> <p>Package org.cjea.struts.example;</p> <p>Import javax.servlet.http. *;</p> <p>Import org.apache.struts.action. *;</p> <p>Public Final Class ReguSerction Extends Action</p> <p>{</p> <p>Public ActionForward Perform (ActionMapping Mapping,</p> <p>Actionform Form, HttpServletRequest Req,</p> <p>HttpservletResponse res)</p> <p>{</p> <p>String title = Req.getParameter ("Title");</p> <p>String password = Req.getParameter ("password");</p> <p>String email = Req.getParameter ("email"); / *</p> <p>Take the user request, do the corresponding database operation, slightly</p> <p>* /</p> <p>}</p> <p>}</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>8, Struts advantages and disadvantages</p> <p>advantage:</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>Disadvantages:</p> <p>Taglib is a great advantage of Struts, but for beginners, it is necessary to continue to learn, and even chaos your habits written, but when you are used to it, you will feel that it is really Baton.</p> <p>Struts divided the MVC's Controller one by three, and also increased the complexity of the system while gaining the structure clearer.</p> <p>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.</p> <p>9, Struts project implementation experience</p> <p>For some time, 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.</p> <p>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.</p> <p>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. This sacrific is particularly obvious when you combine Tiles. Of course, you will decide yourself by your ability to make yourself.</p> <p>4, taglib is a good thing, but flexible uses it requires a process, if you don't want to spend too much time on taglib, then just understand several tags related to Form, other tags are put, Look, first study ActionServlet and Struts-Config.xml, you will feel very accomplished.</p> <p>5, the birth time of Struts is not long, but more and more tools, if you use JBuilder as a development tool, then I can recommend a few excellent Open Tools, greatly improve development efficiency .</p> <p>6. Is Struts only suitable for large projects? NO! Struts is suitable for projects of various sizes. Of course, for large projects, it is more obvious.</p> <p>10, summary</p> <p>Struts is an excellent J2EE MVC architecture. It uses Taglib to get reusable code and abstract Java code, and use the ActionServlet to match Struts-Config.xml to achieve the entire system navigation. Enhanced developers's overall grasp of the system, improved the maintenanceability and expandability of the system.</p> <p>11, reference</p> <p>1, "struts-documentation"</p> <p>2. "Current popular J2EE Web Application Architecture" Gong Yongsheng IBM Developer Website</p> <p>3, "Struts, MVC an open source implementation with this servlet and JSP framework to manage complex large websites" Malcolm G. Davis IBM Developer Website</p> <p>4, "Jakarta Struts Book Review Project" Chuck Cavarative TherevelSide</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-74350.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="74350" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.055</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'BXp_2BLAtZViueUPfzwPdExLnSWeD_2Ftg1CgSDLiNVQbT2aXIP_2BR2e6QHNmpREh5I_2FAT3JhjeWVhTiJnb9uen_2FTZg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>