//创建JSONTree.java
package com.tree;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import net.sf.json.JSONArray;
import com.luke.hm.sf.ConnectionPool;
public class JSONTree{
private String PID;
public String getJSONString(){
Connection conn =null;
Statement st = null;
ResultSet rs = null;
ArrayList TreeNodeArray = null;
String SQLString = "SELECT * FROM S_MLS WHERE PID="+this.PID+" ORDER BY DID";
try
{
conn = ConnectionPool.getConnection();
st = conn.createStatement();
rs = st.executeQuery("SELECT PID FROM S_MLS WHERE PID>0 Group By PID Order By PID");
StringBuffer parentIDBuffer =new StringBuffer();
parentIDBuffer.append("|");
while(rs.next())
{
parentIDBuffer.append(rs.getString("PID"));
parentIDBuffer.append("|");
}
//得到所有的parentDID列表(这是一个巧妙的算法^_^)
String parentIDString = parentIDBuffer.toString();
// System.out.println(parentIDString);
rs = st.executeQuery(SQLString);
TreeNodeArray = new ArrayList();
while(rs.next())
{
JSONTreeNode TreeNode = new JSONTreeNode();
TreeNode.setId(rs.getString("DID"));
TreeNode.setText(rs.getString("JDMC"));
TreeNode.setDescription(rs.getString("JDMC"));
TreeNode.setHref("rightframe.jsp?did="+rs.getString("DID").toString());
TreeNode.setHrefTarget("rightFrame");
// System.out.println(rs.getString("DID"));
// System.out.println(parentIDString.indexOf("|"+rs.getString("DID")+"|"));
if (parentIDString.indexOf("|"+rs.getString("DID")+"|")>=0) //父节点
{
TreeNode.setCls("folder");
TreeNode.setLeaf(false);
TreeNode.setExpandable(false);
}
else //子节点
{
TreeNode.setCls("file");
TreeNode.setLeaf(true);
TreeNode.setExpandable(false);
}
TreeNodeArray.add(TreeNode);
}
// System.out.println(TreeNodeArray.size());
// System.out.println(JSONArray.fromObject(TreeNodeArray));
JSONArray jsonarr = JSONArray.fromObject(TreeNodeArray); //得到JSON数组
return jsonarr.toString();//返回JSON数据
}
catch(Exception e)
{
System.out.println("getJSONString() of JSONTree.java throws : "+e.toString());
return "";
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String getPID() {
return PID;
}
public void setPID(String pid) {
PID = pid;
}
public static void main(String[] args) {
// JSONTree tree = new JSONTree();
// tree.setPID("1");
// String tt = tree.getJSONString();
// System.out.println(tt);
}
}
//创建JSONTreeNode
package com.tree;
import java.io.Serializable;
public class JSONTreeNode implements Serializable{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
}
private String id; //ID
private String text; //节点显示
private String cls; //图标
private boolean leaf; //是否叶子
private String href; //链接
private String hrefTarget; //链接指向
private boolean expandable; //是否展开
private String description; //描述信息
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getCls() {
return cls;
}
public void setCls(String cls) {
this.cls = cls;
}
public boolean isLeaf() {
return leaf;
}
public void setLeaf(boolean leaf) {
this.leaf = leaf;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public String getHrefTarget() {
return hrefTarget;
}
public void setHrefTarget(String hrefTarget) {
this.hrefTarget = hrefTarget;
}
public boolean isExpandable() {
return expandable;
}
public void setExpandable(boolean expandable) {
this.expandable = expandable;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
//get、set方法。略。
}
//创建index.jsp
<%@page contentType="text/html; charset=GBK"%>
<%@page import="com.luke.hm.tree.EJBContext"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="<%=EJBContext.getRootPath()%>/ext/resources/css/ext-all.css"/>
<script type="text/javascript" src="<%=EJBContext.getRootPath()%>/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="<%=EJBContext.getRootPath()%>/ext/ext-all.js"></script>
<script type="text/javascript" src="./mytree.js"></script>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" onContextMenu="return false;">
<tr>
<td align="left" valign="top" >
<!--树的填充区域-->
<div id="tree_div" style="height:101%;width:100%"></div>
</td>
</tr>
</table>
</body>
</html>
//创建Tree.jsp
<%@ page language="java" pageEncoding="GBK"%>
<jsp:useBean class="com.tree.JSONTree" id="JSONTree"></jsp:useBean>
<%
String PID = "";
if (request.getParameter("DID")!=null)
{
PID = request.getParameter("DID").toString();
}
JSONTree.setPID(PID);
// System.out.println(JSONTree.getJSONString());
%>
<%=JSONTree.getJSONString()%>
//创建mytree.js
Ext.onReady(function(){
var Tree = Ext.tree;
//定义根节点的Loader
var treeloader=new Tree.TreeLoader({dataUrl:'tree.jsp?DID=1'});
//异步加载根节点
var rootnode=new Tree.AsyncTreeNode({
id:'1',
text:'目录树根节点'
});
var treepanel = new Tree.TreePanel({
//renderTo:"tree_div",//如果使用renderTo,则不能使用setRootNode()方法,需要在TreePanel中设置root属性。
el:'tree_div', //填充区域
rootVisible:true, //隐藏根节点
border:true, //边框
animate:true, //动画效果
autoScroll:true, //自动滚动
enableDD:false, //拖拽节点
containerScroll:true,
loader:treeloader
});
//设置根节点
treepanel.setRootNode(rootnode);
//响应事件,传递node参数
treepanel.on('beforeload',
function(node){
treepanel.loader.dataUrl='tree.jsp?DID='+node.id; //定义子节点的Loader
});
treepanel.render();
rootnode.expand(false,false);
});
别忘记把json.jar加进去
库表设计
DID PID JDMC
package com.tree;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import net.sf.json.JSONArray;
import com.luke.hm.sf.ConnectionPool;
public class JSONTree{
private String PID;
public String getJSONString(){
Connection conn =null;
Statement st = null;
ResultSet rs = null;
ArrayList TreeNodeArray = null;
String SQLString = "SELECT * FROM S_MLS WHERE PID="+this.PID+" ORDER BY DID";
try
{
conn = ConnectionPool.getConnection();
st = conn.createStatement();
rs = st.executeQuery("SELECT PID FROM S_MLS WHERE PID>0 Group By PID Order By PID");
StringBuffer parentIDBuffer =new StringBuffer();
parentIDBuffer.append("|");
while(rs.next())
{
parentIDBuffer.append(rs.getString("PID"));
parentIDBuffer.append("|");
}
//得到所有的parentDID列表(这是一个巧妙的算法^_^)
String parentIDString = parentIDBuffer.toString();
// System.out.println(parentIDString);
rs = st.executeQuery(SQLString);
TreeNodeArray = new ArrayList();
while(rs.next())
{
JSONTreeNode TreeNode = new JSONTreeNode();
TreeNode.setId(rs.getString("DID"));
TreeNode.setText(rs.getString("JDMC"));
TreeNode.setDescription(rs.getString("JDMC"));
TreeNode.setHref("rightframe.jsp?did="+rs.getString("DID").toString());
TreeNode.setHrefTarget("rightFrame");
// System.out.println(rs.getString("DID"));
// System.out.println(parentIDString.indexOf("|"+rs.getString("DID")+"|"));
if (parentIDString.indexOf("|"+rs.getString("DID")+"|")>=0) //父节点
{
TreeNode.setCls("folder");
TreeNode.setLeaf(false);
TreeNode.setExpandable(false);
}
else //子节点
{
TreeNode.setCls("file");
TreeNode.setLeaf(true);
TreeNode.setExpandable(false);
}
TreeNodeArray.add(TreeNode);
}
// System.out.println(TreeNodeArray.size());
// System.out.println(JSONArray.fromObject(TreeNodeArray));
JSONArray jsonarr = JSONArray.fromObject(TreeNodeArray); //得到JSON数组
return jsonarr.toString();//返回JSON数据
}
catch(Exception e)
{
System.out.println("getJSONString() of JSONTree.java throws : "+e.toString());
return "";
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String getPID() {
return PID;
}
public void setPID(String pid) {
PID = pid;
}
public static void main(String[] args) {
// JSONTree tree = new JSONTree();
// tree.setPID("1");
// String tt = tree.getJSONString();
// System.out.println(tt);
}
}
//创建JSONTreeNode
package com.tree;
import java.io.Serializable;
public class JSONTreeNode implements Serializable{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
}
private String id; //ID
private String text; //节点显示
private String cls; //图标
private boolean leaf; //是否叶子
private String href; //链接
private String hrefTarget; //链接指向
private boolean expandable; //是否展开
private String description; //描述信息
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getCls() {
return cls;
}
public void setCls(String cls) {
this.cls = cls;
}
public boolean isLeaf() {
return leaf;
}
public void setLeaf(boolean leaf) {
this.leaf = leaf;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public String getHrefTarget() {
return hrefTarget;
}
public void setHrefTarget(String hrefTarget) {
this.hrefTarget = hrefTarget;
}
public boolean isExpandable() {
return expandable;
}
public void setExpandable(boolean expandable) {
this.expandable = expandable;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
//get、set方法。略。
}
//创建index.jsp
<%@page contentType="text/html; charset=GBK"%>
<%@page import="com.luke.hm.tree.EJBContext"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="<%=EJBContext.getRootPath()%>/ext/resources/css/ext-all.css"/>
<script type="text/javascript" src="<%=EJBContext.getRootPath()%>/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="<%=EJBContext.getRootPath()%>/ext/ext-all.js"></script>
<script type="text/javascript" src="./mytree.js"></script>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" onContextMenu="return false;">
<tr>
<td align="left" valign="top" >
<!--树的填充区域-->
<div id="tree_div" style="height:101%;width:100%"></div>
</td>
</tr>
</table>
</body>
</html>
//创建Tree.jsp
<%@ page language="java" pageEncoding="GBK"%>
<jsp:useBean class="com.tree.JSONTree" id="JSONTree"></jsp:useBean>
<%
String PID = "";
if (request.getParameter("DID")!=null)
{
PID = request.getParameter("DID").toString();
}
JSONTree.setPID(PID);
// System.out.println(JSONTree.getJSONString());
%>
<%=JSONTree.getJSONString()%>
//创建mytree.js
Ext.onReady(function(){
var Tree = Ext.tree;
//定义根节点的Loader
var treeloader=new Tree.TreeLoader({dataUrl:'tree.jsp?DID=1'});
//异步加载根节点
var rootnode=new Tree.AsyncTreeNode({
id:'1',
text:'目录树根节点'
});
var treepanel = new Tree.TreePanel({
//renderTo:"tree_div",//如果使用renderTo,则不能使用setRootNode()方法,需要在TreePanel中设置root属性。
el:'tree_div', //填充区域
rootVisible:true, //隐藏根节点
border:true, //边框
animate:true, //动画效果
autoScroll:true, //自动滚动
enableDD:false, //拖拽节点
containerScroll:true,
loader:treeloader
});
//设置根节点
treepanel.setRootNode(rootnode);
//响应事件,传递node参数
treepanel.on('beforeload',
function(node){
treepanel.loader.dataUrl='tree.jsp?DID='+node.id; //定义子节点的Loader
});
treepanel.render();
rootnode.expand(false,false);
});
别忘记把json.jar加进去
库表设计
DID PID JDMC