这样吧,可否不使用递归也可以实现以上目录树的结构!!!我的代码是
<%!
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>
但是在没有子目录情况下就会出错!!如何更改?或者有更好的解决办法

解决方案 »

  1.   


    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 数据,效率可能要慢一些,其实还有更好的办法,你自己想了:)
      

  2.   


    刚才上面的写错了,现改写一下/** 此方法的作用是显示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了今天太忙了,没时间写了,你自己改一下吧。
    祝你好运!
      

  3.   

    感谢你给予大家支持!!!!问题解决了  happyegg(想转型的程序员)给我信息所说:这个问题根本不值这个分数,不需要拿这么多分数,希望给大家云云!  回想起来可真的让人很遗憾:这么简单的问题(最终他解决方法很简单明了,关键我是新接触JAVA,不是很懂)却出现如此局面:看到分数多而且用递归,造成无人解决,我只好到别的讨论模块找人解决;让人很不解。是什么原因造成的?不懂!................  550分他当之无愧!不是这个问题不值。而是一个人的挑战精神:不去用指定的框框解决问题:我要求使用递归,而你却用其他方法解决!让人真的很高兴。其余的人就算有意见我也坚持:550分照给:你值!!!!!!!!!  希望大家从这个问题当中学到什么..................................
      同时,请真正认识kw123的作风:说到做到;只要你行!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!