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"%>
Select file category
title>
hEAD>
Select the type of file belong
h3>