这里只介绍实现的流程及步骤,方便以后查阅,并没有考虑代码的优化及其它的方面,有兴趣的可以自己去修改!
另外,这里只是实现显示树,并没有实现节点的添加及删除,有需要的可以自己去完成!
首先,建立数据库表,treeTabel
create table treeTables
(
treeId int identity(1,1) primary key, //id标识列,自动增长
treeName varchar(20), //节点名称
parent_treeId int //父节点id
)
建立数据库的操作类,得到树的详细信息
package com.tree.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.sql.rowset.CachedRowSet;
import com.sun.rowset.CachedRowSetImpl;
public class TreeService {
private TreeService(){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static TreeService treeService;
public static TreeService getInstance(){
if(treeService == null)
treeService = new TreeService();
return treeService;
}
public Connection getConn() throws Exception{
return DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=testDB","sa","");
}
/**
* 得到树信息
* @return
* @throws Exception
*/
public Treeinfo[] getAllTreeinfo() throws Exception{
Connection conn = getConn();
PreparedStatement smt = conn.prepareStatement("select * from treeTable where parent_treeId = -1");
CachedRowSet cs = new CachedRowSetImpl();
cs.populate(smt.executeQuery());
List<Treeinfo> treeinfoList = new ArrayList<Treeinfo>();
while(cs.next()){
Treeinfo treeinfo = new Treeinfo();
treeinfo.setTreeId(cs.getInt(1));
treeinfo.setNodeName(cs.getString(2));
treeinfo.setParentId(cs.getInt(3));
treeinfo.setChildren(getChildren(treeinfo.getTreeId()));
treeinfoList.add(treeinfo);
}
cs.close();
smt.close();
conn.close();
Treeinfo[] treeinfo = new Treeinfo[treeinfoList.size()];
treeinfo = treeinfoList.toArray(treeinfo);
return treeinfo;
}
/**
* 得到当前id的子节点信息,这里用了递归调用
* @param treeNodeId
* @return
* @throws Exception
*/
public Treeinfo[] getChildren(int treeNodeId) throws Exception{
Connection conn = getConn();
PreparedStatement smt = conn.prepareStatement("select * from treeTable where parent_treeId = ?");
smt.setInt(1, treeNodeId);
ResultSet rs = smt.executeQuery();
List<Treeinfo> childList = new ArrayList<Treeinfo>();
while(rs.next()){
Treeinfo treeinfo = new Treeinfo();
treeinfo.setTreeId(rs.getInt(1));
treeinfo.setNodeName(rs.getString(2));
treeinfo.setParentId(rs.getInt(3));
treeinfo.setChildren(getChildren(treeinfo.getTreeId()));
childList.add(treeinfo);
}
rs.close();
smt.close();
conn.close();
Treeinfo[] childResult = new Treeinfo[childList.size()];
childResult = childList.toArray(childResult);
return childResult;
}
}
建立中间层Action类
package com.tree.test;
import com.opensymphony.xwork2.ActionSupport;
public class TreeAction extends ActionSupport {
private Treeinfo root;
@Override
public String execute() throws Exception {
root = new Treeinfo();
root.setTreeId(-1);
root.setNodeName("root");
root.setChildren(TreeService.getInstance().getAllTreeinfo());
return SUCCESS;
}
public Treeinfo getRoot() {
return root;
}
public void setRoot(Treeinfo root) {
this.root = root;
}
}
最后新建显示树形菜单的jsp页面
<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'testtree.jsp' starting page</title>
<s:head theme="ajax" debug="true" />
<script language="JavaScript">
function treeNodeSelected(arg) {
alert("id["+arg.source.widgetId+"], name["+ arg.source.title+ "] selected");
}
dojo.addOnLoad(function() {
var s = dojo.widget.byId('treeTestId').selector;
dojo.event.connect(s, 'select', 'treeNodeSelected');
});
</script>
</head>
<body>
<s:tree id="treeTestId"
theme="ajax"
rootNode="root"
childCollectionProperty="children"
nodeIdProperty="treeId"
nodeTitleProperty="nodeName"
treeSelectedTopic="treeSelected">
</s:tree>
</body>
</html>
特别注意,要在页面引入 <s:head theme="ajax" debug="true" />