Struts's first experience

zhaozj2021-02-11  221

Struts's first experience

Baitianhai baichenhong@hotmail.com

Development Environment WINDOW2000 JBuilder7 Tomcat4.1.12

First, download jakarta-struts-1.0.2.zip from http://jakarta.apache.org, there are two main directories lib and webapps after unwrapping, in the lib directory, use Struts to create applications, There are several suffixes under the webapps directory, familiar with Java Web development is more clear, put them under Tomcat / WebApps / Directory, and then restart Tomcat, Tomcat will deploy these web projects. Struts-Documentation.war is a document, struts-example.war is a good example, I mainly refer to this example. Then establish the struts directory in the JBuilder installation directory / lib / down, go in the things under the above lib directory, then start JBuilder7, run the menu Tools-> configure-> libraries-> new-> name Enter Struts, then select Just COPY The past struts.jar file, then OK.

Second, newly established a project, refer to struts, server selection Tomcat4, then build a jsp1.jsp file, then put all the .tld files under the lib / directory to your created project directory / default / Web-infault / Directory Below, in / default / web-INF / build action.xml, the content is as follows:

ActionClass = "eXample.logonaction"

Formattribute = "logonform"

Formclass = "eXample.logonform"

InputForm = "/ logon.jsp">

The struts-config.xml file is as follows:

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

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

TYPE = "eXample.logonaction"

Name = "logonform"

Scope = "request"

INPUT = "/ logon.jsp">

Type = "org.apache.struts.Actions.addformbection" />

Type = "org.apache.struts.Actions.addforwardAction" />

Type = "org.apache.struts.Actions.addmapping" />

TYPE = "org.apache.struts.Actions.ReloadAction" />

Type = "org.apache.struts.Actions.removeformbection" />

TYPE = "org.apache.struts.Actions.removeforwardAction" />

TYPE = "org.apache.struts.Actions.removemapping" />

then establishes Logon.jsp, the content is as follows: <% @ page language = "java"%> <% @ taglib URI = "/ web-inf / struts-bean.tld" prefix = "bean"% > <% @ Taglib Uri = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%> <bean: message key = "logon .title "/> </ title> <html: base /> </ head> <body bgcolor =" white "> <html: errors /> <html: form action =" / logon "focus =" username "> < Table border = "0" width = "100%"> <tr> <trH align = "right"> <bean: message key = "prompt.username" /> </ tH> <TD align = "left"> < HTML: Text Property = "UserName" size = "16" maxlength = "16" /> <tr> <tr> <th align = "right"> <bean: message key = "prompt.password" /> </ Th> <TD align = "left"> <html: password property = "password" size = "16" maxlength = "16" redisplay = "false" /> </ td> </ tr> <TR > <TD align = "right"> <html: submit property = "submit" value = "submit" /> </ td> <td align = "left"> <html: reset /> </ td> </ tr > </ table> </ html: form> </ body> </ html: html> then build a class logonform.java, inherit from ActionForm, package In the example content as follows</p> <p>Package example;</p> <p>Import org.apache.struts.Action.actionform;</p> <p>Import javax.servlet.http.httpservletRequest; 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.actionmapping;</p> <p>Public class logonform extends actionform {</p> <p>Private string password = null;</p> <p>Private string username = null;</p> <p>Public string getpassword () {</p> <p>Return Password;</p> <p>}</p> <p>Public string getUsername () {</p> <p>Return UserName;</p> <p>}</p> <p>Public void setpassword (string password) {</p> <p>this.password = password;</p> <p>}</p> <p>Public void setusername (String username) {</p> <p>THIS.USERNAME = UserName;</p> <p>}</p> <p>Public void reset (actionMapping mapping, httpservletRequest request) {</p> <p>This.Password = NULL;</p> <p>THIS.USERNAME = NULL;</p> <p>}</p> <p>Public ActionerRors Validate (ActionMapping Mapping,</p> <p>HttpservletRequest request) {</p> <p>ActionerroS Errors = new actionerrors ();</p> <p>IF ((username == null) || (username.length () <1))</p> <p>Errors.Add ("UserName", New Actionerror ("Error.userName.Required));</p> <p>IF ((Password == null) || (Password.Length () <1))</p> <p>Errors.Add ("Password", New ActionError ("Error.Password.Required));</p> <p>Return Errors;</p> <p>}</p> <p>}</p> <p>Then build a class logonaction.java, inherit from Action, packaged in the example content as follows</p> <p>Package example;</p> <p>Import java.io.ioException;</p> <p>Import java.util.hashtable;</p> <p>Import java.util.locale;</p> <p>Import javax.servlet.RequestDispatcher;</p> <p>Import javax.servlet.servletException;</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.Actionerror;</p> <p>Import org.apache.struts.Action.Actionerro;</p> <p>Imports.Apache.struts.action.actionform; import org.apache.struts.Action.actionforward;</p> <p>Import org.apache.struts.action.actionmapping;</p> <p>Import org.apache.struts.action.ActionServlet;</p> <p>Import org.apache.struts.util.MessageResources;</p> <p>Public class logonaction extends action {</p> <p>Public ActionForward Perform (ActionMapping Mapping,</p> <p>Actionform Form,</p> <p>HTTPSERVLETREQUEST REQUEST,</p> <p>Httpservletresponse response</p> <p>THROWS IOException, servletexception {</p> <p>Locale locale = getLocale (request);</p> <p>// Messageresources Messages = getResources ();</p> <p>ActionerroS Errors = new actionerrors ();</p> <p>String username = ((logonform) form .getusername ();</p> <p>String password = ((logonform) form .Getpassword ();</p> <p>// Report Any Erroors We Have Discovered Back to the Original Form</p> <p>IF (! errors.empty ()) {</p> <p>SaveerRors (Request, Errors);</p> <p>Return (maping.getInput ())));</p> <p>}</p> <p>// Save Our Logged-in User In the session</p> <p>HttpSession session = request.getations ();</p> <p>Session.SetaTRibute ("User", "OK");</p> <p>IF (servlet.getdebug ()> = 1)</p> <p>servlet.log ("LogonAction: user:" </p> <p>"'logged on in session" session.getID ());</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>Then modify the /defaultroot/web-inf/web.xml file as</p> <p><? XML Version = "1.0" Encoding = "UTF-8"?> <! doctype web-app public "- // sun microsystems, incaps//dtd web application 2.3 // en" "http: // java. Sun.com/dtd/web-app_2_3.dtd "> <web-app> <servlet> <servlet-name> action </ servlet-name> <servlet-class> org.apache.struts.Action.ActionServlet </ servlet -Class> <init-param> <param-name> debug </ param-name> <param-value> 2 </ param-value> </ init-param> <init-param> <param-name> config < / param-name> <param-value> /web-inf/struts-config.xml </ param-value> </ init-param> <init-param> <param-name> Application </ param-name> < PARAM-VALUE> Example.ApplicationResources </ param-value> </ init-param> <init-param> <param-name> detail </ param-name> <param-value> 2 </ param-value> </ Init-param> <init-param> <param-name> validate </ param-name> <param-value> true </ param-value> </ init-param> <load-on-startup> 2 </ loading -on-startup> </ servlet> <servlet> <servlet-name> debugjsp </ servlet-name> <servlet-class> OR g.apache.jasper.servlet.JspServlet </ servlet-class> <init-param> <param-name> jspCompilerPlugin </ param-name> <param-value> com.borland.jbuilder.webserverglue.tomcat.jsp.JasperSunJavaCompiler </ param-value> </ init-param> <load-on-startup> 3 </ loading-on-startup> </ servlet> <servlet-maping> <servlet-name> action </ servlet-name> <</p> <p>URL-PATTERN> *. do </ url-pattern> </ servlet-maping> <servlet-maping> <servlet-name> debugjsp </ servlet-name> <url-pattern> *. jsp </ url-pattern> </ servlet-mapping> <welcome-file-list> <welcome-file> Logon.jsp </ welcome-file> </ welcome-file-list> <taglib> <taglib-uri> / web-inf / app. TLD </ taglib-uri> <taglib-location> /web-inf/app.tld </ taglib-location> </ taglib> <taglib> <taglib-uri> /web-inf/struts-bean.tld </ Taglib-uri> <taglib-location> /web-inf/struts-bean.tld </ taglib-location> </ taglib> <taglib> <taglib-uri> /web-inf/struts-html.tld </ taglib -uri> <taglib-location> /Web-inf/struts-html.tld </ taglib-location> </ taglib> <taglib> <taglib-uri> /web-inf/struts-logic.tld </ taglib- URI> <taglib-location> /web-inf/struts-logic.tld </ taglib-location> </ taglib> </ web-app> There is another important issue, that is, we have said Struts-EXample .war, put the /struts-example/web-inf/src/org/apache/shroperties after installation, put it in the / default / web-inf / class / example / Directory, then you can run, When you run JBuilder, Type http: // localhost: 8080 / will see the effect on IE. For details, please see the next explanation.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-5338.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="5338" 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.043</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 = 'GLYtV26O751sm_2Bbc6LeyCyzVWH5lqw4B58vnGD0LrfsCKw6MQkgjrqoTQf7_2FQ1IfY8_2BjxFO1my6s_2FX1Fk7WrBw_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>