Instance Struts (transfer)

xiaoxiao2021-03-06  126

[Struts] instance learning struts fast Dad's bear @ 2004-07-31 14:27 Transfer from http://vip.6to23.com/hanson/htdocs/struts.htm Select pure JSP or pure servlet design site has it Limitations, Struts is a powerful tool that links them together. The Struts can develop MVC mode-based applications, and the concept of MVC can see GOF's "design mode - the basis for object-oriented software". What you have to do now is, download, install, configure the following tools, version different words can be different, see their documents: Tomcat 4.1.24 apache 2.0.43, w / mod_jk2 2.0.43 java 2 SDK Standard Edition 1.4.0 Struts 1.1 Eclipse 2.1.0 Struts is written with Java, which requires JDK 1.2 or higher. If you use JDK 1.4, just like me, XML Parser and JDBC 2.0 Optional package binary have been included in the default. New projects In this routine we have to develop a simple web application, allowing users to log in and log out. Simply put, the data is set to constants, not in the database, after all, Struts is Struts, not Java. First create a directory in your Tomcat Configuration, for example, logoApp. Create a directory SRC and Web-INF in LogonApp, create directory classs and libs in Web-INF, copy struts.jar to lib directory from Struts distribution, and copy $ catalina_home / common / lib / servlets.jar to LiB directory. Copy all struts * .tld to the web-inflicity from the Struts distribution. Now open Eclipse, you will see four views. Now we have a new project, click File -> New Project, open a window, select Java in the first pane, select Java Project in the second pane, click Next. Enter the project name (for a good memory, you are also called LogonApp), remove the check box of the USE DEFAULT check box, browse to the logoApp directory, click Next. A new window appears, click Add Folder on the Source Tab, add $ app_base / src, fill in $ app_base / web-inf / classes in Default Output Folder, click Finish. Click on Window -> Open Perspective -> Resource to see if the .project file has automatically contain all JAR files in the lib directory. Your logoApp / web-inf / web.xml should look like this:

action org.apache.struts.Action .ActionServlet config /web-inf/struts-config.xml 1 action < URL-PATTERN> *. do index.jsp /web-inf/struts-bean.tld / Web-inf / struts-bean.tld /Web-inf/struts-html.tld /web-inf/struts-html.tld /web-inf/struts-logic.tld /web-inf/struts-logic.tld struts profile logoApp / web-inf / struts-config.xml :

<

Message-resources parameter = "org.monotonous.struts.ApplicationResources" /> Creating View Now returns to Eclipse, create a new page index.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 "%> <bean: message key =" index.title "/> </ title> <html: base /> </ head> <body> <html: errors /> <html: form action = "/ logon> <bean: message key =" prompt.username "/> <html: text property =" username "/> <br /> <Bean: message key = "prompt.password" /> <html: password proteth = "password" /> <html: submit> <bean: message key = "index.logon" /> </ HTML: Submit> </ html: form> </ body> </ html: html> Successfully logged in page main.jsp:</p> <p><% @ 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"%> <html: html> <head> <title> <bean: message key = "main.title" /> </ Title> <html: base /> </ head> <body> <html: link forward = "logoff"> <bean: message key = "main.logoff" /> </ html: link> </ body> </ HTML: HTML> You may notice that these two pages use the convenient and international feature, this requires at least one default properties file ApplicationResources.properties:Index.title=Struts HomePage Prompt.userName = PASSWORD Index.logon = log on main.title = struts main page main.logoff = log off error.password.mismatch = invalid username and / or password. Create Controller LogonAction.java:</p> <p>package org.monotonous.struts; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action. Action; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache .struts.action.ActionMapping; import org.apache.struts.util.MessageResources; import org.apache.commons.beanutils.PropertyUtils; public final class LogonAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {Locale locale = getLocale (request); MessageResources messages = getResources (request); // Validate the request parameters specified by the user ActionErrors errors = new ActionErrors ( ); String username = (String) PropertyUtils.getSimpleProperty (form, "username"); String password = (String) PropertyUtils.getSimpleProperty (form, "password");! If ((username = "foo") || (password ! = "BAR")) Errors.add (actionerrors.global_error, new actionerror ("error.password.mismch")); // report any errors we have attovered back to the Original Form If (! errors.Isempty ()) {SaveError (Request, Errors); Return (mapping.getinputforward ());} // save our logged -in user in the session httpsession session = request.getations ();</p> <p>// DO Something with sessions ... // Remove the obsole form bean if (mapping.getattribute ()! = Null) {if ("request" .Equals ()) Request.removeAttribute (mapping.getattribute ()); else session.removeAttribute (mapping.getAttribute ());} // Forward control to the specified success URI return (mapping.findForward ( "success"));}} LogoffAction.java:package org.monotonous.struts ; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache .struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.util.MessageResources; public final class LogoffAction extends Action {public ActionForward execute (ActionMapping Mapping, Actionform Form, HttpServletRequest request, HttpServletResponse response) throws Exception {Locale locale = getLocale (request); MessageResources messages = getResources (request); HttpSession session = request.getSession (); session.removeAttribute ( "userattrib"); session.invalidate (); / / Forward Control to the specified success uri return ("success"));}} Appreciate it in the browser, but when you can't open champagne, maybe you should consider some security measures for this application. Let me talk about it next time.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-127037.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="127037" 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 = '1ggohJbelNLSdKRVb_2BGWHk3Z1GfrJhNLwfdLoVSb_2BJsgLmzdnxNlmlaMkHd3Sb0D_2BoWIKr3Tov4HWANO72VelA_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>