Title: Struts Practice

xiaoxiao2021-03-06  78

Title: Struts Practice

[comment]

Author:

Liu Guodong

(dev2dev id: yahoo163)

introduction

I recently used Struts, and now I will write down my personal feelings, I hope to help you. It is used to translation in the writing format of foreigners in the translation document. This is also available for use, which looks like it is clearly J. I am not intended to introduce the advantages of Struts and what is MVC mode. There will be many articles on the Internet. I only specify an example. Combine this example to put Struts's primary stuff some stuff (Advanced, I didn't understand it, I didn't look at it. Ok, nonsense doesn't talk about it. Otherwise, someone wants to throw bricks.

Operating environment

I can't help but say the steps to say two environmental buildings. It is really what I think this is very important. You have only tolerate the first.

1. Required software

In order to run this example, you need the following software:

WebLogic8.1sp2 or any other web server, I use WebLogic8.1sp2. You can download from the BEA's website: http://commerce.be.com/index.jsp struts 1.0, you can be fooled from www.apache.org. A easy-to-use Java IDE, I use JBuilder, of course, this is not necessary. Another is a tool to write JSP, and the general web editing tool can be.

2. Environmental construction

Install struts

Several deployment description files and tag library description files Copy to your defaultApp / web-inferid directory from the Struts compression package.

These files include:

Struts-config.xml: The file used to configure your serveet and mapping Java Bean.

Struts-html.tld: This is a label library description file that he implements several labels related to the request response in HTML.

Struts-logic.tld: This is also a label library description file, which implements the functionality of logical operations in Java, such as loop, judgment, and more.

Struts-template.tld: Template tag library description file

Struts-bean.tld: Some tag libraries required for Java bean mapping.

In the DEFAULTAPP / WEB-INF / LIB directory copy of Struts.jar. It's okay, and then configure it. Web.xml is OK.

Configure web.xml files

You can directly put this file copy to the defaultApp / web-inflicment from the Struts compression package, if you want to figure out what they mean, it is not difficult, you will know if you have a little bit a little bit of JSP tag library. He is just to load the label library you want to use into your web app. In order to save space, I will no longer post him.

Start

This example we have to say is a simple login verification page, which is to enter the username and password. Click "Submit" to print out the username and password at the console. Since Struts is MVC structure, we may wish to start our example in the order of this structure.

Struts Model

The so-called model I understand that a container for storage information may be a bit shallow, but beginners may wish to understand this, so it is easy. To put it bluntly, the information submitted by the user from the JSP page is saved to a Java Bean. Of course, this Java Bean is not a normal Java Bean because it inherits from Struts Actionform, so that the Control class can recognize it and control it. From the literal meaning of the Actionform, we are not difficult to see, it is like the Form tag in our HTML, an actionform can represent the content in a form, such as this class Loginform in our example:

Package mytest.struct; // Import must be packaged

Import org.apache.struts.action. *;

Import javax.servlet.http. *;

// Inherited ActionForm

Public class loginform extends an actionform {

Private string name; // represents Name in the JSP page, note that the Name and the NAME in the page must be the same.

PRIVATE STRING Password;

/ / Set NAME value

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public string getpassword () {

Return Password;

}

Public void setpassword (string password) {

this.password = password;

}

// Data check, you can do it yourself.

Public ActionerRors Validate (ActionMapping ActionMapping, HttpservletRequest HttpservletRequest) {

ActionerroS Errors = new actionerrors ();

IF (name == null) {

Errors.Add ("Name", New Actionerror ("Name Can Not Be Null!");

}

IF (Password.length () <3) {

Errors.Add ("Password", New ActionError ("Password Lengh At Least 3 Chars");

}

Return Errors;

}

// Data reset processing

Public Void Reset (ActionMapping ActionMapping, httpservletRequest httpservletRequest) {

Name = NULL;

Password = NULL;

}

}

Ok, this is one of our Models, only two fields of Name and Password, which inherits the ActionForm and has the Java Bean standard Getter and Setter methods. That's so much, let's take a look at what is in the view.

Struts VIEW

VIEW is our JSP page, but if you use struts, JSP is more different. Let us take a look at the code:

Login.jsp

<% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%>

<% @ Taglib URI = "/ Web-INF / STRUTS-TEMPLATE.TLD" prefix = "template"%>

<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>

<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>

<% @ Page ContentType = "Text / HTML; Charset = GBK"%>

</p> <p>login</p> <p></ title></p> <p></ hEAD></p> <p><body></p> <p><p></p> <p><html: form action = "/ loginaction.do" Method = "post"></p> <p><! - Here /loginaction.do will tell, equivalent to the servervet we have to request -></p> <p>Username:</p> <p><HTML: Text Property = "Name" /></p> <p><! - Playing a text box, named Name, pay attention to it must be the same as the fields in the loginform -></p> <p><br></p> <p>PASSWORD:</p> <p><html: Password property = "password" /></p> <p><br></p> <p><Table Border = "1"></p> <p><tr></p> <p><TD> <HTML: Submit Property = "Submit" value = "submit" /> </ td></p> <p><TD> <HTML: Reset Value = "RESET" /> </ td></p> <p></ TR></p> <p></ TABLE></p> <p><br></p> <p></ html: form></p> <p></ body></p> <p></ html: html></p> <p>The content in view is so much, it is white, it is the label in HTML instead with the label in Struts. What is the use of this? Don't be in a hurry to tell the Struts deployment, you will understand. Here I strongly recommend that it is best to learn about the knowledge of JSP tag libraries before learning Struts.</p> <p>Struts Control</p> <p>This Control here is a servlet we handle page information. Of course, he and our usual servlet still distinguish it, it inherits the struts' action. And implement the Perform method:</p> <p>Loginaction.java</p> <p>Package mytest.struct;</p> <p>Import org.apache.struts.action. *;</p> <p>Import javax.servlet.http. *;</p> <p>Public class loginaction extends action {</p> <p>Public ActionForward Perform (ActionMapping ActionMApping, ActionMApping ActionMApping, Actionform ActionForm, HttpServletRequest HttpservletRequest, httpservletResponse httpservletResponse) {</p> <p>Loginform loginform = (loginform) Actionform;</p> <p>System.out.println ("UserName:" loginform.getname ());</p> <p>System.out.println ("Password:" loginform.getpassword ());</p> <p>Return ActionMapping.FindForward ("Logok");</p> <p>}</p> <p>}</p> <p>Description: Loginform loginform = (loginform) Actionform;</p> <p>This sentence gains a loginform container object so that data can be obtained from the page, and if you say to get the data of the Name text box in the JSP page: loginform.getname (); last Return ActionMapping.FindForward ("Logok" ); Return a link. The "Logok" is defined in struts-config.xml. That is, where the page jumps to where the logic is executed.</p> <p>Struts deployment:</p> <p>Struts deployment information is described in Struts-Config.xml. Now tells the most critical part of Struts. If you want our previous procedures to run the key to see this step, I will put this post it first:</p> <p>Struts-config.xml</p> <p><? XML Version = "1.0" encoding = "UTF-8"?></p> <p><! Doctype struts-config public "- // Apache Software Foundation // DTD Struts Configuration 1.0 // En" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"></p> <p><struts-config></p> <p><form-beneans></p> <p><form-bean name = "loginform" type = "mytest.struct.loginform" /></p> <p></ form-beans></p> <p><global-forwards></p> <p><forward name = "logok" path = "/ login.jsp" /></p> <p></ global-forwards></p> <p><action-mappings></p> <p><action name = "loginform" type = "mytest.struct.loginaction" Validate = "true" scope = "request" path = "/ loginaction" /></p> <p></ action-mappings></p> <p></ Struts-Config></p> <p>Description:</p> <p><form-beans> tag describes the information of the container bean is the loginform we wrote.</p> <p>The <global-forwards> tag describes the position of the page jump. His name attribute is the parameters of the ActionMApping.Forward ("Logok") method in the LoginAction class, which will jump to where the page will be jumped after the page is executed. , I set it here to /login.jsp is to tell the Struts Running request to return to the original location.</p> <p>All control classes are described in <action-mapings>, only one control class in our example is, loginaction, where the value of the Path property describes the URL path in the browser, and that Name description It is this control class to handle that FORMBEAN.</p> <p>run:</p> <p>All work is complete, start WebLogic8.1, then enter http: // localhost: 7001 / Applications / Login.jsp in the browser. You can see the following page: Enter your username and password, click the Submit button. WebLogic console will appear:</p> <p>in conclusion</p> <p>A simple Struts program is like this. Because of the limitations of the space, I just print the information obtained from the page to the console, of course, you can also save the obtained data to the database or perform complex business processing, etc. Wait. However, through this simple example you should understand how struts run, you should also learn how to deploy and use Struts under WebLogic8.1. I hope this article will take you into the world of Struts.</p> <p>Click here to download this document section</p> <p>Author:</p> <p>Name: Liu Guodong</p> <p>Address: No. 20, Haidian District, Beijing, China Mailbox</p> <p>Zip code: 100083</p> <p>self introduction:</p> <p>2000 graduates have been engaged in software development after graduation. It has been involved in the development of multiple projects, using technology including EJB, SELVLET, JSP, STRUTS, JDO, Web Services, etc. Using WebLogic Server as the application server in projects recently hosted, the Struts's MVC structure is designed and developed, using EJB2.0 technology as a data store for the persistence layer.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-88867.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="88867" 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.040</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 = 'pFy03bAfPgyL5DvxVIE5ZqM_2Fj7sEREGtUWzhfrDRoDbbwQE8if4Wng6zz2CbOe6svGKd2LJhdXxZ3067SWO0MQ_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>