Instance Struts

zhaozj2021-02-16  58

Choosing pure JSP or a pure servlet design site has its limitations, and 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 may be some differences, specific to their documentation:

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 in Java, which should require 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 logonapp / web-inf / web.xml should look like this: action org.apache.struts.Action.ActionServlet config / web-inf /struts-config.xml 1 action *. 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 logo pAPP / Web-INF / STRUTS-Config.xml is as follows:

- // Apache Software Foundation // DTD STRUTS Configuration 1.1 // en "" http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">

<@ js / action> Create View Now returned 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 proteth = "username" /> <bean: message key = "prompt.password" /> <html: password property = "password" /> <br / > <html: Submit> <bean: message key = "index.logon" /> </ html: Submit> </ html: form> </ body> </ html: html> Successfully logged in page main.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"%> <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 convenient and international characteristics, at least one default properties file ApplicationResources.properties :index.title=struts HomePage Prompt.usename = USE PROMPT.USERNAME rname prompt.password = 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 (ActionerRRORS. GLOBAL_ERROR, new ActionError ( "error.password.mismatch")); // Report any errors we have discovered back to the original form if) {saveErrors (request, errors) (errors.isEmpty (!); return (mapping.getInputForward ());} // save our logged -in user in the session httpsession session = request.getations (); // DO Something with session ... // remove the obsolete formle i (mapping.getattribute ()! = NULL) {IF ("Request" .Equals (mapping.getscope ())))))); mapping.getattribute ());</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-19948.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="19948" 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 = 'yXerPLxCoIPfmcOsDcyCCjgf0Ke2gIFcOlAX3vYhIYJGg6Y9qopgYFZfDHP1tGDRyG3bgWFcyr5_2FO_2BCcq3lYQQ_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>