Struts Principles and Practices (7)

xiaoxiao2021-03-06  15

Struts Principles and Practice (7) Author: Zhang Mei Luo Kuaibo dispatch time: 2004.11.03

In the previous article, it is introduced that the calibration of the customer will be used to use JavaScript. It actually uses Struts to cooperate with JavaScript to implement many useful features. For example, the realization of cascaded drop-down menus is a typical example: this If you want to implement an article publishing system, the article we have to publish is divided into news and technology, in which news is divided into news and industry dynamics; technical classes are divided into operating systems, databases, and programming languages, etc. To facilitate adding new entries, all of this is saved in the database table. To do this, we build a table named Articles and a table named articles.

The structure of the ArticleClass table is as follows:

ArticleClassID field: CHAR type, length is 2, primary key

ArticleClassName field: varchar type, length 20

The structure of the ArticlesusuBClass table is as follows:

ArticleClassID field: CHAR type, length 2

ArticlesUBClassID field: CHAR type, length 2 with ArticleClassID constitutes primary key

ArticlesUBClassName field: VARCHAR type, after the length is 20 tables, enter the following data in the articleclass table: 01, news classes; 02, technology class entry in ArticlesUBClass table: 01, 01, news news; 01,02, Industry dynamics; 02, 01, operating system, etc. record. Here, the database preparation work has been made. With the foundation of the login example, it is not difficult to understand the work you have to do. Our current work is also conducted in the original mystruts project. First, establish the FORMBEAN, which needs to be used, is Articles, and its code is as follows:

Package Entity;

Import org.apache.struts.Apache.struts.action. *;

Import javax.servlet.http. *;

Import java.util.collection;

Public class articleclassform extends actionform {

/ / Preparation for SELECT Option

PRIVATE COLLECTION Beancollection;

Private string singleselect = ""

Private string [] beancollectionSelect = {"};

Private string articlassid;

Private string articlassname;

Private string subi; // Substate number

Private string subj; // Subclass number

PRIVATE STRING ARTICLESUBCLASSID;

PRIVATE STRING ArticlesUbClassName;

Public Collection getBeancollection () {

Return beancollection;

}

Public void setBeancollection (Collection Beancollection) {

THIS.BEANCOLLECTION = Beancollection;

}

Public string getsingLect () {

Return (this.singleSelect);

Public void setsingleselect (string singleselect) {

THIS.SINGLECT = SINGLESELECT;

}

Public string [] getBeancollectionSelect () {

Return (this.beancollectionSelect);

}

Public void setBeancollectionSelect (String BeancollectionSelect "{

THIS.BeancollectionSelect = beancollectionSelect;

}

Public string getArticleClassID () {

Return articles;

}

Public void setArticleClassId (string articles) {

THIS.ArticleClassID = ArticleClassID;

}

Public string getArticleClassName () {

Return articlesName;

}

Public void setArticleClassName (String articleclassname) {

THIS.ArticleClassName = ArticleClassName;

}

Public string getSubi () {

Return Subi;

}

Public void setsubi (string subi) {

THIS.SUBI = SUBI;

}

Public string getsubj () {

Return Subj;

}

Public void setsubj (String Subj) {

THIS.SUBJ = SUBJ;

}

Public string getArticlesUbclassId () {

Return ArticlesUBClassID;

}

Public void setArticlesUBClassID (String ArticlesUBClassID) {

THIS.ArticlesUBClassID = ArticlesUBClassID;

}

Public string getArticlesubclassname () {

Return ArticlesUBClassName;

}

Public void setArticlesUBClassName (String articlesubclassname) {

THIS.ArticleSubclassName = ArticlesUBClassName;

}

} Place it in the bag entry. Second, our system has to access the database, so we must establish the corresponding database access object ArticleClassDao, which is as follows:

Package DB;

Import Entity.ArticleClassform;

IMPORT DB. *;

Import java.sql. *;

Import java.util.collection;

Import java.util.arraylist;

Import org.apache.struts.util.labelvaluebean;

Public class articleclassdao {

PRIVATE CONNECTION CON;

Public ArticleClassdao (Connection Con) {

THIS.CON = con;

}

Public Collection FindinuseForselect () {

PreparedStatement PS = NULL;

ResultSet RS = NULL; ArrayList List = New ArrayList ();

String SQL = "SELECT * from articles);

Try {

IF (con?isclosed ()) {

Throw New IllegalStateException ("Error.unexpected");

}

PS = con.preparestatement (SQL);

RS = ps.executeQuery ();

While (rs.next ()) {

String value = rs.getstring ("articleclassid");

String label = rs.getstring ("articleclassname");

List.add (New LabelValuebean (Label, Value);

}

Return List;

}

Catch (SQLException E) {

E.PrintStackTrace ();

Throw new runtimeException ("error.unexpected");

}

Finally {

Try {

IF (PS! = NULL)

ps.close ();

IF (rs! = null)

Rs.close ();

}

Catch (SQLException E) {

E.PrintStackTrace ();

Throw new runtimeException ("error.unexpected");

}

}

}

Public Collection FindinuseForsubselect () {

PreparedStatement PS = NULL;

ResultSet RS = NULL;

PreparedState pssub = null;

ResultSet Rssub = NULL;

INT i = 0; // Classical record

INT j = 0; // Small class record

String classid = "";

String subclassid = "";

String subclassname = "";

ArrayList List = New ArrayList ();

ArticleClassForm Articles;

String SQL = "SELECT * from articles);

Try {

IF (con?isclosed ()) {

Throw New IllegalStateException ("Error.unexpected");

}

PS = con.preparestatement (SQL);

RS = ps.executeQuery ();

While (rs.next ()) {

i ;

ClassID = rs.getstring ("ArticleClassID");

String SQLSUB = "SELECT * from ARTICLESUBCLASS WHERE ARTICLASSID =?

ORDER BY ArticlesUBClassID ";

Pssub = con.preparestatement (SQLSUB);

Pssub.setstring (1, classid);

Rssub = pssub.executequery ();

ArticleClassform = new articleclassform (); ArticleClassform.SetSubi (" i);

ArticleClassform.SetSubj ("" j);

ArticleSusForm.SetArticlesubClassID ("Please enter a small class");

ArticleSusForm.SetArticlesubclassName ("Please enter a small class");

List.add (articlassform);

While (Rssub.next ()) {

SubclassID = rsSusub.getstring ("ArticlesUBClassID");

SubclassName = rsSusub.getstring ("ArticlesUBClassName);

J ;

// Optionstr = "ArticlesUBClassGroup [" i "] [" J "] =

New option ('" subsclassname ",' " subclassid " "";

ArticleClassform = new articles;

ArticleClassForm.SetSubi (" i);

ArticleClassform.SetSubj ("" j);

ArticleSsForm.SetArticlesubClassID (SUBCLASSID);

ArticleSsForm.SetArticlesubclassName (SubclassName);

List.add (articlassform);

}

J = 0;

}

Return List;

}

Catch (SQLException E) {

E.PrintStackTrace ();

Throw new runtimeException ("error.unexpected");

}

Finally {

Try {

IF (PS! = NULL)

ps.close ();

IF (rs! = null)

Rs.close ();

}

Catch (SQLException E) {

E.PrintStackTrace ();

Throw new runtimeException ("error.unexpected");

}

}

}

} Save it in the DB directory. The purpose is to read the class and subclass of the article from the database table, saved in a set object in a set of formats for paged display. Again, we have to establish a corresponding JSP file, the file name is Selecticleclass.jsp, the code is as follows:

<% @ Page ContentType = "Text / HTML; Charset = UTF-8"%>

<% @ 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"%>

</p> <p>Select file category</p> <p></ title></p> <p></ hEAD></p> <p><body bgcolor = "# ffffff"></p> <p><h3></p> <p>Select the type of file belong</p> <p></ h3></p> <p><html: errors /></p> <p><table width = "500" border = "0" cellspacing = "0" cellpadding = "0"></p> <p><tr></p> <p><TD> <html: form name = "articleclassform" type = "entity.Arssform"</p> <p>Action = "SelectArticleClassAction.do"></p> <p><table width = "500" border = "0" cellspacing = "0" cellpadding = "0"></p> <p><tr></p> <p><TD align = "Right"> Article Class * </ TD></p> <p><TD></p> <p><html: select protprion = "articles" styleclass = "word"</p> <p>οnchange = "Article.Options.SelectedIndIndex"></p> <p><html: option value = ""> Please select a large class </ html: option></p> <p><html: optionscollection name = "articleclassform" property = "beancollection" style = "word" /></p> <p></ html: select></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><TD Align = "Right"> Article Small Category * </ TD></p> <p><TD></p> <p><select name = "articles" class = "word"></p> <p><option value = ""> Please select a small class </ option></p> <p></ select></p> <p><Script language = javaScript></p> <p><! -</p> <p>Var articlesuBclassGroups = document.ArsSForm.ArticleClassID.</p> <p>Options.Length</p> <p>Var ArticlesUBClassGroup = New Array (ArticlesUBClassGroups)</p> <p>For (i = 0; i <articles, i )</p> <p>ArticlesUBClassGroup [i] = new array ()</p> <p><logic: Iterate Name = "ArticlesUBClassList" ID = "ArticleClassForm"</p> <p>Scope = "request" type = "entity.ArticleClassform"> ArticlesUBClassGroup [<bean: Write name = "articlassform"</p> <p>Property = "Subi" />] [<bean: write name = "articles"</p> <p>Property = "Subj" />] = new option ("<bean: write name =" articlassform "</p> <p>Property = "ArticlesUBClassName" /> "," <bean: write name = "articles"</p> <p>Property = "ArticlesUBClassID" /> ")</p> <p></ logic: Iterate></p> <p>Var articlesubclasstemp = document.ArticleClassForm.ArticlesUBClassID</p> <p>Function articleclassformredirect (x) {</p> <p>For (m = articlesubclasstemp.Options.Length-1; m> 0; M -)</p> <p>ArticlesUBClassTemp.Options [M] = NULL</p> <p>For (i = 0; i <articlesubclassgroup [x] .length; i ) {</p> <p>ArticlesUBClassTemp.Options [i] = new</p> <p>Option (ArticlesUBClassGroup [x] [i] .text,</p> <p>ArticlesUBClassGroup [x] [i] .value)</p> <p>}</p> <p>ArticlesUBClassTemp.Options [0] .selected = true</p> <p>}</p> <p>// -></p> <p></ Script></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ html: form></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ body></p> <p></ html> The JavaScript code is worth focusing here, and interested in carefully analyzing how they cooperate with elements in the collection to achieve cascade selection. Finally, for example, complete. We will involve the Action code and the necessary configuration code below: where the action of the action is SelecticleclassAction.java, the code is as follows:</p> <p>Package action;</p> <p>Import Entity. *;</p> <p>Import org.apache.struts.Apache.struts.action. *;</p> <p>Import javax.servlet.http. *;</p> <p>Import javax.sql.datasource;</p> <p>Import java.sql.connection;</p> <p>Import db.ArticleClassda;</p> <p>Import java.util.collection;</p> <p>Import java.sql.sqlexception;</p> <p>Public class selectarticleclassaction extends action {</p> <p>Public ActionForward Execute (ActionMApping ActionMApping, Actionform Actionform,</p> <p>HTTPSERVLETREQUEST HTTPSERVLETREQUEST HTTPSERVLETREQUEST, HTTPSERVLETRESPONSE HTTPSERVLETRESPONSE) {/ ** @ Todo: Complete The Business Logic Here, This Is Just A Sketon. * /</p> <p>ArticleClassForm ArticleClassform = (ArticleClassform) ActionForm;</p> <p>DataSource DataSource;</p> <p>CONNECTION CNN = NULL;</p> <p>ActionerroS Errors = new actionerrors ();</p> <p>Try {</p> <p>DataSource = GetDataSource (HttpServletRequest, "a");</p> <p>CNN = DataSource.getConnection ();</p> <p>ArticleClassdao ArticlesDAO = New Articles ;;;;;;;;;;;;;;;;;;;;;;</p> <p>Collection col = articleclassdao.findinuseforselect ();</p> <p>ArticleClassForm.SetBeancollection (COL);</p> <p>HttpservletRequest.setttribute ("ArticleClassList", COL);</p> <p>// handle subclass options</p> <p>Collection Subcol = ArticleClassDao.findinuseForsubselect ();</p> <p>HttpServletRequest.setttribute ("ArticlesUBClassList", Subcol;</p> <p>Return ActionMapping.FindForward ("Success");</p> <p>}</p> <p>Catch (throwable e) {</p> <p>E.PrintStackTrace ();</p> <p>// throw new runtimeException ("Failed to connect to Database");</p> <p>Actionerror Error = New ActionError (E.getMessage ());</p> <p>Errors.Add (ActionerRors.global_Error, Error);</p> <p>}</p> <p>Finally {</p> <p>Try {</p> <p>IF (CNN! = NULL)</p> <p>CNN.Close ();</p> <p>}</p> <p>Catch (SQLException E) {</p> <p>Throw new runtimeException (E.GetMessage ());</p> <p>}</p> <p>}</p> <p>SaveerRors (httpservletRequest, Errors);</p> <p>Return ActionMapping.FindForward ("fail");</p> <p>}</p> <p>} Save it in the Action directory. Do the following configuration in the struts-config.xml file:</p> <p>Add to <form-beans></p> <p><form-bean name = "articleclassform" type = "entity.ArticleClassform" /></p> <p>> <Action-mapings> Add:</p> <p><action name = "articlassform" path = "/ selectarticleclassaction" scope = "session"</p> <p>TYPE = "action.selectative" validate = "false"></p> <p><forward name = "success" path = "/ selectarticleclass.jsp" /> <forward name = "fail" path = "/ generrror.jsp" /></p> <p></ action> For the corresponding configuration</p> <p><forward name = "fail" Path = "/ genericerror.jsp" />, we also provide a JSP page that displays the error message, the code is as follows:</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%></p> <p><html></p> <p><HEAD></p> <p><Title></p> <p>Genericerror</p> <p></ title></p> <p><link href = "css / mycss.css" rel = "stylesheet" Type = "text / css"></p> <p></ hEAD></p> <p><body bgcolor = "# ffffff"></p> <p><html: errors /></p> <p></ body></p> <p></ html> Now everything is ready, you can compile it. Enter: http: //127.0.0.1: 8080 / mystruts / selectterClassAction.do You can see the results of this example. (T111)</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-47381.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="47381" 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.041</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 = 'Ub8ce9_2BdZBBaIiuGVREKE4dmn7KJ1dlXsme5j2S1njg_2F63USAbSq_2Bkb2mnE_2BCTpVjO1U6PGJ06a117uNSV_2BtYQ_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>