JSP 动态树的实现
程序员文章站
2023-12-01 08:22:16
第一步:在开始之前我们需要准备这么一个js文件,代码如下。我姑且将它命名为tree.js。复制代码 代码如下:function node(id, pid, name, ur...
第一步:在开始之前我们需要准备这么一个js文件,代码如下。我姑且将它命名为tree.js。
function node(id, pid, name, url, title, target, icon, iconopen, open, appendedstr) {
this.id = id;
this.pid = pid;
this.name = name;
this.url = url;
this.title = title;
this.target = target;
this.icon = icon;
this.iconopen = iconopen;
this.appendedstr = appendedstr;
this._io = open || false;
this._is = false;
this._ls = false;
this._hc = false;
this._ai = 0;
this._p;
};
// tree object
function tree(objname,path) {
this.path = path;
this.config = {
target : null,
folderlinks : true,
useselection : true,
usecookies : true,
uselines : true,
useicons : true,
usestatustext : false,
closesamelevel : false,
inorder : false
}
this.icon = {
root : path + 'treedemo/images/tree_base.gif',
folder : path + 'treedemo/images/tree_folder.gif',
folderopen : path + 'treedemo/images/tree_folderopen.gif',
node : path + 'treedemo/images/tree_folder.gif',
empty : path + 'treedemo/images/tree_empty.gif',
line : path + 'treedemo/images/tree_line.gif',
join : path + 'treedemo/images/tree_join.gif',
joinbottom : path + 'treedemo/images/tree_joinbottom.gif',
plus : path + 'treedemo/images/tree_plus.gif',
plusbottom : path + 'treedemo/images/tree_plusbottom.gif',
minus : path + 'treedemo/images/tree_minus.gif',
minusbottom : path + 'treedemo/images/tree_minusbottom.gif',
nlplus : path + 'treedemo/images/tree_nolines_plus.gif',
nlminus : path + 'treedemo/images/tree_nolines_minus.gif'
};
this.obj = objname;
this.anodes = [];
this.aindent = [];
this.root = new node(-1);
this.selectednode = null;
this.selectedfound = false;
this.completed = false;
};
// adds a new node to the node array
tree.prototype.add = function(id, pid, name, url, title, target, icon, iconopen, open, appendedstr) {
this.anodes[this.anodes.length] = new node(id, pid, name, url, title, target, icon, iconopen, open, appendedstr);
};
// open/close all nodes
tree.prototype.openall = function() {
this.oall(true);
};
tree.prototype.closeall = function() {
this.oall(false);
};
// outputs the tree to the page
tree.prototype.tostring = function() {
var str = '<div class="tree">\n';
if (document.getelementbyid) {
if (this.config.usecookies) this.selectednode = this.getselected();
str += this.addnode(this.root);
} else str += 'browser not supported.';
str += '</div>';
if (!this.selectedfound) this.selectednode = null;
this.completed = true;
return str;
};
// creates the tree structure
tree.prototype.addnode = function(pnode) {
var str = '';
var n=0;
if (this.config.inorder) n = pnode._ai;
for (n; n<this.anodes.length; n++) {
if (this.anodes[n].pid == pnode.id) {
var cn = this.anodes[n];
cn._p = pnode;
cn._ai = n;
this.setcs(cn);
if (!cn.target && this.config.target) cn.target = this.config.target;
if (cn._hc && !cn._io && this.config.usecookies) cn._io = this.isopen(cn.id);
if (!this.config.folderlinks && cn._hc) cn.url = null;
if (this.config.useselection && cn.id == this.selectednode && !this.selectedfound) {
cn._is = true;
this.selectednode = n;
this.selectedfound = true;
}
str += this.node(cn, n);
if (cn._ls) break;
}
}
return str;
};
// creates the node icon, url and text
tree.prototype.node = function(node, nodeid) {
var str = '<div class="treenode">' + this.indent(node, nodeid);
if (this.config.useicons) {
if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
if (!node.iconopen) node.iconopen = (node._hc) ? this.icon.folderopen : this.icon.node;
if (this.root.id == node.pid) {
node.icon = this.icon.root;
node.iconopen = this.icon.root;
}
str += '<img id="i' + this.obj + nodeid + '" src="' + ((node._io) ? node.iconopen : node.icon) + '" src="' + ((node._io) ? node.iconopen : node.icon) + '" alt="" />';
}
if (node.url) {
str += '<a id="s' + this.obj + nodeid + '" class="' + ((this.config.useselection) ? ((node._is ? 'nodesel' : 'node')) : 'node') + '" href="' + node.url + '" href="' + node.url + '"';
if (node.title) str += ' title="' + node.title + '"';
if (node.target) str += ' target="' + node.target + '"';
if (this.config.usestatustext) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
if (this.config.useselection && ((node._hc && this.config.folderlinks) || !node._hc))
str += ' onclick="javascript: ' + this.obj + '.s(' + nodeid + ');"';
str += '>';
}
else if ((!this.config.folderlinks || !node.url) && node._hc && node.pid != this.root.id)
str += '<a href="javascript: ' + this.obj + '.o(' + nodeid + ');" href="javascript: ' + this.obj + '.o(' + nodeid + ');" class="node">';
str += node.name;
if (node.url || ((!this.config.folderlinks || !node.url) && node._hc)) str += '</a>';
//[!--begin--]add by wangxr to append str
if(node.appendedstr) str += node.appendedstr;
//[!--end--]
str += '</div>';
if (node._hc) {
str += '<div id="d' + this.obj + nodeid + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
str += this.addnode(node);
str += '</div>';
}
this.aindent.pop();
return str;
};
// adds the empty and line icons
tree.prototype.indent = function(node, nodeid) {
var str = '';
if (this.root.id != node.pid) {
for (var n=0; n<this.aindent.length; n++)
str += '<img src="' + ( (this.aindent[n] == 1 && this.config.uselines) ? this.icon.line : this.icon.empty ) + '" src="' + ( (this.aindent[n] == 1 && this.config.uselines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
(node._ls) ? this.aindent.push(0) : this.aindent.push(1);
if (node._hc) {
str += '<a href="javascript: ' + this.obj + '.o(' + nodeid + ');" href="javascript: ' + this.obj + '.o(' + nodeid + ');"><img id="j' + this.obj + nodeid + '" src="';
if (!this.config.uselines) str += (node._io) ? this.icon.nlminus : this.icon.nlplus;
else str += ( (node._io) ? ((node._ls && this.config.uselines) ? this.icon.minusbottom : this.icon.minus) : ((node._ls && this.config.uselines) ? this.icon.plusbottom : this.icon.plus ) );
str += '" src="'; if (!this.config.uselines) str += (node._io) ? this.icon.nlminus : this.icon.nlplus; else str += ( (node._io) ? ((node._ls && this.config.uselines) ? this.icon.minusbottom : this.icon.minus) : ((node._ls && this.config.uselines) ? this.icon.plusbottom : this.icon.plus ) ); str += '" alt="" /></a>';
} else str += '<img src="' + ( (this.config.uselines) ? ((node._ls) ? this.icon.joinbottom : this.icon.join ) : this.icon.empty) + '" src="' + ( (this.config.uselines) ? ((node._ls) ? this.icon.joinbottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
}
return str;
};
// checks if a node has any children and if it is the last sibling
tree.prototype.setcs = function(node) {
var lastid;
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n].pid == node.id) node._hc = true;
if (this.anodes[n].pid == node.pid) lastid = this.anodes[n].id;
}
if (lastid==node.id) node._ls = true;
};
// returns the selected node
tree.prototype.getselected = function() {
var sn = this.getcookie('cs' + this.obj);
return (sn) ? sn : null;
};
// highlights the selected node
tree.prototype.s = function(id) {
if (!this.config.useselection) return;
var cn = this.anodes[id];
if (cn._hc && !this.config.folderlinks) return;
if (this.selectednode != id) {
if (this.selectednode || this.selectednode==0) {
eold = document.getelementbyid("s" + this.obj + this.selectednode);
eold.classname = "node";
}
enew = document.getelementbyid("s" + this.obj + id);
enew.classname = "nodesel";
this.selectednode = id;
if (this.config.usecookies) this.setcookie('cs' + this.obj, cn.id);
}
};
// toggle open or close
tree.prototype.o = function(id) {
var cn = this.anodes[id];
this.nodestatus(!cn._io, id, cn._ls);
cn._io = !cn._io;
if (this.config.closesamelevel) this.closelevel(cn);
if (this.config.usecookies) this.updatecookie();
};
// open or close all nodes
tree.prototype.oall = function(status) {
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n]._hc && this.anodes[n].pid != this.root.id) {
this.nodestatus(status, n, this.anodes[n]._ls)
this.anodes[n]._io = status;
}
}
if (this.config.usecookies) this.updatecookie();
};
// opens the tree to a specific node
tree.prototype.opento = function(nid, bselect, bfirst) {
if (!bfirst) {
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n].id == nid) {
nid=n;
break;
}
}
}
var cn=this.anodes[nid];
if (cn.pid==this.root.id || !cn._p) return;
cn._io = true;
cn._is = bselect;
if (this.completed && cn._hc) this.nodestatus(true, cn._ai, cn._ls);
if (this.completed && bselect) this.s(cn._ai);
else if (bselect) this._sn=cn._ai;
this.opento(cn._p._ai, false, true);
};
// closes all nodes on the same level as certain node
tree.prototype.closelevel = function(node) {
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n].pid == node.pid && this.anodes[n].id != node.id && this.anodes[n]._hc) {
this.nodestatus(false, n, this.anodes[n]._ls);
this.anodes[n]._io = false;
this.closeallchildren(this.anodes[n]);
}
}
}
// closes all children of a node
tree.prototype.closeallchildren = function(node) {
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n].pid == node.id && this.anodes[n]._hc) {
if (this.anodes[n]._io) this.nodestatus(false, n, this.anodes[n]._ls);
this.anodes[n]._io = false;
this.closeallchildren(this.anodes[n]);
}
}
}
// change the status of a node(open or closed)
tree.prototype.nodestatus = function(status, id, bottom) {
ediv = document.getelementbyid('d' + this.obj + id);
ejoin = document.getelementbyid('j' + this.obj + id);
if (this.config.useicons) {
eicon = document.getelementbyid('i' + this.obj + id);
eicon.src = (status) ? this.anodes[id].iconopen : this.anodes[id].icon;
}
ejoin.src = (this.config.uselines)?
((status)?((bottom)?this.icon.minusbottom:this.icon.minus):((bottom)?this.icon.plusbottom:this.icon.plus)):
((status)?this.icon.nlminus:this.icon.nlplus);
ediv.style.display = (status) ? 'block': 'none';
};
// [cookie] clears a cookie
tree.prototype.clearcookie = function() {
var now = new date();
var yesterday = new date(now.gettime() - 1000 * 60 * 60 * 24);
this.setcookie('co'+this.obj, 'cookievalue', yesterday);
this.setcookie('cs'+this.obj, 'cookievalue', yesterday);
};
// [cookie] sets value in a cookie
tree.prototype.setcookie = function(cookiename, cookievalue, expires, path, domain, secure) {
document.cookie =
escape(cookiename) + '=' + escape(cookievalue)
+ (expires ? '; expires=' + expires.togmtstring() : '')
+ (path ? '; path=' + path : '')
+ (domain ? '; domain=' + domain : '')
+ (secure ? '; secure' : '');
};
// [cookie] gets a value from a cookie
tree.prototype.getcookie = function(cookiename) {
var cookievalue = '';
var posname = document.cookie.indexof(escape(cookiename) + '=');
if (posname != -1) {
var posvalue = posname + (escape(cookiename) + '=').length;
var endpos = document.cookie.indexof(';', posvalue);
if (endpos != -1) cookievalue = unescape(document.cookie.substring(posvalue, endpos));
else cookievalue = unescape(document.cookie.substring(posvalue));
}
return (cookievalue);
};
// [cookie] returns ids of open nodes as a string
tree.prototype.updatecookie = function() {
var str = '';
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n]._io && this.anodes[n].pid != this.root.id) {
if (str) str += '.';
str += this.anodes[n].id;
}
}
this.setcookie('co' + this.obj, str);
};
// [cookie] checks if a node id is in a cookie
tree.prototype.isopen = function(id) {
var aopen = this.getcookie('co' + this.obj).split('.');
for (var n=0; n<aopen.length; n++)
if (aopen[n] == id) return true;
return false;
};
// if push and pop is not implemented by the browser
if (!array.prototype.push) {
array.prototype.push = function array_push() {
for(var i=0;i<arguments.length;i++)
this[this.length]=arguments[i];
return this.length;
}
};
if (!array.prototype.pop) {
array.prototype.pop = function array_pop() {
lastelement = this[this.length-1];
this.length = math.max(this.length-1,0);
return lastelement;
}
};
由于代码太长,我这里就不给大家拿来细讲了,我们只要会用就ok。就像猪肉我们能吃就ok,不一定非要知道猪养。
第二步:创建数据库,创建数据库的代码如下,我这边使用的是mysql数据为。
create database `treedemo`;
use treedemo;
create table trees(
tid int primary key not null,
pid int not null,
tname varchar(50) not null,
isleaf int
);
select * from trees;
insert into trees(tid,pid,tname)values(0,-1,'组织内容');
insert into trees(tid,pid,tname)values(1,0,'短信');
insert into trees(tid,pid,tname)values(2,0,'彩信');
insert into trees(tid,pid,tname)values(3,0,'新闻');
insert into trees(tid,pid,tname)values(4,1,'移动生活');
insert into trees(tid,pid,tname)values(5,1,'单条滚动点播');
insert into trees(tid,pid,tname)values(6,2,'定制');
insert into trees(tid,pid,tname)values(7,2,'点播');
insert into trees(tid,pid,tname)values(8,3,'房产频道');
insert into trees(tid,pid,tname)values(9,3,'农村频道');
insert into trees(tid,pid,tname)values(10,3,'数码频道');
insert into trees(tid,pid,tname)values(11,6,'幽默笑话');
insert into trees(tid,pid,tname)values(12,7,'铃声');
insert into trees(tid,pid,tname)values(13,7,'贺卡');
insert into trees(tid,pid,tname)values(14,7,'动画');
insert into trees(tid,pid,tname)values(15,13,'贺卡1');
insert into trees(tid,pid,tname)values(16,13,'贺卡2');
insert into trees(tid,pid,tname)values(17,13,'贺卡3');
insert into trees(tid,pid,tname)values(18,13,'贺卡4');
select * from trees;
表字段的说明:
(1)tid表示节点的编号;
(2)pid 该节点父节点的编号;
(3)tname 节点名称;
(4)isleaf 表明该节点是否为叶节点,叶节点为1,非叶节点为0。该字段可根据实际情况增删。
第三步:在myeclipse中创建一个web工程,命名为“treedemo”。在webroot文件夹下创建js文件夹、css文件夹、images文件夹分别用来存放.js文件、.css文件和项目中用到的图片文件。
第四步:编写数据库连接类,其中的用户名、密码需要根据你数据库情况进行修改,代码如下。
package com.sx.mas.utils;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;
public class dbconn {
private static final string driver="com.mysql.jdbc.driver";
private static final string url="jdbc:mysql://localhost:3306/treedemo";
connection conn=null;
public connection getconnection(){
try {
class.forname(driver);
conn=drivermanager.getconnection(url,"root","root");
} catch (classnotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return conn;
}
}
package com.sx.mas.utils;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;
public class dbconn {
private static final string driver="com.mysql.jdbc.driver";
private static final string url="jdbc:mysql://localhost:3306/treedemo";
connection conn=null;
public connection getconnection(){
try {
class.forname(driver);
conn=drivermanager.getconnection(url,"root","root");
} catch (classnotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return conn;
}
}
第五步:编写javabean类,代码如下,
package com.sx.mas.beans;
public class treenode {
private int tid;
private int pid;
private string tname;
private int isleaf;
public int gettid() {
return tid;
}
public void settid(int tid) {
this.tid = tid;
}
public int getpid() {
return pid;
}
public void setpid(int pid) {
this.pid = pid;
}
public string gettname() {
return tname;
}
public void settname(string tname) {
this.tname = tname;
}
public int getisleaf() {
return isleaf;
}
public void setisleaf(int isleaf) {
this.isleaf = isleaf;
}
}
package com.sx.mas.beans;
public class treenode {
private int tid;
private int pid;
private string tname;
private int isleaf;
public int gettid() {
return tid;
}
public void settid(int tid) {
this.tid = tid;
}
public int getpid() {
return pid;
}
public void setpid(int pid) {
this.pid = pid;
}
public string gettname() {
return tname;
}
public void settname(string tname) {
this.tname = tname;
}
public int getisleaf() {
return isleaf;
}
public void setisleaf(int isleaf) {
this.isleaf = isleaf;
}
}
第六步:创建相关的dao类,代码如下。dao类将数据库表装保存到vector对象中。
package com.sx.mas.beans;
import java.sql.connection;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.vector;
import com.sx.mas.utils.dbconn;
public class treedao {
public vector gettree(){
vector vec = new vector();
dbconn dbconn = new dbconn();
connection conn = dbconn.getconnection();
try {
statement stmt = conn.createstatement();
resultset rs = stmt.executequery("select * from trees");
while(rs.next()){
treenode treenode = new treenode();
treenode.settid(rs.getint("tid"));
treenode.setpid(rs.getint("pid"));
treenode.settname(rs.getstring("tname"));
treenode.setisleaf(rs.getint("isleaf"));
vec.add(treenode);
}
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return vec;
}
}
package com.sx.mas.beans;
import java.sql.connection;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.vector;
import com.sx.mas.utils.dbconn;
public class treedao {
public vector gettree(){
vector vec = new vector();
dbconn dbconn = new dbconn();
connection conn = dbconn.getconnection();
try {
statement stmt = conn.createstatement();
resultset rs = stmt.executequery("select * from trees");
while(rs.next()){
treenode treenode = new treenode();
treenode.settid(rs.getint("tid"));
treenode.setpid(rs.getint("pid"));
treenode.settname(rs.getstring("tname"));
treenode.setisleaf(rs.getint("isleaf"));
vec.add(treenode);
}
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return vec;
}
}
第七步:页面上显示树状结构。在treedemo中创建show_tree.jsp页面,代码如下。
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=gb2312"%>
<%@ page import="com.sx.mas.beans.*" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<link href="css/tree.css" href="css/tree.css" type=text/css rel=stylesheet>
<link href="css/css.css" href="css/css.css" rel=stylesheet>
<script src="js/tree.js" src="js/tree.js" type=text/javascript></script>
<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="styles.css" href="styles.css">
-->
</head>
<body onresize="return true;" leftmargin=1 topmargin=1>
<table>
<tr>
<td valign="top">
<table class=table_left_menu cellspacing=0 cellpadding=0 width="100%"
background=images/tree_bg.gif border=0>
<tbody>
<tr>
<td>
<div align=center><img height=24 src="images/tree_button.gif" src="images/tree_button.gif"
width=147 usemap=#map border=0>
<map id=map name=map>
<area shape="rect" shape="rect" coords="16,3,69,15" coords="16,3,69,15" href="javascript:%20d.openall()" href="javascript:%20d.openall()">
<area shape="rect" shape="rect" coords="72,3,131,15" coords="72,3,131,15" href="javascript:%20d.closeall()" href="javascript:%20d.closeall()">
</map>
</div>
</td>
</tr>
</tbody>
</table>
<script type=text/javascript>
d = new tree('d','../');
<%
treedao treedao = new treedao();
vector vec =treedao.gettree();
iterator iterator = vec.iterator();
while(iterator.hasnext()){
treenode treenode = (treenode)iterator.next();
if(treenode.getisleaf()==0){
%>
d.add(<%=treenode.gettid()%>,<%=treenode.getpid()%>,'<%=treenode.gettname()%>')
<%}else{%>
d.add(<%=treenode.gettid()%>,<%=treenode.getpid()%>,'<%=treenode.gettname()%>',parent.geturlbycatalogid('<%=treenode.gettid()%>'),null,'list');
<%}}%>
document.write(d);
</script>
</td>
</tr>
</table>
</body>
</html>
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=gb2312"%>
<%@ page import="com.sx.mas.beans.*" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<link href="css/tree.css" href="css/tree.css" type=text/css rel=stylesheet>
<link href="css/css.css" href="css/css.css" rel=stylesheet>
<script src="js/tree.js" src="js/tree.js" type=text/javascript></script>
<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="styles.css" href="styles.css">
-->
</head>
<body onresize="return true;" leftmargin=1 topmargin=1>
<table>
<tr>
<td valign="top">
<table class=table_left_menu cellspacing=0 cellpadding=0 width="100%"
background=images/tree_bg.gif border=0>
<tbody>
<tr>
<td>
<div align=center><img height=24 src="images/tree_button.gif" src="images/tree_button.gif"
width=147 usemap=#map border=0>
<map id=map name=map>
<area shape="rect" shape="rect" coords="16,3,69,15" coords="16,3,69,15" href="javascript:%20d.openall()" href="javascript:%20d.openall()">
<area shape="rect" shape="rect" coords="72,3,131,15" coords="72,3,131,15" href="javascript:%20d.closeall()" href="javascript:%20d.closeall()">
</map>
</div>
</td>
</tr>
</tbody>
</table>
<script type=text/javascript>
d = new tree('d','../');
<%
treedao treedao = new treedao();
vector vec =treedao.gettree();
iterator iterator = vec.iterator();
while(iterator.hasnext()){
treenode treenode = (treenode)iterator.next();
if(treenode.getisleaf()==0){
%>
d.add(<%=treenode.gettid()%>,<%=treenode.getpid()%>,'<%=treenode.gettname()%>')
<%}else{%>
d.add(<%=treenode.gettid()%>,<%=treenode.getpid()%>,'<%=treenode.gettname()%>',parent.geturlbycatalogid('<%=treenode.gettid()%>'),null,'list');
<%}}%>
document.write(d);
</script>
</td>
</tr>
</table>
</body>
</html>
第八步:创建一个框架页面,index.html代码如下,和list_default.htm。
<!doctype html public "-//w3c//dtd html 4.0 frameset//en">
<!-- saved from url=(0203)http://211.142.31.211:8050/cms/content/content_frame.jsp?eafplaceholder=1&staffid=11010000&ssessionid=a5704fd79b3367c9fbe67681c048ca6a×tamp=1240457326218&secret=649820873&menuid=10081&labelstyle=dls -->
<html><head><title></title>
<meta http-equiv=content-type content="text/html; charset=gb2312"><link href=""
type=text/css rel=stylesheet>
<script type=text/javascript>
function geturlbycatalogid(catalogid) {
return "treedemo/content_add.jsp?catalogid="+catalogid;
}
</script>
<meta content="mshtml 6.00.2900.3527" name=generator></head><frameset border=0
framespacing=0 rows=* frameborder=0>
<frameset border=0 framespacing=1 frameborder=0 cols=200,*>
<frame name=stree src="show_cat_tree.jsp" src="show_tree.jsp" scrolling=yes target="list">
<frame name=list src="list_default.htm" src="list_default.htm"><noframes>
<body>
</body>
</noframes></frameset></frameset></html>
<!doctype html public "-//w3c//dtd html 4.0 frameset//en">
<!-- saved from url=(0203)http://211.142.31.211:8050/cms/content/content_frame.jsp?eafplaceholder=1&staffid=11010000&ssessionid=a5704fd79b3367c9fbe67681c048ca6a×tamp=1240457326218&secret=649820873&menuid=10081&labelstyle=dls -->
<html><head><title></title>
<meta http-equiv=content-type content="text/html; charset=gb2312"><link href=""
type=text/css rel=stylesheet>
<script type=text/javascript>
function geturlbycatalogid(catalogid) {
return "treedemo/content_add.jsp?catalogid="+catalogid;
}
</script>
<meta content="mshtml 6.00.2900.3527" name=generator></head><frameset border=0
framespacing=0 rows=* frameborder=0>
<frameset border=0 framespacing=1 frameborder=0 cols=200,*>
<frame name=stree src="show_cat_tree.jsp" src="show_tree.jsp" scrolling=yes target="list">
<frame name=list src="list_default.htm" src="list_default.htm"><noframes>
<body>
</body>
</noframes></frameset></frameset></html>
第九步:运行效果
备注:如有需要源码的请直接联系qq398349538。
复制代码 代码如下:
function node(id, pid, name, url, title, target, icon, iconopen, open, appendedstr) {
this.id = id;
this.pid = pid;
this.name = name;
this.url = url;
this.title = title;
this.target = target;
this.icon = icon;
this.iconopen = iconopen;
this.appendedstr = appendedstr;
this._io = open || false;
this._is = false;
this._ls = false;
this._hc = false;
this._ai = 0;
this._p;
};
// tree object
function tree(objname,path) {
this.path = path;
this.config = {
target : null,
folderlinks : true,
useselection : true,
usecookies : true,
uselines : true,
useicons : true,
usestatustext : false,
closesamelevel : false,
inorder : false
}
this.icon = {
root : path + 'treedemo/images/tree_base.gif',
folder : path + 'treedemo/images/tree_folder.gif',
folderopen : path + 'treedemo/images/tree_folderopen.gif',
node : path + 'treedemo/images/tree_folder.gif',
empty : path + 'treedemo/images/tree_empty.gif',
line : path + 'treedemo/images/tree_line.gif',
join : path + 'treedemo/images/tree_join.gif',
joinbottom : path + 'treedemo/images/tree_joinbottom.gif',
plus : path + 'treedemo/images/tree_plus.gif',
plusbottom : path + 'treedemo/images/tree_plusbottom.gif',
minus : path + 'treedemo/images/tree_minus.gif',
minusbottom : path + 'treedemo/images/tree_minusbottom.gif',
nlplus : path + 'treedemo/images/tree_nolines_plus.gif',
nlminus : path + 'treedemo/images/tree_nolines_minus.gif'
};
this.obj = objname;
this.anodes = [];
this.aindent = [];
this.root = new node(-1);
this.selectednode = null;
this.selectedfound = false;
this.completed = false;
};
// adds a new node to the node array
tree.prototype.add = function(id, pid, name, url, title, target, icon, iconopen, open, appendedstr) {
this.anodes[this.anodes.length] = new node(id, pid, name, url, title, target, icon, iconopen, open, appendedstr);
};
// open/close all nodes
tree.prototype.openall = function() {
this.oall(true);
};
tree.prototype.closeall = function() {
this.oall(false);
};
// outputs the tree to the page
tree.prototype.tostring = function() {
var str = '<div class="tree">\n';
if (document.getelementbyid) {
if (this.config.usecookies) this.selectednode = this.getselected();
str += this.addnode(this.root);
} else str += 'browser not supported.';
str += '</div>';
if (!this.selectedfound) this.selectednode = null;
this.completed = true;
return str;
};
// creates the tree structure
tree.prototype.addnode = function(pnode) {
var str = '';
var n=0;
if (this.config.inorder) n = pnode._ai;
for (n; n<this.anodes.length; n++) {
if (this.anodes[n].pid == pnode.id) {
var cn = this.anodes[n];
cn._p = pnode;
cn._ai = n;
this.setcs(cn);
if (!cn.target && this.config.target) cn.target = this.config.target;
if (cn._hc && !cn._io && this.config.usecookies) cn._io = this.isopen(cn.id);
if (!this.config.folderlinks && cn._hc) cn.url = null;
if (this.config.useselection && cn.id == this.selectednode && !this.selectedfound) {
cn._is = true;
this.selectednode = n;
this.selectedfound = true;
}
str += this.node(cn, n);
if (cn._ls) break;
}
}
return str;
};
// creates the node icon, url and text
tree.prototype.node = function(node, nodeid) {
var str = '<div class="treenode">' + this.indent(node, nodeid);
if (this.config.useicons) {
if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
if (!node.iconopen) node.iconopen = (node._hc) ? this.icon.folderopen : this.icon.node;
if (this.root.id == node.pid) {
node.icon = this.icon.root;
node.iconopen = this.icon.root;
}
str += '<img id="i' + this.obj + nodeid + '" src="' + ((node._io) ? node.iconopen : node.icon) + '" src="' + ((node._io) ? node.iconopen : node.icon) + '" alt="" />';
}
if (node.url) {
str += '<a id="s' + this.obj + nodeid + '" class="' + ((this.config.useselection) ? ((node._is ? 'nodesel' : 'node')) : 'node') + '" href="' + node.url + '" href="' + node.url + '"';
if (node.title) str += ' title="' + node.title + '"';
if (node.target) str += ' target="' + node.target + '"';
if (this.config.usestatustext) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
if (this.config.useselection && ((node._hc && this.config.folderlinks) || !node._hc))
str += ' onclick="javascript: ' + this.obj + '.s(' + nodeid + ');"';
str += '>';
}
else if ((!this.config.folderlinks || !node.url) && node._hc && node.pid != this.root.id)
str += '<a href="javascript: ' + this.obj + '.o(' + nodeid + ');" href="javascript: ' + this.obj + '.o(' + nodeid + ');" class="node">';
str += node.name;
if (node.url || ((!this.config.folderlinks || !node.url) && node._hc)) str += '</a>';
//[!--begin--]add by wangxr to append str
if(node.appendedstr) str += node.appendedstr;
//[!--end--]
str += '</div>';
if (node._hc) {
str += '<div id="d' + this.obj + nodeid + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
str += this.addnode(node);
str += '</div>';
}
this.aindent.pop();
return str;
};
// adds the empty and line icons
tree.prototype.indent = function(node, nodeid) {
var str = '';
if (this.root.id != node.pid) {
for (var n=0; n<this.aindent.length; n++)
str += '<img src="' + ( (this.aindent[n] == 1 && this.config.uselines) ? this.icon.line : this.icon.empty ) + '" src="' + ( (this.aindent[n] == 1 && this.config.uselines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
(node._ls) ? this.aindent.push(0) : this.aindent.push(1);
if (node._hc) {
str += '<a href="javascript: ' + this.obj + '.o(' + nodeid + ');" href="javascript: ' + this.obj + '.o(' + nodeid + ');"><img id="j' + this.obj + nodeid + '" src="';
if (!this.config.uselines) str += (node._io) ? this.icon.nlminus : this.icon.nlplus;
else str += ( (node._io) ? ((node._ls && this.config.uselines) ? this.icon.minusbottom : this.icon.minus) : ((node._ls && this.config.uselines) ? this.icon.plusbottom : this.icon.plus ) );
str += '" src="'; if (!this.config.uselines) str += (node._io) ? this.icon.nlminus : this.icon.nlplus; else str += ( (node._io) ? ((node._ls && this.config.uselines) ? this.icon.minusbottom : this.icon.minus) : ((node._ls && this.config.uselines) ? this.icon.plusbottom : this.icon.plus ) ); str += '" alt="" /></a>';
} else str += '<img src="' + ( (this.config.uselines) ? ((node._ls) ? this.icon.joinbottom : this.icon.join ) : this.icon.empty) + '" src="' + ( (this.config.uselines) ? ((node._ls) ? this.icon.joinbottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
}
return str;
};
// checks if a node has any children and if it is the last sibling
tree.prototype.setcs = function(node) {
var lastid;
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n].pid == node.id) node._hc = true;
if (this.anodes[n].pid == node.pid) lastid = this.anodes[n].id;
}
if (lastid==node.id) node._ls = true;
};
// returns the selected node
tree.prototype.getselected = function() {
var sn = this.getcookie('cs' + this.obj);
return (sn) ? sn : null;
};
// highlights the selected node
tree.prototype.s = function(id) {
if (!this.config.useselection) return;
var cn = this.anodes[id];
if (cn._hc && !this.config.folderlinks) return;
if (this.selectednode != id) {
if (this.selectednode || this.selectednode==0) {
eold = document.getelementbyid("s" + this.obj + this.selectednode);
eold.classname = "node";
}
enew = document.getelementbyid("s" + this.obj + id);
enew.classname = "nodesel";
this.selectednode = id;
if (this.config.usecookies) this.setcookie('cs' + this.obj, cn.id);
}
};
// toggle open or close
tree.prototype.o = function(id) {
var cn = this.anodes[id];
this.nodestatus(!cn._io, id, cn._ls);
cn._io = !cn._io;
if (this.config.closesamelevel) this.closelevel(cn);
if (this.config.usecookies) this.updatecookie();
};
// open or close all nodes
tree.prototype.oall = function(status) {
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n]._hc && this.anodes[n].pid != this.root.id) {
this.nodestatus(status, n, this.anodes[n]._ls)
this.anodes[n]._io = status;
}
}
if (this.config.usecookies) this.updatecookie();
};
// opens the tree to a specific node
tree.prototype.opento = function(nid, bselect, bfirst) {
if (!bfirst) {
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n].id == nid) {
nid=n;
break;
}
}
}
var cn=this.anodes[nid];
if (cn.pid==this.root.id || !cn._p) return;
cn._io = true;
cn._is = bselect;
if (this.completed && cn._hc) this.nodestatus(true, cn._ai, cn._ls);
if (this.completed && bselect) this.s(cn._ai);
else if (bselect) this._sn=cn._ai;
this.opento(cn._p._ai, false, true);
};
// closes all nodes on the same level as certain node
tree.prototype.closelevel = function(node) {
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n].pid == node.pid && this.anodes[n].id != node.id && this.anodes[n]._hc) {
this.nodestatus(false, n, this.anodes[n]._ls);
this.anodes[n]._io = false;
this.closeallchildren(this.anodes[n]);
}
}
}
// closes all children of a node
tree.prototype.closeallchildren = function(node) {
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n].pid == node.id && this.anodes[n]._hc) {
if (this.anodes[n]._io) this.nodestatus(false, n, this.anodes[n]._ls);
this.anodes[n]._io = false;
this.closeallchildren(this.anodes[n]);
}
}
}
// change the status of a node(open or closed)
tree.prototype.nodestatus = function(status, id, bottom) {
ediv = document.getelementbyid('d' + this.obj + id);
ejoin = document.getelementbyid('j' + this.obj + id);
if (this.config.useicons) {
eicon = document.getelementbyid('i' + this.obj + id);
eicon.src = (status) ? this.anodes[id].iconopen : this.anodes[id].icon;
}
ejoin.src = (this.config.uselines)?
((status)?((bottom)?this.icon.minusbottom:this.icon.minus):((bottom)?this.icon.plusbottom:this.icon.plus)):
((status)?this.icon.nlminus:this.icon.nlplus);
ediv.style.display = (status) ? 'block': 'none';
};
// [cookie] clears a cookie
tree.prototype.clearcookie = function() {
var now = new date();
var yesterday = new date(now.gettime() - 1000 * 60 * 60 * 24);
this.setcookie('co'+this.obj, 'cookievalue', yesterday);
this.setcookie('cs'+this.obj, 'cookievalue', yesterday);
};
// [cookie] sets value in a cookie
tree.prototype.setcookie = function(cookiename, cookievalue, expires, path, domain, secure) {
document.cookie =
escape(cookiename) + '=' + escape(cookievalue)
+ (expires ? '; expires=' + expires.togmtstring() : '')
+ (path ? '; path=' + path : '')
+ (domain ? '; domain=' + domain : '')
+ (secure ? '; secure' : '');
};
// [cookie] gets a value from a cookie
tree.prototype.getcookie = function(cookiename) {
var cookievalue = '';
var posname = document.cookie.indexof(escape(cookiename) + '=');
if (posname != -1) {
var posvalue = posname + (escape(cookiename) + '=').length;
var endpos = document.cookie.indexof(';', posvalue);
if (endpos != -1) cookievalue = unescape(document.cookie.substring(posvalue, endpos));
else cookievalue = unescape(document.cookie.substring(posvalue));
}
return (cookievalue);
};
// [cookie] returns ids of open nodes as a string
tree.prototype.updatecookie = function() {
var str = '';
for (var n=0; n<this.anodes.length; n++) {
if (this.anodes[n]._io && this.anodes[n].pid != this.root.id) {
if (str) str += '.';
str += this.anodes[n].id;
}
}
this.setcookie('co' + this.obj, str);
};
// [cookie] checks if a node id is in a cookie
tree.prototype.isopen = function(id) {
var aopen = this.getcookie('co' + this.obj).split('.');
for (var n=0; n<aopen.length; n++)
if (aopen[n] == id) return true;
return false;
};
// if push and pop is not implemented by the browser
if (!array.prototype.push) {
array.prototype.push = function array_push() {
for(var i=0;i<arguments.length;i++)
this[this.length]=arguments[i];
return this.length;
}
};
if (!array.prototype.pop) {
array.prototype.pop = function array_pop() {
lastelement = this[this.length-1];
this.length = math.max(this.length-1,0);
return lastelement;
}
};
由于代码太长,我这里就不给大家拿来细讲了,我们只要会用就ok。就像猪肉我们能吃就ok,不一定非要知道猪养。
第二步:创建数据库,创建数据库的代码如下,我这边使用的是mysql数据为。
create database `treedemo`;
use treedemo;
create table trees(
tid int primary key not null,
pid int not null,
tname varchar(50) not null,
isleaf int
);
select * from trees;
insert into trees(tid,pid,tname)values(0,-1,'组织内容');
insert into trees(tid,pid,tname)values(1,0,'短信');
insert into trees(tid,pid,tname)values(2,0,'彩信');
insert into trees(tid,pid,tname)values(3,0,'新闻');
insert into trees(tid,pid,tname)values(4,1,'移动生活');
insert into trees(tid,pid,tname)values(5,1,'单条滚动点播');
insert into trees(tid,pid,tname)values(6,2,'定制');
insert into trees(tid,pid,tname)values(7,2,'点播');
insert into trees(tid,pid,tname)values(8,3,'房产频道');
insert into trees(tid,pid,tname)values(9,3,'农村频道');
insert into trees(tid,pid,tname)values(10,3,'数码频道');
insert into trees(tid,pid,tname)values(11,6,'幽默笑话');
insert into trees(tid,pid,tname)values(12,7,'铃声');
insert into trees(tid,pid,tname)values(13,7,'贺卡');
insert into trees(tid,pid,tname)values(14,7,'动画');
insert into trees(tid,pid,tname)values(15,13,'贺卡1');
insert into trees(tid,pid,tname)values(16,13,'贺卡2');
insert into trees(tid,pid,tname)values(17,13,'贺卡3');
insert into trees(tid,pid,tname)values(18,13,'贺卡4');
select * from trees;
表字段的说明:
(1)tid表示节点的编号;
(2)pid 该节点父节点的编号;
(3)tname 节点名称;
(4)isleaf 表明该节点是否为叶节点,叶节点为1,非叶节点为0。该字段可根据实际情况增删。
第三步:在myeclipse中创建一个web工程,命名为“treedemo”。在webroot文件夹下创建js文件夹、css文件夹、images文件夹分别用来存放.js文件、.css文件和项目中用到的图片文件。
第四步:编写数据库连接类,其中的用户名、密码需要根据你数据库情况进行修改,代码如下。
复制代码 代码如下:
package com.sx.mas.utils;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;
public class dbconn {
private static final string driver="com.mysql.jdbc.driver";
private static final string url="jdbc:mysql://localhost:3306/treedemo";
connection conn=null;
public connection getconnection(){
try {
class.forname(driver);
conn=drivermanager.getconnection(url,"root","root");
} catch (classnotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return conn;
}
}
package com.sx.mas.utils;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;
public class dbconn {
private static final string driver="com.mysql.jdbc.driver";
private static final string url="jdbc:mysql://localhost:3306/treedemo";
connection conn=null;
public connection getconnection(){
try {
class.forname(driver);
conn=drivermanager.getconnection(url,"root","root");
} catch (classnotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return conn;
}
}
第五步:编写javabean类,代码如下,
复制代码 代码如下:
package com.sx.mas.beans;
public class treenode {
private int tid;
private int pid;
private string tname;
private int isleaf;
public int gettid() {
return tid;
}
public void settid(int tid) {
this.tid = tid;
}
public int getpid() {
return pid;
}
public void setpid(int pid) {
this.pid = pid;
}
public string gettname() {
return tname;
}
public void settname(string tname) {
this.tname = tname;
}
public int getisleaf() {
return isleaf;
}
public void setisleaf(int isleaf) {
this.isleaf = isleaf;
}
}
package com.sx.mas.beans;
public class treenode {
private int tid;
private int pid;
private string tname;
private int isleaf;
public int gettid() {
return tid;
}
public void settid(int tid) {
this.tid = tid;
}
public int getpid() {
return pid;
}
public void setpid(int pid) {
this.pid = pid;
}
public string gettname() {
return tname;
}
public void settname(string tname) {
this.tname = tname;
}
public int getisleaf() {
return isleaf;
}
public void setisleaf(int isleaf) {
this.isleaf = isleaf;
}
}
第六步:创建相关的dao类,代码如下。dao类将数据库表装保存到vector对象中。
复制代码 代码如下:
package com.sx.mas.beans;
import java.sql.connection;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.vector;
import com.sx.mas.utils.dbconn;
public class treedao {
public vector gettree(){
vector vec = new vector();
dbconn dbconn = new dbconn();
connection conn = dbconn.getconnection();
try {
statement stmt = conn.createstatement();
resultset rs = stmt.executequery("select * from trees");
while(rs.next()){
treenode treenode = new treenode();
treenode.settid(rs.getint("tid"));
treenode.setpid(rs.getint("pid"));
treenode.settname(rs.getstring("tname"));
treenode.setisleaf(rs.getint("isleaf"));
vec.add(treenode);
}
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return vec;
}
}
package com.sx.mas.beans;
import java.sql.connection;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.vector;
import com.sx.mas.utils.dbconn;
public class treedao {
public vector gettree(){
vector vec = new vector();
dbconn dbconn = new dbconn();
connection conn = dbconn.getconnection();
try {
statement stmt = conn.createstatement();
resultset rs = stmt.executequery("select * from trees");
while(rs.next()){
treenode treenode = new treenode();
treenode.settid(rs.getint("tid"));
treenode.setpid(rs.getint("pid"));
treenode.settname(rs.getstring("tname"));
treenode.setisleaf(rs.getint("isleaf"));
vec.add(treenode);
}
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return vec;
}
}
第七步:页面上显示树状结构。在treedemo中创建show_tree.jsp页面,代码如下。
复制代码 代码如下:
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=gb2312"%>
<%@ page import="com.sx.mas.beans.*" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<link href="css/tree.css" href="css/tree.css" type=text/css rel=stylesheet>
<link href="css/css.css" href="css/css.css" rel=stylesheet>
<script src="js/tree.js" src="js/tree.js" type=text/javascript></script>
<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="styles.css" href="styles.css">
-->
</head>
<body onresize="return true;" leftmargin=1 topmargin=1>
<table>
<tr>
<td valign="top">
<table class=table_left_menu cellspacing=0 cellpadding=0 width="100%"
background=images/tree_bg.gif border=0>
<tbody>
<tr>
<td>
<div align=center><img height=24 src="images/tree_button.gif" src="images/tree_button.gif"
width=147 usemap=#map border=0>
<map id=map name=map>
<area shape="rect" shape="rect" coords="16,3,69,15" coords="16,3,69,15" href="javascript:%20d.openall()" href="javascript:%20d.openall()">
<area shape="rect" shape="rect" coords="72,3,131,15" coords="72,3,131,15" href="javascript:%20d.closeall()" href="javascript:%20d.closeall()">
</map>
</div>
</td>
</tr>
</tbody>
</table>
<script type=text/javascript>
d = new tree('d','../');
<%
treedao treedao = new treedao();
vector vec =treedao.gettree();
iterator iterator = vec.iterator();
while(iterator.hasnext()){
treenode treenode = (treenode)iterator.next();
if(treenode.getisleaf()==0){
%>
d.add(<%=treenode.gettid()%>,<%=treenode.getpid()%>,'<%=treenode.gettname()%>')
<%}else{%>
d.add(<%=treenode.gettid()%>,<%=treenode.getpid()%>,'<%=treenode.gettname()%>',parent.geturlbycatalogid('<%=treenode.gettid()%>'),null,'list');
<%}}%>
document.write(d);
</script>
</td>
</tr>
</table>
</body>
</html>
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=gb2312"%>
<%@ page import="com.sx.mas.beans.*" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<link href="css/tree.css" href="css/tree.css" type=text/css rel=stylesheet>
<link href="css/css.css" href="css/css.css" rel=stylesheet>
<script src="js/tree.js" src="js/tree.js" type=text/javascript></script>
<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="styles.css" href="styles.css">
-->
</head>
<body onresize="return true;" leftmargin=1 topmargin=1>
<table>
<tr>
<td valign="top">
<table class=table_left_menu cellspacing=0 cellpadding=0 width="100%"
background=images/tree_bg.gif border=0>
<tbody>
<tr>
<td>
<div align=center><img height=24 src="images/tree_button.gif" src="images/tree_button.gif"
width=147 usemap=#map border=0>
<map id=map name=map>
<area shape="rect" shape="rect" coords="16,3,69,15" coords="16,3,69,15" href="javascript:%20d.openall()" href="javascript:%20d.openall()">
<area shape="rect" shape="rect" coords="72,3,131,15" coords="72,3,131,15" href="javascript:%20d.closeall()" href="javascript:%20d.closeall()">
</map>
</div>
</td>
</tr>
</tbody>
</table>
<script type=text/javascript>
d = new tree('d','../');
<%
treedao treedao = new treedao();
vector vec =treedao.gettree();
iterator iterator = vec.iterator();
while(iterator.hasnext()){
treenode treenode = (treenode)iterator.next();
if(treenode.getisleaf()==0){
%>
d.add(<%=treenode.gettid()%>,<%=treenode.getpid()%>,'<%=treenode.gettname()%>')
<%}else{%>
d.add(<%=treenode.gettid()%>,<%=treenode.getpid()%>,'<%=treenode.gettname()%>',parent.geturlbycatalogid('<%=treenode.gettid()%>'),null,'list');
<%}}%>
document.write(d);
</script>
</td>
</tr>
</table>
</body>
</html>
第八步:创建一个框架页面,index.html代码如下,和list_default.htm。
复制代码 代码如下:
<!doctype html public "-//w3c//dtd html 4.0 frameset//en">
<!-- saved from url=(0203)http://211.142.31.211:8050/cms/content/content_frame.jsp?eafplaceholder=1&staffid=11010000&ssessionid=a5704fd79b3367c9fbe67681c048ca6a×tamp=1240457326218&secret=649820873&menuid=10081&labelstyle=dls -->
<html><head><title></title>
<meta http-equiv=content-type content="text/html; charset=gb2312"><link href=""
type=text/css rel=stylesheet>
<script type=text/javascript>
function geturlbycatalogid(catalogid) {
return "treedemo/content_add.jsp?catalogid="+catalogid;
}
</script>
<meta content="mshtml 6.00.2900.3527" name=generator></head><frameset border=0
framespacing=0 rows=* frameborder=0>
<frameset border=0 framespacing=1 frameborder=0 cols=200,*>
<frame name=stree src="show_cat_tree.jsp" src="show_tree.jsp" scrolling=yes target="list">
<frame name=list src="list_default.htm" src="list_default.htm"><noframes>
<body>
</body>
</noframes></frameset></frameset></html>
<!doctype html public "-//w3c//dtd html 4.0 frameset//en">
<!-- saved from url=(0203)http://211.142.31.211:8050/cms/content/content_frame.jsp?eafplaceholder=1&staffid=11010000&ssessionid=a5704fd79b3367c9fbe67681c048ca6a×tamp=1240457326218&secret=649820873&menuid=10081&labelstyle=dls -->
<html><head><title></title>
<meta http-equiv=content-type content="text/html; charset=gb2312"><link href=""
type=text/css rel=stylesheet>
<script type=text/javascript>
function geturlbycatalogid(catalogid) {
return "treedemo/content_add.jsp?catalogid="+catalogid;
}
</script>
<meta content="mshtml 6.00.2900.3527" name=generator></head><frameset border=0
framespacing=0 rows=* frameborder=0>
<frameset border=0 framespacing=1 frameborder=0 cols=200,*>
<frame name=stree src="show_cat_tree.jsp" src="show_tree.jsp" scrolling=yes target="list">
<frame name=list src="list_default.htm" src="list_default.htm"><noframes>
<body>
</body>
</noframes></frameset></frameset></html>
第九步:运行效果
备注:如有需要源码的请直接联系qq398349538。