從可以看出,我們要在mystruts目錄下,建一個(gè)名為js的目錄,并將下載的dtree文件dtree.js放在該目錄中。再在mystruts目錄下分別建一個(gè)名為img和名為css的目錄,將dtree中用到的圖標和層疊樣式表單文件分別放在相應的目錄中。有關(guān)dtree的使用方法,詳見(jiàn)其說(shuō)明文檔,如:api.html。筆者在此要感謝dtree的作者為我們提供了一個(gè)結構如此清晰的javascript程序!
現在,可以編譯執行這個(gè)例子程序了,編譯后在瀏覽器中輸入:http://127.0.0.1:8080/mystruts/functionsAction.do就可以看到運行效果。
效果圖為:
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*"%>
<%@ page import="dataoil.tree.model.FunctionsForm"%>
<%@ page import="dataoil.tree.dao.FunctionsDao"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>DTree</title>
<%
String path = request.getContextPath()+"/dataoil/css/";
System.out.println("path = " + path);
%>
<link rel="StyleSheet" href="<%=path%>dtree.css" type="text/css" />
<SCRIPT type="text/javascript"></SCRIPT>
</head>
<body>
<div >
<table width="180">
<tr>
<td height="300" valign="top">
<%
try{
%>
<script type="text/javascript">
<!--
tree = new dTree('tree');
tree.config.folderLinks = false;
tree.config.useCookies = false;
<%
FunctionsDao ftd = new FunctionsDao();
List lst = ftd.findTree();
if(lst != null && lst.size() > 0) {
Iterator it = lst.iterator();
while(it.hasNext()){
FunctionsForm fut = (FunctionsForm)it.next();
%>
tree.add("<%=fut.getId()%>","<%=fut.getPid()%>","<%=fut.getName()%>","<%=fut.getUrl()%>","<%=fut.getTitle()%>","<%=fut.getTarget()%>","<%=fut.getIcon()%>");
<%
}
}
%>
document.write(tree);
//-->
</script>
<%
}catch(Exception e){
System.out.println(">>>>>>>>>>>頁(yè)面出錯>>>>>>>>>>>>>");
out.println(e.getMessage());
e.printStackTrace();
}
%>
</td>
</tr>
</table>
</div>
</body>
</html>
/*
* FileName : FunctionsDao
* Operation : 目錄樹(shù)的顯示查詢(xún)
* CreatedPerson : 高全祥
* CreatedDate : 2007-02-26
*/
package dataoil.tree.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import net.risesoft.commons.database.Conn;
import dataoil.tree.model.FunctionsForm;
public class FunctionsDao {
// /*
// * FunctionName : getConnection
// * Operation : 活的數據庫連接
// *
// */
// private Connection getConnection() {
//
// Connection conn = null;
//
// String url = "jdbc:oracle:thin:@124.128.38.237:1521:LMDSVR";
// String user = "quanxian";
// String password = "quanxian";
//
// try {
// Class.forName("oracle.jdbc.driver.OracleDriver");
// } catch (ClassNotFoundException classnotfoundexception) {
// classnotfoundexception.printStackTrace();
// }
// try {
// conn = DriverManager.getConnection(url, user, password);
// //System.out.println(">>>>>>>>>> getConn() 執行>>>>>>>>>>>>>>");
// } catch (SQLException e) {
// e.printStackTrace();
// }
//
// return conn;
// }
/*
* FunctionName : findTree
* Operation : 查詢(xún)目錄樹(shù)
*
*/
public ArrayList findTree() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList list = new ArrayList();
String sql = "select * from functions";
try {
System.out.println(">>>>>>>>>>>>>開(kāi)始>>>>>>>>>>>>>>>>");
conn = Conn.getConnection();
System.out.println("<<<<<<<<<<<開(kāi)始獲得連接<<<<<<<<<<<<<<<");
if (!conn.isClosed()) {
System.out.println(">>>>>>>>>已經(jīng)獲得數據庫連接>>>>>>>>>>>>");
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
FunctionsForm fut = new FunctionsForm();
fut.setId(rs.getString("id") == null ? "" : rs.getString("id"));
fut.setPid(rs.getString("pid") == null ? "" : rs.getString("pid"));
fut.setName(rs.getString("name") == null ? "" : rs.getString("name"));
fut.setUrl(rs.getString("url") == null ? "" : rs.getString("url"));
fut.setTitle(rs.getString("title") == null ? "" : rs.getString("title"));
fut.setTarget(rs.getString("target") == null ? "" : rs.getString("target"));
fut.setIcon(rs.getString("icon") == null ? "" : rs.getString("icon"));
fut.setIconOpen(rs.getString("iconOpen") == null ? "" : rs.getString("iconOpen"));
fut.setOpened(rs.getString("opened") == null ? "" : rs.getString("opened"));
list.add(fut);
}
} else {
System.out.println(">>>>>>>>>沒(méi)有獲得數據庫連接>>>>>>>>>>>>");
}
} catch (SQLException e) {
System.out.println(">>>>>>>>>>>>>>出錯>>>>>>>>>>>>>>>");
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
注:dtree的下載地址為: http://www.destroydrop.com/javascripts/tree/
聯(lián)系客服