Instance Struts

xiaoxiao2021-03-06  56

Instance Struts

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 with Java, which should be a 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 project

In this routine, we have to develop a simple web application that allows 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:

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

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

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 logoApp / 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">

Type = "org.apache.struts.validator.dynavalidatorform">

TYPE = "org.monotonous.struts.logonaction"

Name = "logonform"

SCOPE = "session"

INPUT = "Logon">

Type = "org.monotonous.struts.logoffaction">

Local or Global "Forward" Rather Than Module-Relative Path ->

Create View

Now return to Eclipse to 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 /></p> <p></ hEAD></p> <p><body></p> <p><html: errors /></p> <p><html: form action = "/ logon"></p> <p><bean: message key = "prompt.username" /></p> <p><HTML: Text Property = "UserName" /></p> <p><br/></p> <p><bean: message key = "prompt.password" /></p> <p><html: Password property = "password" /></p> <p><br/></p> <p><HTML: Submit></p> <p><bean: message key = "index.logon" /></p> <p></ html: Submit></p> <p></ html: form></p> <p></ body></p> <p></ html: html></p> <p>Successfully logged in with page main.jsp:</p> <p><% @ page contenttype = "text / html; charset = uTF-8" 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><html: html></p> <p><HEAD></p> <p><title> <bean: message key = "main.title" /> </ title></p> <p><HTML: BASE /></p> <p></ hEAD></p> <p><body></p> <p><html: link forward = "logoff"></p> <p><bean: message key = "main.logoff" /></p> <p></ html: link></p> <p></ body></p> <p></ html: html></p> <p>You may notice that these two pages use the convenient and international characteristics, this requires at least a default properties file ApplicationResources.properties:</p> <p>Index.title = Struts HomePage</p> <p>Prompt.username = username</p> <p>Prompt.password = password</p> <p>Index.logon = LOG ON</p> <p>Main.title = Struts main page</p> <p>Main.logoff = log off</p> <p>Error.Password.mismatch = invalid username and / or password.</p> <p>Create Controller</p> <p>LogonAction.java:</p> <p>Package org.monotonous.struts;</p> <p>Import java.util.locale;</p> <p>Import javax.servlet.http.httpservletRequest;</p> <p>Import javax.servlet.http.httpsession;</p> <p>Import javax.servlet.http.httpservletResponse; import org.apache.struts.Action.action;</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.actionForward;</p> <p>Import org.apache.struts.action.actionmapping;</p> <p>Import org.apache.struts.util.MessageResources;</p> <p>Import org.apache.commons.beanutils.propertyUtils;</p> <p>Public Final Class LogonAction Extends Action {</p> <p>Public ActionForward Execute</p> <p>ActionMapping mapping,</p> <p>Actionform Form,</p> <p>HTTPSERVLETREQUEST REQUEST,</p> <p>Httpservletresponse response</p> <p>Throws exception {</p> <p>Locale locale = getLocale (request);</p> <p>MessageResources Messages = GetResources (Request);</p> <p>// Validate the Request Parameters Specified by The User</p> <p>ActionerroS Errors = new actionerrors ();</p> <p>String username =</p> <p>(String) PropertyUtils.getsimpleProperty (Form, "UserName");</p> <p>String password =</p> <p>(String) PropertyUtils.getsimpleProperty (Form, "Password");</p> <p>IF ((UserName! = "foo") || (Password! = "bar")))</p> <p>Errors.Add (actionerroS.Global_ERROR,</p> <p>New ActionError ("Error.Password.mismatch))));</p> <p>// Report Any Erroors We Have Discovered Back to the Original Form</p> <p>IF (! errors.isempty ()) {</p> <p>SaveerRors (Request, Errors);</p> <p>Mapping.getInputForward ());</p> <p>}</p> <p>// Save Our Logged-in User In the session</p> <p>HttpSession session = request.getations ();</p> <p>// Do Something with sessions ...</p> <p>// Remove the Obsolete form bean</p> <p>IF (mapping.getattribute ()! = null) {</p> <p>IF ("REQUEST" .Equals (mapping.getscope ()))))</p> <p>Request.Removettribute (mapping.getattribute ());</p> <p>Else</p> <p>Session.Removettribute (mapping.getattribute ());</p> <p>}</p> <p>// forward control to the specified surcess URI</p> <p>Mapping.Findforward ("Success"));</p> <p>}</p> <p>}</p> <p>Logoffaction.java:</p> <p>Package org.monotonous.struts;</p> <p>Import java.util.locale;</p> <p>Import javax.servlet.http.httpservletRequest;</p> <p>Import javax.servlet.http.httpsession;</p> <p>Import javax.servlet.http.httpservletResponse;</p> <p>Import org.apache.struts.Action.action;</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.util.MessageResources;</p> <p>Public final class logoffaction extends action {</p> <p>Public ActionForward Execute</p> <p>ActionMapping mapping,</p> <p>Actionform Form,</p> <p>HTTPSERVLETREQUEST REQUEST,</p> <p>Httpservletresponse response</p> <p>Throws exception {</p> <p>Locale locale = getLocale (request);</p> <p>MessageResources Messages = GetResources (Request);</p> <p>HttpSession session = request.getations ();</p> <p>Session.RemoveAttribute ("Userattrib");</p> <p>session.INVALIDATE ();</p> <p>// forward control to the specified surcess URI</p> <p>Mapping.Findforward ("Success"));</p> <p>}</p> <p>}</p> <p>Let's appreciate it in your browser, but when you can't open champagne, maybe you should consider some security measures for this app, next time I will talk.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-118153.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="118153" 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.046</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 = 'HyXS_2FI6BoOKKh5Kz5HAZlj_2BhFL1prC5_2F5fPF5KkIXu4MK3_2FQ86bJbP1VBg_2BmInYp1jMvOEMrKnA_2FfBq1'; 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>