Article 5: Struts small test cattle knife

zhaozj2021-02-16  45

Serious warning: If you skip the upper article, I am not responsible for any problems you encountered in this article.

Opening white: First of all, I hope that you understand how Struts work. On the JSP page, when we have submitted actions (you understand when the button is pressed), the page is data (such as: text box, check box, radio box, drop basket, etc.) Automatically write into the Form, which is also obtained from Form when the page needs data. Action can obtain relevant data from the Form and can be processed, and the results of the processing are also placed in the Form.

I used to define a text box like this:

Now use Struts label library, we define a text box like this

The text box that is defined, when the submit button on the page is pressed, the content in this text box is automatically placed in the FROM, and is in a str1 property of the Form, for this, Form will define GET method and set method for this attribute

Use example to explain the problem:

The name of the project is called: User

(What hope you know where to put your user, what is in the user)

The appearance of this example is this, first of all the first page has two text boxes: enter your last name (firstname) and your name (Lastname), and a "submit" button, when pressing the button At the time, you will call another page to display the name of the name and name. If the last name or name has a "submit", turn to another warning page. I don't know if I said, "Demand" is not, let's take a look at the effect.

What do you need: Please put all the things below 1 and 2 in Web-INF, let them walk side by side, sit in flat, and in the future program, I don't say it, please consciously put them in you. Application under Web-INF

1: Struts label library file, these things if you can't find it, please give me email.

Struts-bean.tld, struts-config.xml.bak, struts-html.tld, struts-logic.tld, struts-nested.tld, struts-tiles.tld, struts-tiles.tld

2: Two XML configuration files

The content of web.xml is as follows, if I don't specifically explain it later, the content of web.xml is accurate.

Public "- // Sun microsystems, Inc.//dtd Web Application 2.3 // en"

"http://java.sun.com/dtd/web-app_2_3.dtd">

Action

- struts1.0.x: an actioncomponentservlet

- Struts1.1: ActionServlet- No Struts: TilesServlet

->

org.apache.struts.tiles.ActioncomponentServlet ->

org.apache.struts.Action.ActionServlet

org.apache.struts.tiles.tilesservlet ->

Specify Configuration File Names. There Can Be Several CommA

Separated File Names

->

definitions-config

/web-inf/tiles-defs.xml,

/Web-inf/tiles-defs.xml

Specify Tiles Debug Level.

O: no debug information

1: Debug Information

2: More debug information

->

definitions-debug

1

Specify Digester Debug Level. This value is passed to Digester

O: no debug information

1: Debug Information

2: More debug information

->

definitions-paser-details

0

Specify if Xml Parser SHOULD VALIDATE The TILES Configuration File.

True: Validate. DTD SHOULD BE Specified in File HEADER.

False: No Validation

->

definitions-paser-validate

True

config

/Web-inf/struts-config.xml

validate

True

debug

2

detail

2

2

Action

*. do

60

Login.jsp

/web-inf/struts-tiles.tld

/web-inf/struts-tiles.tld

/web-inf/struts-html.tld

/web-inf/struts-html.tld

/web-inf/struts-bean.tld

/web-inf/struts-bean.tld

/web-inf/struts-logic.tld

/web-inf/struts-logic.tld

ZCHFIVE DATABASE Connection Pool

JDBC / ZCHFIVEDB

javax.sql.datasource

container

The contents of Struts-Config.xml are as follows, and the content of this configuration is different depending on your application, but the format will be the same.

"- // Apache Software Foundation // DTD Struts Configuration 1.1 // En"

"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

This Is The Struts Configuration File for the Example Application,

Using the proposed New Syntax.

NOTE: You Would Only Flesh Out The Details in The "Form-Bean"

Declarations if you had a generator Tool That Used Them To create

The Corresponding Java Classes for you. OtherWise, you would

Need Only the "form-bean" Element itself, with the corresponding

"Name" and "type" attributes.

->

Type = "useeraction"

Name = "Userform"

Scope = "request"

Validate = "false">

Enter the page page

in.jsp

<% @ Page ContentType = "text / html; charset = GB2312"%>

<% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%>

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

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

<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>

login </ title></p> <p></ hEAD></p> <p><body></p> <p><center></p> <p><pre></p> <p><html: form action = "execute.do" Method = "post"></p> <p><h3> Please enter </ h3></p> <p>Firstname: <html: text proteth = "str1" size = "12" /></p> <p>Lastname: <html: text property = "str2" size = "12" /></p> <p><html: submit value = "Submit" /> <html: reset value = "reset" /></p> <p></ html: form></p> <p></ pre></p> <p></ center></p> <p></ body></p> <p></ html: html></p> <p>Display the correct result</p> <p>Success.jsp <% @ Page ContentType = "Text / HTML; Charset = GB2312"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%></p> <p><html></p> <p><center></p> <p>Your name: <bean: Write name = "userform" property = "stradd" /></p> <p></ center></p> <p></ html></p> <p>Tip warning page</p> <p>Error.jsp</p> <p><% @ Page ContentType = "text / html; charset = GB2312"%></p> <p><html></p> <p><center></p> <p>Error! You didn't enter firstname or lastname.</p> <p></ center></p> <p></ html></p> <p>Ok, let's go to the programmer to do the job, that is, write the Java code.</p> <p>First, don't ask where to write Java code</p> <p>Second, don't ask what way to compile the Java code into class files.</p> <p>Third, don't ask where Java files and class files are placed</p> <p>... I won't answer any more such a problem.</p> <p>Don't forget to import two JAR packages.</p> <p>FORM used to save data</p> <p>Userform.java</p> <p>Import org.apache.struts.action. *;</p> <p>Public Class Userform Extends ActionForm</p> <p>{</p> <p>PRIVATE STRING STR1 = NULL;</p> <p>PRIVATE STRING STR2 = NULL;</p> <p>PRIVATE STRING STRADD = null;</p> <p>Public String getStr1 ()</p> <p>{</p> <p>RETURN STR1 == NULL? ": str1.trim ();</p> <p>}</p> <p>Public void setStr1 (String str1)</p> <p>{</p> <p>THIS.STR1 = STR1;</p> <p>}</p> <p>Public String getStr2 ()</p> <p>{</p> <p>RETURN STR2 == NULL? ": str2.trim ();</p> <p>}</p> <p>Public String getstradd ()</p> <p>{</p> <p>Return stradd;</p> <p>}</p> <p>Public void setstr2 (String STR2)</p> <p>{</p> <p>THIS.STR2 = STR2;</p> <p>}</p> <p>Public void setstradd (String stradd)</p> <p>{</p> <p>THIS.STRADD = STRADD;</p> <p>}</p> <p>}</p> <p>I saw FORM, there is nothing, is some attributes and GET and SET methods about attributes.</p> <p>Action used to process data</p> <p>UserAction.java</p> <p>Import javax.servlet.http.httpservletRequest;</p> <p>Import javax.servlet.http.httpservletResponse;</p> <p>Import org.apache.struts.Action.Actionerro;</p> <p>Import org.apache.struts.Action.actionform;</p> <p>Import org.apache.struts.Action.actionForward;</p> <p>Import org.apache.struts.action.actionmapping;</p> <p>Import org.apache.struts.Actions.dispatchaction;</p> <p>Public class useraction extends dispatchaction {</p> <p>Public ActionForward Execute (ActionMapping Mapping,</p> <p>Actionform Form,</p> <p>HTTPSERVLETREQUEST REQUEST,</p> <p>Httpservletresponse response</p> <p>Throws Exception</p> <p>{</p> <p>// Get page form information</p> <p>Userform uform = (userform) Form;</p> <p>/ / Get two data already submitted from the FROM</p> <p>String Temp_str1 = uform.getstr1 ();</p> <p>String Temp_str2 = uform.getstr2 ();</p> <p>String Temp_stradd = TEMP_STR1 TEMP_STR1;</p> <p>// put the result of the process in Form</p> <p>uform.setstradd (TEMP_STRADD);</p> <p>IF (! "". Equals (TEMP_STR1) &&! "" "" "Equals (Temp_Str2))</p> <p>{</p> <p>Return mapping.findforward ("ok");</p> <p>}</p> <p>Else</p> <p>{</p> <p>Return mapping.findforward ("ERR");</p> <p>}</p> <p>}</p> <p>}</p> <p>Hurry and see your results:</p> <p>Start your Tomcat again, check your IE address:</p> <p>Http: // localhost: 8080 / user / in.jsp</p> <p>Tips you pay attention to a few places</p> <p>1 in Struts-Config.xml</p> <p><action path = "/ execute"</p> <p>Type = "useeraction"</p> <p>Name = "Userform" ............ Execute UseerAction Userform</p> <p>Respectively</p> <p>In.JSP <html: form action = "execute.do" Method = "post"> Execute.do</p> <p>Java class uSerction.class</p> <p>"form-bean name =" userform "in Struts-config.xml ......... Userform</p> <p>"bean: Write name =" userform "in Success.jsp is Userform ...</p> <p>2 in Struts-Config.xml</p> <p><form-bean name = "Userform" type = "userform"> Userform</p> <p>with</p> <p>Java class Userform.class</p> <p>3 See it in Form</p> <p>STR1, STR2, STRADD?</p> <p>I found their shadows in in in .jsp and success. JSP?</p> <p>Understand?</p> <p>Oh, it turned out to be like this. In.jsp puts the value of Str1 and Str2 to the Form, then Aciton uses the GET method to take them out to process them, then put the result through the set method to the Form's stradd, and finally Success.jsp Take it out again to see you, is this? ? ! !</p> <p>This is the process, and FORM has a "warehouse" that saves data (this saved is not permanent), Action has become a "factory" that handles and processes data. They jointly provide a full service of the data to the JSP page of the front desk. Ok, I have to say it, about this example, I have to tell you so much. Oh, forgot, embarrassed. Why do the Execute method in the action when the page is pressed? Haha, this is the advantage of struts! Is it "MYDOG" in this way to this method? Now you can't, I will tell you how you can tell you. .</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-20953.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="20953" 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.051</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 = 'Z3DOblUHcKasjzKXhQL4hSPsOwRWQtKcmzG6fi5JcTR655KOs_2FXBW3QJW0kTTdaoQnJRJZG8fooht057wcEi1Q_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>