A practical JBUILDER8 + STRUTS, a simple example

zhaozj2021-02-08  214

A practical JBUILDER8 STRUTS, a simple example

Liu Xiaowei: Liuxiaowei2000@sina.com

I. Introduction

This article describes a simple Struts application step using JBuilder 8, mainly based on step, theoretical instructions. I hope to help everyone, I hope everyone can criticize!

Operating environment:

Windows 2000 Server (SP3)

J2SDK1.4.1_01

JAKARTA-STRUTS-1.0.2 (JBuilder8 comes with)

Second. Actual combat

1 Create a Web Application

1.1 Select File- "New Project to establish a new project called StrutsDemo, as shown below:

figure 1

1.2 Click Next, remove the items in Required Libraries, others constant, click Finish.

figure 2

1.3 Select Project- "Project Properties, set the Server page according to the image below, click OK:

image 3

1.4 Select File- "New-" Web page - "Web Application, click OK.

Figure 4

1.5 After filling out the picture below, click OK.

Figure 5

2 establish an actionform

2.1 Select File- "New-" Web Page - "ActionForm, click OK.

Image 6

2.2 After filling in the picture below, click OK.

Figure 7

2.3 Click the Add button, add the definition of the Name and Password field, click Next.

Figure 8

2.4 After filling out the picture below, click Finish.

Figure 9

2.5 Write an ActionForm code.

Simple validity checking in ActionForm, fill in the loginActionform's validate () function:

Public ActionerRors Validate (ActionMapping ActionMapping, HttpservletRequest HttpservletRequest) {

ActionerroS Errors = new actionerrors ();

IF ((Name == null) || (Name.length () <1))

Errors.Add ("Name", New ActionError ("Error.userName.Required)));

IF ((Password == null) || (Password.Length () <1))

Errors.Add ("Password", New ActionError ("Error.Password.Required));

Return Errors;

}

3 establish an action

3.1 Select File- "New-" Web page - "Action, click OK.

Figure 10

3.2 After filling out the picture below, click Next.

Figure 11

3.3 After filling out the picture below, click Finish. Figure 12

3.4 Write an action code.

In general, write logic in Action, access the database, etc. Fill in the Loginaction's Perform () function as follows:

Public ActionForward Perform (ActionMapping ActionMApping, ActionMApping ActionMApping, Actionform ActionForm, HttpServletRequest HttpservletRequest, httpservletResponse httpservletResponse) {

LoginActionform form = (loginactionform) Actionform;

IF (Form.GetName (). Equals ("Liuxiaowei" && (Form.GetPassword (). Equals ("Liuxiaowei"))))))))

{

// Save Our Logged-in User In the session

HttpSession session = httpservletRequest.getations ();

Session.setttribute ("Username", Form.GetName ());

Return (ActionMapping.FindForward ("Success");

}

ActionerroS Errors = new actionerrors ();

IF (! form.getname (). Equals ("liuxiaowei")))

Errors.Add (ActionerRors.global_ERROR, New Actionerror ("Error.userName.wrong"));

IF (! form.getpassword (). Equals ("liuxiaowei"))

Errors.Add (ActionerRors.global_ERROR, New Actionerror ("Error.Password.wrong");

SaveerRors (httpservletRequest, Errors);

Return (ActionMapping.FindForward ("login"));

}

4 establish JSP

4.1 Select File- "New-" Web Page - "JSP from ActionForm, click OK.

Figure 13

4.2 After filling in the picture below, click Next.

Figure 14

4.3 After filling out the picture below, click Next.

Figure 15

4.4 After filling out the picture below, click Finish.

Figure 16

4.5 Modify the Login.jsp file.

After the modification is complete, you are as follows (red is a modification section):

<% @ 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><bean: message key = "login.title" /></p> <p></ title></p> <p></ hEAD></p> <p><body></p> <p><html: errors /></p> <p><bean: message key = "login.Heading" /></p> <p><p></p> <p><html: form action = "/ loginaction.do" Method = "post"></p> <p><bean: message key = "login.inputname" /> <html: text property = "name" /></p> <p><br></p> <p><bean: message key = "login.inputpassword" /> <html: password property = "password" /></p> <p><br></p> <p><html: Submit property = "submit" value = "submit" /> <br></p> <p><html: reset value = "reset" /></p> <p></ html: form></p> <p></ body></p> <p></ html: html></p> <p>4.6 Select File- "New-" Web Page - "JavaServer Page, click OK.</p> <p>Figure 17</p> <p>4.7 After filling out the picture below, click Next.</p> <p>Figure 18</p> <p>4.8 After filling out the picture below, click Finish.</p> <p>Figure 19</p> <p>4.9 Modify the Loginok.jsp file.</p> <p>After the modification is complete, you are as follows (red is a modification section):</p> <p><% @ page contenttype = "text / html; charset = GBK"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-TEMPLATE.TLD" prefix = "template"%></p> <p><html></p> <p><HEAD></p> <p><Title></p> <p><bean: message key = "loginok.title" /></p> <p></ title></p> <p></ head> <body bgcolor = "# ffffff"></p> <p><h1></p> <p><% = Request.getSession (). GetaTtribute ("UserName")%></p> <p><bean: message key = "loginok.message" /></p> <p></ h1></p> <p></ body></p> <p></ html></p> <p>4.10 Modify Struts-Config.xml files</p> <p>Double-click the struts-config.xml file on the left side of the file tree, select the Global Forwards item below, click the Add button on the right, add two items, as shown below:</p> <p>Figure 20</p> <p>4.11 Modify Web.xml file</p> <p>Double-click the web.xml file on the file tree on the left side, select the servlets-> Action item below, click the Add button on the right, add an application, as shown below:</p> <p>Figure 21</p> <p>Figure 22</p> <p>4.12 Write a resource file</p> <p>Select File- "Save all to save all files.</p> <p>Select Project- "Make Project" strutsdemo.jpx "compile project.</p> <p>Write a text file called ApplicationResources.properties, save the SRC / STRUTSDEMO in the project storage directory and add it to the project, right-click this file, select Properties, and press the figure.</p> <p>Figure 23</p> <p>The document content is as follows:</p> <p>Login.title = Struts demo</p> <p>Login.Heading = <H1> Welcome to the strutsdeMo! </ h1></p> <p>LGOIN.MESSAGE = Welcome to the strutsdemo!</p> <p>Login.inputPassword = please input password:</p> <p>Login.inputName = please input username:</p> <p>Loginok.title = User login successfull</p> <p>Loginok.Message = <h1> login successfull! </ h1></p> <p>Error.Username.Required = <h3> <font color = "red"> username is required! </ font> </ h3></p> <p>Error.Password.required = <h3> <font color = "red"> password is required! </ font> </ h3></p> <p>Error.username.wrong = <h3> <font color = "red"> username is wrong! <font> </ h3></p> <p>Error.Password.wrong = <h3> <font color = "red"> Password is Wrong! </ font> </ h3></p> <p>Errors.footer = </ ul> <hr></p> <p>Errors.Header = <h3> <font color = "red"> validation error </ font> </ h3> YourRect the folload error (s) before proceding: <ul> III. Run Application</p> <p>1. Right-click on the login.jsp in the left file tree, select Webrun Useing Defaults.</p> <p>2. If you do not enter Name and Password, click Submit, you will see an error message, which is generated by ActionForm.</p> <p>3. If you enter the wrong Name and Password, click Submit, you will also see the error prompt, which is generated by action, correct Name and Password in this example are "liuxiaowei", the correct words will look To the Login Successful page, Loginok.jsp, the username is passed through Session.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-2802.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="2802" 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.038</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 = 'QYNfXaOo5igTp5Aypihe9ffmsFh_2BhS4idjJGtjsxyzdD87UHzPi6qiQbKbHsLP_2BeUukPVDidoiMdwivw'; 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>