这样吧,可否不使用递归也可以实现以上目录树的结构!!!我的代码是
<%!
String sId;
String sName;
String sTemp;
ResultSet rset=null;
int iCount=0;
int iLen = 0;
int iLength = 3;
int iLens=3;
%>
<div>
<span><input type=checkbox>企业</span>
<UL id=root>
<%
rset=conn.executequery("select * from tree order by id");
//主目录信息
while (rset.next())
{
sId = rset.getString("id");
iLen = sId.length();
sName=rset.getString("name");
ResultSet rs = conn.executequery("select count(*) as icount from tree where id like: '"+sId+"%'");
if(rs.next()) iCount = rs.getInt("icount");//得到是否有下级目录
if(iCount==1)
{
%>
<LI><span><input type=checkbox value=<%=sId%>> <%=sName%></span>
<%
}
if((iCount>1)&&((iLength-iLen==0)||(iLen % iLength==0)))
{
%>
<LI><span><input type=checkbox value=<%=sId%>> <%=sName%></span>
<UL>
<%
}
if(iLen-iLens<0)
{
%>
</UL><%
} iLens = iLen;
}
%>
</UL>
</div>
但是在没有子目录情况下就会出错!!如何更改?或者有更好的解决办法
<%!
String sId;
String sName;
String sTemp;
ResultSet rset=null;
int iCount=0;
int iLen = 0;
int iLength = 3;
int iLens=3;
%>
<div>
<span><input type=checkbox>企业</span>
<UL id=root>
<%
rset=conn.executequery("select * from tree order by id");
//主目录信息
while (rset.next())
{
sId = rset.getString("id");
iLen = sId.length();
sName=rset.getString("name");
ResultSet rs = conn.executequery("select count(*) as icount from tree where id like: '"+sId+"%'");
if(rs.next()) iCount = rs.getInt("icount");//得到是否有下级目录
if(iCount==1)
{
%>
<LI><span><input type=checkbox value=<%=sId%>> <%=sName%></span>
<%
}
if((iCount>1)&&((iLength-iLen==0)||(iLen % iLength==0)))
{
%>
<LI><span><input type=checkbox value=<%=sId%>> <%=sName%></span>
<UL>
<%
}
if(iLen-iLens<0)
{
%>
</UL><%
} iLens = iLen;
}
%>
</UL>
</div>
但是在没有子目录情况下就会出错!!如何更改?或者有更好的解决办法
private void showTree(String strId){
int intLen = strId.length();
ResultSet rs = conn.executequery("select id, name from tree where left(id,intLen)='strId'" );
String tempId, tempName;
// put code here to output <UL>
while (rs.next()){
tempId = rs.getString("id");
tempName = rs.getString("name");
if ( tempId.length() > intLen){
showTree(tempId);
}else{
// put code here to output
// <LI><span><input type=checkbox value=<%=tempId%>> <%=tempValue%></span>
}// end if }// end while // put code here to output </UL>}// end method showTree这是一个递归方法,应该可以的。不过每次都需要从数据库中Select 数据,效率可能要慢一些,其实还有更好的办法,你自己想了:)
刚才上面的写错了,现改写一下/** 此方法的作用是显示strId目录及下面的所有目录,
调用方法,首先你把一级目录的id(长度为3的)取出来,作一个循环
把每一个一级目录传给此方法就行了。
*/
private void showTree(String strId){
int intLen = strId.length(); // 取得当前目录ID的长度
// 下面一句主要是取得属于当前目录的下级目录(仅限于下一级目录)
ResultSet rs = conn.executequery("select id, name from tree where (left(id,intLen)='strId' and id len(id)=intLen+3)order by id" );
// 如果没有下级目录,返回: 判断返回的记录数为0,好像是这样判断的,好久好久没写了,忘记了。
if ( rs.isBeforeFirst() && rs.isAfterLast() ) {
// 什么也不用作,返回就是了 }else{
// 如果有子目录,输出
// 临时变量,用于保存ID和Name String tempId, tempName;
// 开始一个新的UL,输出字符串“<UL>”
// put code here to output <UL>
// JSP中应该是什么语句,你应该很清楚吧:)
while (rs.next()){
tempId = rs.getString("id"); // 取得部门ID
tempName = rs.getString("name");// 取得部门名称
// 输入此目录
// put code here to output
// <LI><span><input type=checkbox value=<%=tempId%>> <%=tempValue%></span>
// 递归,判断此目录下面是否还有子目录
showTree(tempId); }// end while
// 结束刚才的UL,输出字符串“</UL>”
// put code here to output </UL> }// end if
}// end method showTree// 调用,取得一级目录,也就是长度为3的
ResultSet rs = conn.executequery("select id, name from tree where len(id)=3 order by id" );
然后针对每个一级目录,再调用showTree(String id)就OK了今天太忙了,没时间写了,你自己改一下吧。
祝你好运!
同时,请真正认识kw123的作风:说到做到;只要你行!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!