Talk about struts learning

zhaozj2021-02-16  94

Learning to talk about Struts, 2004 June 21 Author: blue_sunny

This article mainly introduces the basic principles of Struts 1.1 framework, and introduces Struts 1.1 installation and a simple example. 1. The origin of Struts Struts is an integral part of the Apache Jakarta project. The project's goal is to establish an open source framework provided by the establishment of a Java web application. At present, the version is generally used in 1.1, but it has also appeared 1.2. By using the Struts framework can improve and improve the application of Java Server Pages (JSPS), servlet, label libraries, and object-oriented technology in web applications. Applying the Struts framework to reduce the development time of your MVC (Model-View -Controller) design mode, thereby increasing development efficiency. Applying Struts to the J2EE client, it should be a good choice. 2. Struts works in Struts, we have to mention the MVC design mode. MVC is an abbreviation of Model-View-Controller, which is a common design pattern in the web application. The MVC weakens the coupling between business logic interfaces and data interfaces, and makes view layers more varied. Struts is an implementation of MVC, which combines JSP, Java Servlet, Java Bean, Taglib and other technologies. Then let's take a look at the working principle of the Struts framework: Control: In Struts, ActionServlet plays a controller. ActionServlet is a universal control component. This control component provides an entry point that processes all HTTP requests sent to Struts. It intercepts and distributes these requests to the appropriate action classes (these action classes are subclasses of the Action class). Additional control components are also responsible for popping an action form (usually called frombean) with the corresponding request parameters, and transmits it to the action class (usually called ActionBean). Action class implements core business logic, which can access Java Beans or call EJB. All of these control logics use the struts-config.xml file to configure. View: Mainly by JSP to control the page output. It receives data in the Action Form, which utilizes HTML, Taglib, Bean, Logic. Model: In Struts, there are mainly three beans, namely: action, actionform, ejb, or java bean. Actionform is used to encapsulate customer request information, and the Action acquires data in the ActionForm, and then processes the EJB or Java Bean. 3 Struts Advantages and Disadvantages Advantages: 1. TAGLIBs provided by Struts can greatly save time. 2. Performance is separated from logic. 3. It is more convenient to maintain and expansion. 4 Easy to develop shortcomings: a large number of labels, the difficulty of beginners is difficult. 4. Struts 1.1 Installation Install Struts 1.1 is relatively simple. Suppose we use Tomcat 5.0, and installed under D: / Tomcat. First, please go to http://jakarta.apache.org/struts/ download struts 1.1, after decompression, you can see the Jakarta-Struts-1.1 directory, under this directory, you can see such a directory: WebApps. There are multiple * .war below the directory, our Copy Struts-Blank.war files Under D: / Tomcat / WebApps, RESTART TOMCAT, which generates a Struts-Blank blank Struts development environment under WebApps of Tomcat.

In addition, Struts-Example.War in WebApps in your decompressed directory is a good struts learning tutorial, if you want to learn in-depth, please visit http://jakarta.apache.org/struts or download "Struts in action "E text. 5. A simple example 5.1 logon.jsp <% @ Taglib URI = "/ Web-inf / struts-html.tld" prefix = "html"%> <% @ page contenttype = "text / html; charSet = GB2312" %> <% / ** * this page shows logon interface. * Title: Logon System * Description: Logon Page * Copyright: Copyright (c) 2004 * Company: www.ewe.com.cn * @Author Bluesunny * @version 1.0 * / response.setHeader ("Pragma", "no-cache"); response.setheader ("cache-control", "no-cache"); response.setdateHeader ("expires", 0 );%> Logon </ title> </ head> <body> <html: errors /> <html: form action = "/ logon" FOCUS = "username"> username: < HTML: Text Property = "UserName" size = "25" /> password: <html: password proteth = "password" size = "25" /> <html: submit property = "submit" /> </ html: form> </ body> </ html> page is a user login page, applied to the HTML tab of Struts.</p> <p>5.2 LogonForm.java package com.logon.app; import javax.servlet.http *; import org.apache.struts.action *; / ** * This page shows LogonForm.java * Title:.. LOGON SYSTEM * Description: LogonForm Page * Copyright: Copyright (c) 2004 * Company: www.ewe.com.cn * @author bluesunny * @version 1.0 * / public class LogonForm extends ActionForm {private String username; private String password; public LoginForm () {username = null; password = null;} public void setUsername (String username) {this.username = username;} public String getUsername () {return this.username;} public void setPassword (String password) {this.password = password;} public String getPassword () {return this.password;} public void reset (ActionMapping mapping, HttpServletRequest request) {username = null; password = null;}} this page is ActionForm bean, it is mainly used to package the client's user name and password.</p> <p>5.3 logonaction.java package com.logon.App; import java.io. *; import java.sql. *; Import org.apache.struts.Action.Apache.Struts.Action.Apacward; import ORG. Apache.struts.Action.Actionmapping; import org.apache.struts.Action.actionform; import javax.servlet. *; import javax.servlet.http. *; import org.apache.struts.action. *; / ** * This page shows LogonAction.java * Title: LOGON SYSTEM * Description: LogonAction Page * Copyright: Copyright (c) 2004 * Company: www.ewe.com.cn * @author bluesunny * @version 1.0 * / public class LogonAction extends Action { public ActionForward execute (ActionMapping actionMapping, actionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {LogonForm form = (LogonForm) actionForm; ActionErrors errors = new ActionErrors (); String username = form.getUsername (); String password = form.getPassword (); Username.equals ("bluesunny" && password.equals ("bluesunny")) {return anctionsMapping.findforward ("Success") Else {Errors.Add ("Login", New Actionerror ("Logon.Failed"); SaveErRors (Request, Errors); Return ActionMapping.FindForward ("failed");}}} Get data in ActionForm, Processing, the processing result returns to the JSP page.</p> <p>5.4 Web.xml <? XML Version = "1.0" Encoding = "ISO-8859-1"> <! Doctype web-app public "- // sun microsystems, Inc.//dtd Web Application 2.3 // en" " http://java.sun.com/j2ee/dtds/web-app_2_3.dtd ";> <web-app> <display-name> struts logon application </ display-name> <! - standard action servlet configuration WITH Debugging) -> <servlet> <servlet-name> action </ servlet-name> <servlet-class> org.apache.struts.Action.ActionServlet </ servlet-class> <init-param> <param-name > config </ param-name> <param-value> /web-inf/struts-config.xml </ param-value> </ init-param> <init-param> <param-name> debug </ param- Name> <param-value> 2 </ param-value> </ infit-param> <init-param> <param-name> detail </ param-name> <param-value> 2 </ param-value> < / init-param> <load-on-startup> 2 </ loading-on-startup> </ servlet> <! - standard action servlet mapping -> <servlet-maping> <servlet-name> action </ servlet -Name> <url-pattern> *. Do </ url-pattern> </ servlet-mapping> <! - the welcome file list -> <welcome-file-list> <welcome-file> logon.jsp < / Welcome-Fi Le> </ welcome-file-list> <! - struts tag library descriptors -> <taglib> <taglib-uri> / tags / struts-bean </ taglib-uri> <taglib-location> / web-inf /struts-bean.tld </ taglib-location> </ taglib> <taglib> <taglib-uri> / tags / struts-html </ taglib-uri> <taglib-location> / web-inf / struts-html. TLD </ taglib-location> </ taglib> <taglib> <taglib-uri> / tags / struts-logic <</p> <p>/ taglib-uri> <taglib-location> /web-inf/struts-logic.tld </ taglib-location> </ taglib> <taglib> <taglib-uri> / tags / struts-nested </ taglib-uri> <taglib-location> /web-inf/struts-nested.tld </ taglib-location> </ taglib> <taglib> <taglib-uri> / tags / struts-tiles </ taglib-uri> <taglib-location> /Web-inf/struts-tiles.tld </ taglib-location> </ taglib> </ web-app> 5.5 struts-config.xml <? XML Version = "1.0" eNCoding = "ISO-8859-1? > <! Doctype struts-config public "- // Apache Software Foundation // DTD Struts Configuration 1.1 // en" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd";> <struts -config> <form-beans> <form-bean name = "logonform" type = "com.logon.app.loginform" /> </ form-beans> <action-mapings> <action path = "/ logon" TYPE = "com.logon.app.logonaction" name = "logonform" scope = "request" infut = "logon.jsp"> <forward name = "success" path = "/ success =" /> <forward name = " Failure "path =" / logon.jsp "/> </ action> <message-resources parameter =" resources.Application "/> </ struts-config> 5.6 Application.properties s logon.failed = logon failed! please logon in again! 5.7 Deploy logon Application Create a success.jsp, enter: "successflly!". Compile the source file, and deploy the application in Tomcat, assume the deployment below the app directory .</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-12019.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="12019" 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.044</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 = '49DU0T5F8FMsJwkXk8HZH1LlqKgk38t4g8M_2FKDE6dZSJRkCp0t7wcAXfr7_2BVwT7JTYFWYzs_2FhOPkI_2FFrT4a28Q_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>