Building struts project in Eclipse 3.02

xiaoxiao2021-03-05  24

Install Tomcat plugin

Install Tomcat plugin

Note To add JDK's Tools.jar to the configuration, or you will not be able to compile JSP files.

New Tomcat project, name Struts

New Tomcat project, name Struts

Copy all files in the lib directory of jakarta-struts-1.1.zip (new version should not be no problem). Jar and .tld files are copied to struts / web-inf / lib directory.

Copy all files in the lib directory of jakarta-struts-1.1.zip (new version should not be no problem). Jar and .tld files are copied to struts / web-inf / lib directory.

Adding these JAR packages to the project via Project-> Properties-> Java Build Path

Drive to connect the MySQL database mysql-connector-java-

3.0.14-Production-bin.jar to copy to struts / web-inf / lib directory

Drive to connect the MySQL database mysql-connector-java-

3.0.14-Production-bin.jar to copy to struts / web-inf / lib directory

New web-inf / src / com / ben / test / struts directory, new Helloform.java and HelloAction.java files in the directory

New web-inf / src / com / ben / test / struts directory, new Helloform.java and HelloAction.java files in the directory

In the Struts directory, create a new JSP directory to store JSP files, create new hello.jsp and SayHello.jsp in the directory

In the Struts directory, create a new JSP directory to store JSP files, create new hello.jsp and SayHello.jsp in the directory

In the Struts / Web-INF directory, create a new struts-config.xml file, as follows:

In the Struts / Web-INF directory, create a new struts-config.xml file, as follows:

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

defines the data source, where the red fonts need to set as your own database name, user name, password

defines the data source, where the red fonts need to set as your own database name, user name, password

Set your new Helloform class

Setting the correspondence between the Helloform and HelloAction class, and access path

Defines the forwarded JSP file

Create a new web.xml file in the struts / web-inflicity, the content is as follows:

Create a new web.xml file in the struts / web-inflicity, the content is as follows:

action org.apache.struts.Action.ActionServlet

config

/web-inf/struts-config.xml

2

Action

*. do

/web-inf/struts-bean.tld

/web-inf/lib/struts-bean.tld

/web-inf/struts-html.tld

/web-inf/lib/struts-html.tld

/web-inf/struts-logic.tld

/Web-inf/lib/struts-logic.tld

Note the red part, define the servlet class that receives the request is org.apache.struts.Action.ActionServlet, the servlet is responsible for reading /web-inf/struts-config.xml to determine how the user's HTTP request

Note the red part, define the servlet class that receives the request is org.apache.struts.Action.ActionServlet, the servlet is responsible for reading /web-inf/struts-config.xml to determine how the user's HTTP request

just defines some tags, which is not described here.

The content of Helloform.java is as follows:

The content of Helloform.java is as follows:

Package com.ben.test.struts;

Import javax.servlet.http.httpservletRequest;

Import org.apache.struts.Action.Actionerror;

Import org.apache.struts.Action.Actionerro;

Import org.apache.struts.Action.actionform;

Import org.apache.struts.action.actionmapping;

Public Final Class Helloform Extends Actionform {

PRIVATE STRING NAME;

Public string getname () {

Return name;}

Public void setname (String name) {

THIS.NAME = Name;

}

Public ActionerRors Validate (ActionMapping Mapping, httpservletRequest Request) {

ActionerroS Errors = new actionerrors ();

// Error handle, add yourself

Return Errors;

}

}

The content of HelloAction.java is as follows:

The content of HelloAction.java is as follows:

Package com.ben.test.struts;

Import javax.servlet.http.httpservletRequest;

Import javax.servlet.http.httpservletResponse;

Import org.apache.struts.Action.action;

Import org.apache.struts.Action.actionform;

Import org.apache.struts.Action.actionForward;

Import org.apache.struts.action.actionmapping;

Import java.sql. *;

Import java.sql. *;

Import javax.sql. *;

Public final class helloaction extends action {

Public final class helloaction extends action {

Public ActionForward Execute (ActionMapping Mapping, Actionform Form,

HTTPSERVLETREQUEST REQUEST,

HttpservletResponse response) throws exception {

Helloform Helloform = (Helloform) Form;

String name = helloform.getname ();

INT ID = 0;

DataSource DataSource = (DataSource) servlet.getServletContext (). GetAttribute (

"org.apache.struts.action.data_source");

Try {

Try {

Connection conn = DataSource.getConnection ();

Statement Stmt = conn.createstatement ();

ResultSet RS = Stmt.executeQuery ("Select * from name " ");

While (rs.next ()) {

ID = rs.getint ("id");

}

Rs.close ();

Stmt.close ();

CONN.CLOSE ();

}

Catch (SQLException E) {

Throw New Sqlexception ("Database Error");

}

Request.setttribute ("Name", New Integer (ID));

Return mapping.findforward ("Sayhello");

}

}

The code is created in accordance with the data source configured in Struts-Config, and then obtain the ID corresponding to the Name in accordance with the NameTable of the Name to the database, then let the ID value of the request to Sayello More struts-config configuration, Sayhello corresponds to this file

The content of Hello.jsp is as follows:

The content of Hello.jsp is as follows:

<% @ page contenttype = "text / html; charset = uTF-8" Language = "java"%>

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

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

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

test struts </ title></p> <p></ hEAD></p> <p><body></p> <p><html: form action = "/ helloworld"></p> <p>Name: <html: Text property = "name" /></p> <p><html: submit /> <html: ca ZANCEL /></p> <p></ html: form></p> <p></ body></p> <p></ html: html></p> <p>The content of SayHello.jsp is as follows:</p> <p>The content of SayHello.jsp is as follows:</p> <p><% @ page contenttype = "text / html; charset = uTF-8" Language = "java"%></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><html: html locale = "true"></p> <p><HEAD></p> <p><title> test struts </ title></p> <p></ hEAD></p> <p><body></p> <p>Welcome <% = Request.getaTattribute ("name")%></p> <p></ body></p> <p></ html: html></p> <p>A very simple example, I hope to help everyone</p> <p>A very simple example, I hope to help everyone</p> <p>Zaiben @ smth, 55555555555, @ smth, 55555555555, water is dead</p> <p>2005-4-16</p> <p>Zaiben @ smth, 55555555555, @ smth, 55555555555, water is dead 2005-4-16</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-35198.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="35198" 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 = 'AjHGF2CEZP32S8sddPR44RtoMttUMWHPdzsJysUdZHJ3gliliS_2BpVo26KygmnSvc2B_2Fzmaou72oOrkoBbVD_2Bmw_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>