Struts Principles and Practice (8) Author: Zhang Mei Luo Kuaibo dispatch time: 2004.11.05
In the previous article, the JavaScript Implementation Class Decoction menu is introduced. This article continues to introduce an example of a tree menu using existing JavaScript code to form a tree menu. Everyone knows that the tree menu has a wide range of uses in the app. There are many ways to implement the tree type menu. This article describes the understanding of understanding, similar to the method of the last article: is to save the node of the tree menu in the database table (of course, in the actual project, node Information is often not placed in a single table. For example: in a permission management system, this information may be placed in the user table, role table, menu, etc., as long as you try to get the results of the query and below The content given is similar to the content. As long as some database knowledge is not difficult, the detailed implementation details are beyond the theme of this article, not here to say). By the data access object, it is placed in a collection object from the database, and the collection object is passed to the client, and then use a existing JavaScript code - DTREE (a free JavaScript program) to operate the collection The data. After the big direction is determined, we will make it specific to implement it. According to DTREE requirements, let's build a database table to store the node information of the tree, named functions, the structure is as follows:
ID field: varchar 10 primary key - node identification code
PID field: varchar 10 not null - parent node identification code
Name field: varchar 20 not null
URL field: VARCHAR 50 NOT
NULL - This field is stored at the time of clicking this node, such as a URL of a page),
In order not to make this article too long, no corresponding page is given,
You can enter an alphabet such as: a to enable this example.
Title field: VARCHAR 20
Target Field: VARCHAR 10
Icon Field: VARCHAR 20
Iconopen field: VARCHAR 20
Opened field: char 1 Enter some records in the table to supply later:
0, -1, my permissions, JavaScript: void (0);
00, 0, user management, javascript: void (0);
0001,00, create new users;
0002,00, delete users;
01, 0, article management, JavaScript: void (0);
0101, 01, add new articles;
0102, 01, modify articles;
0103, 01, delete articles; here, the database preparation work will take a paragraph. The next job We are still in the MYSTRUTS project previously introduced. Write a named: Functionsform's ActionForm, which is as follows:
Package Entity;
Import org.apache.struts.Apache.struts.action. *;
Import javax.servlet.http. *;
Public class functionsform extends actionform {
PRIVATE STRING ICON;
Private string iConopen;
Private string id;
PRIVATE STRING NAME;
PRIVATE STRING OPENED;
PRIVATE STRING PID;
PRIVATE STRING TARGET;
PRIVATE STRING TITLE;
Private string URL;
Public string geticon () {
Return icon;
}
Public void seticon (String icon) {
THIS.ICON = icon;
}
Public String geticonopen () {
Return Iconopen;
}
Public void seticonopen (String iconopen) {
this.iconopen = iconopen;
}
Public string getId () {
Return ID;
}
Public void setid (String ID) {
THIS.ID = ID;
}
Public string getname () {
Return Name;
}
Public void setname (String name) {
THIS.NAME = Name;
}
Public string getopet () {
Return OPENED;
}
Public void setpened (string opened) {
THIS.OPENED = OPENED;
}
Public string getpid () {
Return PID;
}
Public void setpid (string pid) {
THIS.PID = PID;
}
Public string gettarget () {
Return Target;
}
Public void setTarget (String Target) {
THIS.TARGET = Target;
}
Public string gettitle () {
Return Title;
}
Public void setTitle (String Title) {
THIS.TITLE = Title;
}
Public string geturl () {
Return URL;
}
Public void seturl (String URL) {
this.url = URL;
}
} Because the data of our tree node is stored in the database table, then you want to make a data access object class, the name is: functionsDao.java, the code is as follows:
Package DB;
Import java.sql. *;
Import java.util. *;
Import Entity.Functionsform;
Public class functionsdao {
Private static connection con = NULL;
Public functionsdao (connect con) {
THIS.CON = con;
}
Public static collectiment findtree () {
PreparedStatement PS = NULL;
ResultSet RS = NULL;
ArrayList List = New ArrayList ();
String SQL = "SELECT * from functions";
Try {
IF (con?isclosed ()) {
Throw New IllegalStateException ("Error.unexpected");
}
PS = con.preparestatement (SQL);
RS = ps.executeQuery ();
While (rs.next ()) {
Functionsform Functionsform = New functionsform (); functionsform.setid (rs.getstring ("id"));
Functionsform.SetPid (Rs.getstring ("PID"));
Functionsform.SetName (Rs.getstring ("name");
Functionsform.SetURL (RS.GetString ("URL"));
Functionsform.Settitle (RS.GetString ("Title");
Functionsform.SetTarget (RS.GetString ("Target");
Functionsform.Seticon (rs.getstring ("icon"));
Functionsform.Seticonopen ("iconopen");
Functionsform.Setopened (Rs.getstring ("OPENED"));
List.add (functionsform);
}
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");
}
}
}
} This is worth noting that some programs we have seen in the past, such as: Some ASP programs often simply use the recursive call method to find each node of the tree. This is still some useful, but this processing method also has a fatal weakness, which is repeated database query, and more applications for some nodes, the impact on application performance is very large. So sometimes it is difficult to accept; and in most cases in the actual application, the depth of the tree is often limited, such as: the tree used for accounting accounts is generally up to six layers. Another example: the case of the web function menu, the principle of web design is: reaching the final destination, the number of mouse clicks is not more than three times. Therefore, consider the efficiency of the query when the table actually designed the storage tree structure. To determine the maximum depth of the tree, try to optimize the query statement, reduce the number of queries, to improve the performance of the application while reducing the load load. This example corresponds to the name of the Action, which is as follows:
Package action;
Import Entity. *;
Import org.apache.struts.Apache.struts.action. *;
Import javax.servlet.http. *;
Import javax.sql.datasource;
Import java.sql.connection;
Import java.sql.sqlexception;
Import java.util.collection;
Import db.functionsdao;
Public class functionSaction extends action {
Public ActionForward Execute (ActionMApping ActionMApping, Actionform Actionform, HttpservletRequest HttpServletRequest, HttpServletResponse HttpservletResponse)
{
DataSource DataSource;
CONNECTION CNN = NULL;
ActionerroS Errors = new actionerrors ();
Try {
DataSource = GetDataSource (HttpServletRequest, "a");
CNN = DataSource.getConnection ();
Functionsdao functionsdao = new functionsdao (cnn);
Collection col = functionsdao.findtree ();
HTTPSERVLETREQUEST.SetAttribute ("treelist", col);
Return ActionMapping.FindForward ("Success");
}
Catch (throwable e) {
E.PrintStackTrace ();
// throw new runtimeException ("Failed to connect to Database");
Actionerror Error = New ActionError (E.getMessage ());
Errors.Add (ActionerRors.global_Error, Error);
}
Finally {
Try {
IF (CNN! = NULL)
CNN.Close ();
}
Catch (SQLException E) {
Throw new runtimeException (E.GetMessage ());
}
}
SaveerRors (httpservletRequest, Errors);
Return ActionMapping.FindForward ("fail");
}
} Add the following in the Struts-Config.xml file:
form-beans>
Type = "action.functions" VALIDATE = "false"> action> action-mappings> For the corresponding configuration We also provide a JSP page that displays the error message, which is as follows: <% @ Page ContentType = "Text / HTML; Charset = UTF-8"%> <% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>
Genericerror
title>
hEAD>
body>
html> Let's take a look at the page code we show the tree menu. It can be seen from the configuration that the name of the page is TestDtree.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"%>
Testdtree
title>
hEAD>