Struts in action

xiaoxiao2021-03-06  63

Struts in action

This simple exercise will show the following: l Create an HTML form; L from the HTML form to get the input (Logical logic); L According to dynamic input, change the control flow; to complete this program, you need to build: l An actionforml Actionl struts-config.xml file l That's three pages! 1.1.1.1 Creating an Actionform ActionForm is a JavaBean that extends the org.apache.struts.Actionform class. This object captures the input by requesting. When the browser submits a form, it creates a parameter for the fields in each form in the request. ActionForm has a corresponding attribute for the fields in each HTML form. The ActionServlet matching the parameters in the request and the properties in the ActionForm. When the match is matched, the ActionServlet calls the setter method and enters the value in the request. In our exercise, the UserName field in the form requires a setUserName (String) method. The Password field requires SetPassword1 (String) and SetPassword2 (String) method. These methods are responsible for assembling instance variables hidden in RegisterForm JavaBean. The source code of RegisterForm is displayed in Listing 1. package app; import org.apache.struts.action *;. public class RegisterForm extends ActionForm {protected String username; protected String password1; protected String password2; public String getUsername () {return this.username;}; public String getPassword1 () {return this.password1;}; public String getPassword2 () {return this.password2;}; public void setUsername (String username) {this.username = username;}; public void setPassword1 (String password) {this.password1 = password ;}; public void setpassword2 (string password) {this.password2 = password;};} code list error! There is no text in the document. .1 RegistrationForm

Create a file, named Registerform.java, the content is shown in the code list 1.1. Stored under / WebApps / register / web-inf / class. By default, may be C: / Program Files / Apache Tomcat 4.0. For other containers, use their Classes directory path to deploy our Register program. 1.1.1.2 Creating a Java class with a Java class and expanding org.apache.struts.Action. ActionServlet is assembled, and then passes it to the action. Action is typically responsible for entering the check, access business information, and deciding which ActionForward returns to servlet. Now, create a file, named registeraction.java, its content is the content list 1.2: package app; import org.apache.struts.action. *; Import javax.servlet.http. *; Import java.io. * ; public class RegisterAction extends Action {public ActionForward perform (ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) {// ①Cast the form to the RegisterFormRegisterForm rf = (RegisterForm) form; String username = rf.getUsername (); String password1 = rf.getPassword1 (); String password2 = rf.getPassword2 ();. // ②Apply business logicif (password1.equals (password2)) {try {// ③Return ActionForward for successUserDirectory.getInstance () setUser (username, password1); return mapping.findForward ( "success");} catch (UserDirectoryException e) {return mapping.findForward ( "failure");}} // ④Return ActionForward for failurereturn mapping.findForward ( "failure");}} Listing error! There is no text in the document. .2 registeraction.java

Store files in the / WebApps / register / web-inf / class of the file. Although it is very simple, our registerAction has been a typical thing for Action. At 1, the input ActionForm is converted to RegisterForm. We can get the contents of UserName, Password1, and Password2. If the password matches two times, we add the user to the userDirectory 3 and return to the ActionForward corresponding to the Success. UserDirectory is a Helper class that records usernames and passwords to a standard properties file. Otherwise, return an actionforward corresponding to Failure. When we create a struts-config file next step, we will identify an ActionForward object representative of Success and Failure. Note: Struts 1.1 provides another entered method called Execute. This method provides better accidents, but not the same as the Struts 1.0's Perform method. Here we will use the Perform method to make our examples can run under two versions. 1.1.1.3 Creating a struts-config.xml file contains the ActionServlet to process details for application requests. To practice, we create an empty Struts-Config.xml file. What you need to do is to fill in some details. The file is stored in the / WebApps / Register / Web-INF / directory, which needs to be changed: First, add / register to the Path property of the element. ActionServlet uses a web container forward to its URI to select the correct Action class. URI and ActionMApping Path properties match. Here, the path requested must match the prefix and the suffix and / register. Prefix or suffix is ​​usually / do / or .do. In our exercise, set the suffix to .do. When the URI has a .do extension, the container knows to forward the request to the ActionServlet. Struts will automatically remove the extension, so we don't have to add them when configured.

The next step adds the Name property of the registerform to the element. Element Use the Name property to identify which ActionForm will be created and assemble the submitted form to him.

Then, add the app.registerAction to the TYPE property of the element. The ActionServlet uses this property to identify the Action class that will be used to process the request.

Next, under the element, add Success to the Name property, and /success.html to the Path property. Finally, add Failure to Name properties, / failure.html to the Path property in another . These elements will create an ActionForward object, we will use it to select the program's control flow. The element defines the association between the logical name used in the RegisterAction. Struts-config.xml Source code See code 1.3. < Action-mapings> code list error! There is no text in the document. .3 Struts-config.xmlstruts framework treats the struts-config.xml file as deployment descriptor. It allows us to create and change the association of actionmapping and paths without recompiling Java classes. We can also change the connection between the page without changing the JSP template. 1.1.1.4 Creating a page The last step is to create success.html, failure.html, and Register.jsp page. The source code of 3 files is as follows, see the code list 1. 4, 1. 5, 1.6. Store in the / WebApps / register directory. Success </ title> </ head> <body> registration successded! <P> <a href="register.jsp"> Try Another? </a> </ p> </ Body> </ html> code list error! There is no text in the document. .4 Success HTML</p> <p><Html> <head> <title> failure </ title> </ head> <body> registration failed! <P> <a href="register.jsp"> try again? </A> </ p> </ Body> </ html> code list error! There is no text in the document. .5 failure.html <% @ Taglib URI = "/ Web-inf / struts-form.tld" prefix = "form"%> <form: form: <form: text property = "username" /> <br> ENTER password: <form: password property = "password1" /> <br> RE-ENTER password: <form: password property = "password2" /> <br> <form: submit value = "Register" /> </ form: form> code list error! There is no text in the document. .6 register.jsp</p> <p>At this time, all work to build a simple Struts application is done. Now, try to run.</p> <p>If Tomcat is not run, start it. Enter the following address in your browser: http: // localhost: 8080 / register / register.jsp</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-84708.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="84708" 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.039</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 = '5ZocEKsbgY55zzC9iOAobJwnGTPgENaYZhsJnZh1B0SV5S6BmlcEgzlPXV_2B_2FZSknOv7MG9LTVYoqnNOWY27oUQ_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>