The relationship between the Struts programs are all files (1)

zhaozj2021-02-16  57

The relationship between the Struts program

The relationship between the "How to achieve the simplest Struts program" example in each file and the source code analysis are described.

[Related Links]

"JavaWebstudio Series Development Tools Visaul Struts Version" http://dev.9cbs.net/develop/Article/28/28871.shtm

"How to achieve the simplest Struts program" http://dev.9cbs.net/develop/Article/28/28946.shtm

"The implementation of the Struts drop-down box" http://dev.9cbs.net/develop/Article/28/28956.shtm

"The implementation of the Sruts radio box" http://dev.9cbs.net/develop/Article/29/29042.shtm

"Struts check box" http://dev.9cbs.net/develop/Article/29/29043.shtm

Development Environment: Java Web Studio Series Development Environment Visual Strutst Version.

The latest JavaWebStudio download address: ftp: //210.36.64.79/kui

[background knowledge]

Struts uses the MVC structure; the logic diagram shown in Figure 2.1 describes the high-level structure of the frame.

We now discuss each element in the figure.

A representation of a Struts-based application (VIEW) is built using the Struts Tag Library (Taglibs). Request from the customer is passed to the servlet called ActionServlet, which uses the Struts application, all requests that need to pass through the ActionServlet. This ActionServlet passes data from request to Actionform JavaBean.

ActionForm is a JavaBean that represents data from a form view component. These forms are generated by JSP with the Struts HTML tag library. This BEAM is generated by ActionServlet, with a request parameter, which also requests ActionForm to verify the legality of the user's submit data.

ActionServlet is configured by defining a set of actionMApping. An ActionMApping is an object that maps the URL in the request to the components of the process requested by the application developer. The configuration of ActionServlet and ActionMapping is done in the XML configuration file.

A dedicated application is used to handle the components of the request called an Action class. In the MVC structure, they represent models. They may be used to verify the legality of the user input. If an application error occurs during the information processing, the Action class can create an instance of an Error object, and then save the HTTP Request object. If the logic in the Action class is successfully passed to the Controller to deliver an ActionForward object, represent the JSP of the desired depiction response. There are two types of ActionForward: dedicated to a specific Action class or global forwarding (any Action class can transfer these ActionForwards to Controller).

The relationship between this main file:

The beginners often don't know how helloword.jsp, hellowordform.java, hellowordaction.java, how to associate together, in fact they are implemented by struts-config.xml configuration. These configurations are a difficult point in the Struts program design. Fortunately, in the Visual Struts of Java Web Studio, all of these configurations are automatically completed in the Struts file wizard.

[Key Source Code Analysis]

1 Enter the JSP page file

Entering the JSP page file name is Hello.jsp, which is mainly implemented with a form with a text box, and the file code is as follows:

<% @ Page ContentType = "Text / HTML; Charset = GB2312" Language = "Java"%> <% @ Taglib URI = "/ Web-INF / STRUTS-Bean.TLD" prefix = "bean"%>

<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>

<% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%>

<% @ Taglib URI = "/ Web-INF / STRUTS-TEMPLATE.TLD" prefix = "template"%>

</ title></p> <p><HTML: BASE /></p> <p></ hEAD></p> <p><body bgcolor = "white"></p> <p><html: form action = "/ hellowordaction.do" Method = "POST"></p> <p><HTML: Text Property = "MyBeanVariable1" /></p> <p><html: submit value = "Submit" /></p> <p><html: reset value = "rewrite" /></p> <p></ html: form></p> <p></ body></p> <p></ html: html></p> <p>The first is to define this page as support Chinese display, Chinese encoding uses GB2312 format, program language to Java:</p> <p><% @ page contenttype = "text / html; charset = GB2312" Language = "java"%></p> <p>Then define four Struts tags beans, html, logic, and template, the labels that are not used in this file can remove the corresponding definition, if you need to use a custom label in the file, you also need to add the corresponding definition here. :</p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-TEMPLATE.TLD" prefix = "template"%></p> <p><html: html> Never said. <html: base /> label corresponds to the <base> tab of the HTML, used to set the absolute path of the file in the betler, and then write the relative position of the file in the file, when the board is detected These locations are automatically attached to the absolute path to become a complete path. The URL in all relative address forms in the document is in terms of utrl defined here. The <base> tag in a document cannot be more than one, must be placed in the <head> tag, and should be defined before any statement containing the URL address. <html: form> The black body is used here, indicating that it is necessary to focus. <html: form action = "/ helloword action.do" Method = "post"></p> <p><HTML: Text Property = "MyBeanVariable1" /></p> <p><html: submit value = "Submit" /></p> <p><html: reset value = "rewrite" /></p> <p></ html: form></p> <p><html: form> tab defines a form, the target URL when submit is "/helloaction.do", the method "POST", the POST method is to include the data filled in the form in the form, together In the handler on the server, the 10,000 Method has no character limit, which contains the character set of ISO10646, is a mailing, which does not display information submitted in the address bar of the betler, this uniform transmission The data is not limited. Of course, you can also select the GET method. The GET method is to attach the form content behind the URL address, so the length of the submission information is limited, and the maximum can not exceed 8192 characters. If the information is long, it will be intercepted, resulting in the meaning Thinking of the results, and cannot transmit non-ASCII code characters. The form contains a text input box <html: text>, which corresponds to the variable of the property "mybeanvariable1" in the bean. If the MyBeanVariable1 pays the initial value, the default is displayed in the text box when starting this page. MyBeanVariable1 initial value. In addition, the form also includes two most common commit buttons <HTML: Submit value = "submit" /> and overwritten buttons <html: reset value = "rewrite" />.</p> <p>2 Output JSP page file</p> <p>Output JSP page file name is HelloWordout.jsp, which is mainly implemented for the value of the text box submitted by the file, the file code is as follows:</p> <p><% @ page contenttype = "text / html; charset = GB2312" Language = "java"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%></p> <p><html: html></p> <p><head> <title> </ title></p> <p><HTML: BASE /></p> <p></ hEAD></p> <p><body bgcolor = "white"></p> <p><html: html></p> <p><HEAD></p> <p><title> </ title></p> <p><HTML: BASE /></p> <p></ hEAD></p> <p><body bgcolor = "white"></p> <p><bean: write name = "HelloWordform" property = "mybeanvariable1" /></p> <p></ body></p> <p></ html: html></p> <p></ body></p> <p></ html: html></p> <p>The output file is unique to the previous input file is to turn the <html: form> tag into the <bean: write> tag:</p> <p><bean: Write Name = "Helloform" property = "mybeanvariable1" /></p> <p><bean: Write> Tag Implements the output of the bean property value, where Name saves the bean name, here is "Helloform", can be seen from the back configuration file, its corresponding class is a Helloform class; Property Save the property name, here is " MyBeanVariable1, ", with the property name that is taken with the text box of the input file.</p> <p>3 actionform bean file</p> <p>The Hello program's ActionForm bean file implementation contains the bean that contains a variable. The file name is HelloWordform.java, the file code is as follows:</p> <p>Package EmptyPRJ;</p> <p>Import javax.servlet.http.httpservletRequest;</p> <p>Import org.apache.struts.Action.Actionerror;</p> <p>Import org.apache.struts.Action.Actionerro;</p> <p>Import org.apache.struts.Action.actionform;</p> <p>Import org.apache.struts.action.actionmapping;</p> <p>Public Final Class HelloWordForm Extends Actionform</p> <p>{</p> <p>Private string mybeanvariable1;</p> <p>Public string getMyBeanvariable11 ()</p> <p>{</p> <p>Return (this.mybeanvariable1);</p> <p>}</p> <p>Public void setMyBeanvariable1 (String MyBeanvariable1)</p> <p>{</p> <p>THIS.MYBEANVARIABLE1 = MyBeanVariable1;</p> <p>}</p> <p>}</p> <p>The first is to define the Package name:</p> <p>Package EmptyPRJ;</p> <p>This is the default package name EMPTYPRJ for JSPSTUDIO, and it is necessary to note that the package name corresponds to the directory name. Then introduced five types of HTTPSERVLETREQUEST, ActionError, ActionerRors, ActionForm, ActionMApping, which are commonly used in Actionform Beans, is automatically joined by jspstudio. In fact, the HelloWordForm class is only used for Actionform classes, and other parts can be deleted, but here keep it to facilitate Use it after the extension program.</p> <p>Import javax.servlet.http.httpservletRequest; import org.apache.struts.Action.Actionerror;</p> <p>Import org.apache.struts.Action.Actionerro;</p> <p>Import org.apache.struts.Action.actionform;</p> <p>Import org.apache.struts.action.actionmapping;</p> <p>Next, hellowordform, a parameter MyBeanVariable1 and its corresponding GET (), set () function, which is exactly the same as the normal bean defined.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-25859.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="25859" 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.045</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 = 'DAmJ4fhLdGH1dR06U3NgcNmTZiC4QP75JlxK8qxevuni0h1_2FFzhwdkl4MsggodrzkdIcwjNa173NTM6fnXt9BA_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>