我贴出来吧
<%@ page contentType="text/html;
charset=gb2312"%>
<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id = "qry" scope = "request" class = "dbConn.ViewQueryBean"/>
<HTML>
<HEAD>
<script language="javascript" src=createtree.js></script>
</HEAD>
<H1>无限级树</H1>
<%
/*
create table NODESTABLE
(
A1 VARCHAR2(20),
ID VARCHAR2(10),
PID VARCHAR2(10),
NAME VARCHAR2(50)
)
最顶层的结点PID为0
*/
String sControl="";
String sOutput="";
StringBuffer sBuff = new StringBuffer();
qry.openConnection();
ResultSet rs=qry.openQuery("select * from nodestable ");
sBuff.append("<script LANGUAGE=/"JavaScript/">/n");
//下面加树的控件
sBuff.append(" var strStyle=/'"+"<style type=/"text/css/">"+"/'/n");
sBuff.append(" strStyle+=/'"+"span.SelectIng{color:#0000ff;border:solid 1pt #93BEE2;padding:0.5pt;height:4px;background-color:#93BEE2;}"+"/'/n");
sBuff.append(" strStyle+=/'"+"span.NoSelect{color:#000000;border:solid 1pt #93BEE2;padding:0.5pt;height:4px;background-color:#93BEE2;}"+"/'/n");
sBuff.append(" strStyle+=/'"+"span.SelectEd{color:#0000FF;border:solid 1pt #000000;padding:0.5pt;height:4px;background-color:#ffffff;}"+"/'/n");
sBuff.append(" strStyle+=/'"+"</style>"+"/'/n");
sBuff.append("do
cument.write(strStyle) /n /n");
sBuff.append(" var myRs=new Array(), selectItem;
/n");
sBuff.append(" function compare(a,b) {return parseInt(a[1]) - parseInt(b[1])} /n");
sBuff.append(" function InsertItem(s){ /n");
sBuff.append(" myRs[myRs.length++] = s.split(/,/);} /n /n");
sBuff.append(" function ExCloAll
{ /n");
sBuff.append(" var d=document.all(/'"+"0/'), e=d.all.tags(/'"+"div/') /n");
sBuff.append(" for (i=0;i<e.length;i++)e
.style.display=(n==1?/'"+"block/'"+":/'"+"none/') /n");
sBuff.append(" e=d.all.tags(/'"+"p/'"+") /n");
sBuff.append(" for (i=0;i<e.length;i++){if(ChkExist(e.children[1].value))e.children[0].innerText=(n==1?/'"+"- /'"+":/'"+"+ /')}} /n /n");
sBuff.append(" function ExCloItem(){ /n");
sBuff.append(" var c,e=event.srcElement, p=e.parentElement.children /n");
sBuff.append(" if(selectItem!=null)selectItem.className=/'"+"NoSelect/'"+";
/n");
sBuff.append(" e.className=/'"+"SelectEd/'"+" /n");
sBuff.append(" selectItem=event.srcElement;
/n");
sBuff.append(" if(c=document.all(e.value)){ /n");
sBuff.append(" pop[0].innerText=(p[0].innerText==/'"+"+ /'"+"?/'"+"- /'"+":/'"+"+ /'"+") /n");
sBuff.append(" }else
{p[0].innerText=/'"+"- /'"+"}} /n /n");
sBuff.append(" function ExCloItem2(){ /n");
sBuff.append(" var c,e=event.srcElement, p=e.parentElement.children /n");
sBuff.append(" if(c=document.all(p[1].value)){ /n");
sBuff.append(" e.innerText=(e.innerText==/'"+"+ /'"+"?/'"+"- /'"+":/'"+"+ /') /n");
sBuff.append(" c.style.display=(c.style.display==/'"+"block/'"+"?/'"+"none/'"+":/'"+"block/'"+") /n");
sBuff.append(" }else
e.innerText=/'"+"- /'} /n /n");
sBuff.append(" function OutItem(){ /n");
sBuff.append(" var e=event.srcElement /n");
sBuff.append(" if(selectItem != /'"+"/'"+")e.className=(selectItem==e?/'"+"SelectEd/'"+":/'"+"NoSelect/'"+") /n");
sBuff.append(" else
e.className=/'"+"NoSelect/'"+"} /n /n");
sBuff.append(" function ChkExist{ /n");
sBuff.append(" n=parseInt /n");
sBuff.append(" if((n<myRs[0][1])||(n>myRs[myRs.length-1][1]))return false /n");
sBuff.append(" var a,b,c,x=0, y=parseInt(myRs.length/2), z=myRs.length-1 /n");
sBuff.append(" while((x!=y)&&(y!=z)){ /n");
sBuff.append(" a=myRs[x][1], b=myRs[y][1], c=myRs[z][1] /n");
sBuff.append(" if(n==a||n==b||n==c)return true /n");
sBuff.append(" if(n>b)x=y;
/n");
sBuff.append(" else
z=y;y=parseInt(x+z);
/n");
sBuff.append(" y=parseInt((x+z)/2);
/n");
sBuff.append(" }return false} /n /n");
sBuff.append(" function drawTree{ /n");
sBuff.append(" if(n==0){ /n");
sBuff.append(" myRs.sort(compare) /n");
sBuff.append(" do
cument.write(/'"+"<p style=/"font:10pt;cursor:hand;/"><span onclick=/"ExCloAll(1)/">全部展开</span>&nbsp;&nbsp;<span onclick=/"ExCloAll(0)/">全部合上</span></p>/'"+")} /n /n");
sBuff.append(" if (ChkExist!=true) return /n");
sBuff.append(" do
cument.write(/'"+"<div id=/"/'"+"+n+/'"+"/" style=/"font:10pt;cursor:hand;/'"+"+(n!=0?/'"+"position:relative;left:20;display:none;/'"+":/'"+"/'"+")+/'"+"/">/'"+") /n");
sBuff.append(" for(var i=0;i<myRs.length;i++){ /n");
sBuff.append(" if (parseInt(myRs[1])>n)break;
/n");
sBuff.append(" if (myRs[1]==n){ /n");
sBuff.append(" do
cument.write(/'"+"<p style=/"margin:0pt;/"><span style=/"vertical-align:top;/" onclick=/"ExCloItem2()/">/'"+"+(ChkExist(myRs[0])?/'"+"+ /'"+":/'"+"- /'"+")+/'"+"</span>/'"+") /n");
sBuff.append(" do
cument.write(/'"+"<span onclick=/"ExCloItem()/" value=/"/'"+"+myRs[0]+/'"+"/" class=/"NoSelect/" onmouseout=/"OutItem()/" onmouseover=/"this.className="+"//'SelectIng"+"//'/">/'"+"+myRs[2]+/'"+"</span></p>/'"+") /n");
sBuff.append(" drawTree(myRs[0])}} /n");
sBuff.append(" do
cument.write(/'"+"</div>/'"+")} /n /n");
//加上数据库里的结点
while (rs.next()){
sOutput=rs.getString("id")+","+rs.getString("pid")+","+rs.getString("name");
sBuff.append(" InsertItem(/'"+sOutput+"/') /n");
}
sBuff.append(" drawTree(0) /n /n");
sBuff.append("</script>");
sControl = sBuff.toString();
out.println(sControl);
try{
qry.close_Resource(qry.getThisConn(),qry.getThisStatement());
}catch(Exception e){
out.println(e);
}
%>
<body >
</BODY>
</HTML>