Struts1.1 introduction

xiaoxiao2021-03-06  101

Learn about MVC

MVC is the abbreviament of Model, View, Controller, MVC is the design pattern developed by Application. It is MODEL2 that everyone knows. In the MVC design mode, you request your business logic, interface display, data separation in Application development. That is, in Model, View, Controller implementation: data, control (business logic) display (page display). In the previous or traditional Web Application development methods, such as ASP, PHP, JSP (Model 1) is developed, we In ASP (PHP, JSP) implementation, such as: From the database to the data we need, and according to the relationship between the data and the actual needs, according to some way to display him in the page and submitted from the page. Extract data, query related data from the database according to business logic, or write data to the database. That is to say we implement everything in ASP (PHP, JSP): interface display, business logic, data access. The consequence of this is that the ASP (PHP, JSP) you are writing is not level, and HTML and Script (JavaScript, JSCript, ASP, PHP, JSP source code) are nesting each other. The maintenance is poor, the most interesting is in Web Application usually shows a piece of art, many times, you are also writing ASP, PHP, JSP and then beautifying, it is very likely that you have found that your code has been faced with it. You have to reorganize your code. The solution to this problem in the MVC mode is: VIEW is responsible for displaying, and view usually gets the data that has been processed from the Controller, and then displayed in the page, it should be said to nested very few Script in HTML. Basic artist Modifying that you will abolish your labor results. There are several developments that meet the MVC design model to make you choose from using Java Development Web Application. 1: JSP Servlet Javabean (EJB) 2: JSP Javabean (Controller) Javabean (EJB) (Model) 3: TDK (Turbine, Velocity ...) 4: XSP5: JSP Struts Javabean (EJB) I Personally think that there are two more than two, others have any places, especially using TDK because there is a better tool to automatically generate a lot of code, as for its shortcomings in several development methods, I will introduce.

The new features of Struts1.1 Struts1.1 and 1.0 have some very good features. The most important is that the function enhancement is enhanced. The verification of Struts1.1 data is not true in the Action before the Action, but the benefits of virtually the use of Validation.xml through configuration is a lot of reuse.

The main composition of Struts1.1 is mainly included in: Action, ActionForm, ActionMapping, ActionForward, the most important thing in development is that actionActionform can write or do not write as needed. Below I will introduce a specific introduction. ActionAn Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. Struts development team is above description of the Action that actually Action Request Business Logic and intermediate adapter. Popularity is to take the data from the form and wear a series of operations to the business logic operation and return the corresponding operational information.

ActionFormAn ActionForm is a JavaBean optionally associated with one or more ActionMappings. Such a bean will have had its properties initialized from the corresponding request parameters before the corresonding action's execute () method is called.ActionForm is actually taken from the data package to the Request And check, then the legal data is processed to the action. In addition to data checks, ActionForm is more important in addition to data checks. Instead, most of the data verification after 1.1 will be implemented in validation.xml. ActionMapping, ActionForwardActionMapping is mainly used with configuration and description related properties. First look at the configuration file in Struts-config.xml, one configuration description: ActionMapping is used to describe the URL of an action, a specific implementation file, a corresponding ActionForm Data Attribute (Request Or Session), whether data checksum is required, and the URL that may be jumped after processing is completed. ActionForward you can understand the jump URL after the Action operation is completed, and the action returned after processing the correlation operation is an actionforward, which is to tell Struts. I will go to this step.

Build a struts1.1 Run environment My configuration is to discuss the Tomcat 4.0 or later, and the other AppServer is roughly the same. 1: Get struts1.1 http://jakarta.apache.org/builds/jakarta-struts/release/v1.1-b1/jakarta-struts-1.1-b1.zip2: Setting struts.jar Copy to $ Tomcat_home / COMMON / LIB or you add the following configuration under Web-INF / LIB under the APPACTION of Struts Add the following configuration under Web.xml under the web.xml

action org.apache.struts.Action.ActionServlet config /Web-inf/struts-config.xml debug 3 detail 3 < Load-on-startup> 2 /web-inf/struts-html.tld / web -Inf / struts-html.tld

/web-inf/struts-logic.tld /web-inf/struts-logic.tld

/web-inf/struts-nested.tld /web-inf/struts-nested.tld

/web-inf/struts-template.tld /web-inf/struts-template.tld Struts1.1 provides a very detailed example, you can take a closer look. Next you should configure struts-config.xml as needed, here is a simple example

<-! Add multiple validator resource files by setting the pathname property -> sT-property property =" pathname "value =" / web-inf / validation.xml "/>

I believe you can understand. I will not explain it.

You need to continue to configure Validation.xml, see the following simple example.

Mask ^ / w $ minlength 5 maxlength 20 < / var> mask ^ / W $ minlength 5 maxlength 20

"Maxlength" key = "$ {var: maxlength}" Resource = "false" /> mask ^ / w $ minlength 5 maxlength 20 < Var-name> mask ^ / w $ minlength 10 maxlength 20 The above Validation.xml is to tell Struts My usegegform taken to do the following verification username must not be empty and the smallest length is 5, the maximum length is 20 .... ............. PASSWORD, NICKNAME, Superpass basically I don't do more. The configuration is basically over, we have to start writing the first Struts. Develop struts1.1

Useg.jsp In order to consider the flexibility of the page, all elements displayed in the page are put in the Properties file and take this file from com.bingo.finance.common.htmlmsg. ========== ============================================================================================================================================================================================================= ======= <% @ page import = "java.io. *"%> <% @ page import = "com.bingo. Finance.common.htmlmsg "%> <% @ Taglib URI =" / Web-inf / struts-html.tld "prefix =" html "%> <% @ Taglib URI =" / Web-inf / struts-logic.tld "prefix =" Logic "%> <% @ taglib URI =" / Web-inf / struts-bean.tld "prefix =" bean "%> <% = htmlmsg.title_regiseet%> </ title> <meta http-equiv = "content-type" content = "text / html; charSet = GB2312"> </ head></p> <p><body bgcolor = "# ffffff" text = "# 000000"> <form name = "usereg" method = "post" action = "usegeg.esp"> <! - The defixes in Struts are .do you can Modify it according to need-> <table width = "100%" border = "0" cellspacing = "1" cellpadding = "1"> <tr align = "center"> <td color colspan = "2"> <% = HTMLMSG.TITLE_REGISET%> </ td> </ tr> <tr> <td width = "49%" align = "right"> <% = htmlmsg.common_username%>: </ td> <TD width = "51% "<! - In order to implement backcrupration, it is recommended that you use TAG to use the following way to use the following method to replace the general write <html: text protection =" username "size =" 20 "maxlength =" 30 "styleclass =" css "styleID = "UserID" /> The code above you don't understand what you need to learn more about tag .--> <input type = "text" name = "username" size = "20" maxlength = "20"> </ td> </ Tr> <Tr> <TD Width = "49%" align = "right"> <% = htmlmsg.common_password%>: </ td> <TD width = "51%> <input type =" password "name = "Password" size = "20" MaxLength = "20"> <td width = "49%" align = "right"> <% = HTMLMSG.COMMON_NICKNAME%>: </ TD> <td width = "51%"> <input type = "text" name = "nickname" size = "20" maxlength = "20"> <td> </ tr> <tr> <td width = " 49% "align =" right "> <% = htmlmsg.common_superpass%>: </ td> <td width =" 51%> <</p> <p>INPUT TYPE = "password" name = "superpass" size = "20" maxlength = "20"> <td> </ tr> <tr> <TD width = "49%" Align = "Right"> <input Type = "Submit" name = "Submit" value = "<% = htmlmsg.action_regiseet%>> </ td> <TD width =" 51%> <input type = "reset" name = "Submit2" value = " <% = Htmlmsg.common_reset%>> </ td> </ tr> </ table> </ form> </ body> </ html> usegegactiom.java ================================================================================================================================================================================================= ===================================================== package com.bingo .finance.action;</p> <p>// java importimport java.io.ioException; import java.util.locale;</p> <p>// servlet importimport javax.servlet.servletException; import javax.servlet.http.httpsession; import javax.servlet.http.httpservletRequest; import javax.servlet.http.httpservletResponse;</p> <p>// struts importimport 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;</p> <p>// Finance Importimport com.bingo.finance.action.userform; import com.bingo.finance.manager.userManager; import com.bingo.finance.entity.user;</p> <p>public class UseregAction extends Action {// before use Struts1.1 perform // struts1.1 use execute alternate performpublic ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {try {UserForm userForm = ( userForm) form; UserManager userManager = new UserManager (); User user = userManager.formToEntity (userForm); userManager.add (user); // Insert into DataBase a Userinfo} catch (Exception ex) {return mapping.findForward ( "error ");} return mapping.findforward (" success "); // forward is" /msg.jsp" }ouseuserform.java===================== ============================================================================================================================================================================================================= == package com.bingo.finance.action; import java.util *;. import java.io.Serializable; // servlet importimport javax.servlet.ServletException; import javax.servlet.http.HttpSession; import javax.servlet.http .HttpservletRequest; import javax.servlet.http.httpservletResponse; // struts importimport org.apache.struts.Action.ActionMapping; import org.apache.struts.validator .ValidatorForm; public class UserForm extends ValidatorForm implements Serializable {// Struts1.1 in previous usual Form inherit ActionForm // actually ValidatorForm inherited ActionForm // Why add a layer of ValidatorForm to check and do private String id; private String username; private string password; private string superpass;</p> <p>Public userform () {</p> <p>}</p> <p>/ **** / public string getId () {return this.id;}</p> <p>/ **** / public void setId (string _id) {this.id = _id;}</p> <p>/ **** / public string getUsername () {return this.username;} / **** / public void setUsername (String _username) {this.username = _USERNAME;}</p> <p>/ **** / public string getPassword () {return this.password;}</p> <p>/ **** / public void setPassword (string _password) {this.password = _password;</p> <p>/ **** / public string getnickname () {return this.neickname;}</p> <p>/ **** / public void setnickname (String _nickname) {this.neickname = _nickname;}</p> <p>/ **** / public string getsuperpass () {return this.superpass;}</p> <p>/ **** / public void setsuperpass (String _superpass) {this.superpass = _superpass;</p> <p>/ ** show this class info ** / public string toString () {stringbuffer info = new stringbuffer (); info.append (".... ID is:" id); info.append (".... Username is: " username); info.append (" .... password is: " password); info.append (" .... nickname is: " nickname); info.apund (" ... .SUPASS IS: " superpass); Return Info.toString ();</p> <p>Public void reset (ActionMApping mapping, httpservletRequest request) {id = null; username = null; password = null; nickname = null; superpass = null;}}</p> <p>UserManager.java, User.java file I will not provide, this part is dealing with yourself in the actual development, which is to insert the data into the database. Now a simple registration user is developed. Very simple. Oh, continue to work hard ... but there are still many details, you need to know more, I only get the door, you must go to study some things, for example, I hope that my users have more email fields and an age. How should I do, and I hope that Validation.xml verifies that Email legal verification, age must be greater than 0 and must be an integer. Then how should I do? I can only tell you to add two fields. You need to modify the Form to add fields, and the corresponding file must be modified. Email's verification in Struts is very simple. Look at the example, study Validator-Rules.xml, this I think Struts1.1 is the best function increase (Struts1.0 does not have this file). A very flexible and highly high verification mechanism is provided in Struts.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-122299.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="122299" 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.049</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 = 'ct_2FaRjmR1_2FMt_2Fv23QzGpSCRwne4RHzSSKC6jcABEiXQRSPL8lbY2vI9YsurDTEm3_2F5_2BbqFois3qP6BWsMaHv_2Bw_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>