从可以看出,我们要在mystruts目录下,建一个名为js的目录,并将下载的dtree文件dtree.js放在该目录中。再在mystruts目录下分别建一个名为img和名为css的目录,将dtree中用到的图标和层叠样式表单文件分别放在相应的目录中。有关dtree的使用方法,详见其说明文档,如:api.html。笔者在此要感谢dtree的作者为我们提供了一个结构如此清晰的javascript程序!
现在,可以编译执行这个例子程序了,编译后在浏览器中输入: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(">>>>>>>>>>>页面出错>>>>>>>>>>>>>");
out.println(e.getMessage());
e.printStackTrace();
}
%>
</td>
</tr>
</table>
</div>
</body>
</html>
/*
* FileName : FunctionsDao
* Operation : 目录树的显示查询
* 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 : 查询目录树
*
*/
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(">>>>>>>>>>>>>开始>>>>>>>>>>>>>>>>");
conn = Conn.getConnection();
System.out.println("<<<<<<<<<<<开始获得连接<<<<<<<<<<<<<<<");
if (!conn.isClosed()) {
System.out.println(">>>>>>>>>已经获得数据库连接>>>>>>>>>>>>");
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(">>>>>>>>>没有获得数据库连接>>>>>>>>>>>>");
}
} 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./javascripts/tree/