有限分级排序和分级排序(递归)

JAVA递归实现列栏目目录树

(2011-04-03 01:28:39)
标签:

it

分类: java编程

仅仅是实现了列目录树的功能,理论上可实现无限分级,需要完善的地方还很多......

栏目数据表(名:category)结构:

id(自动排序),name(栏目名称),parent_id(父ID)

java源文件(listCate.java):

package list;
import java.sql.*;

public class listCate
{
public static String listCates(int tempId,String tempStr)
{
     String sql="";
     String user="root";
     String pswd="mysql";
     String url="jdbc:mysql://localhost:3306/MyDataBase?useUnicode=true&characterEncoding=gbk";
     Connection conn;
     Statement stmt1,stmt2;
     ResultSet rs1,rs2;
     try
     {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      conn = DriverManager.getConnection(url,user,pswd);
      sql = "select * from category where parent_id = " + tempId + " order by orders";
      stmt1 = conn.createStatement();
      rs1=stmt1.executeQuery(sql);
      while(rs1.next())
      {
       tempId = rs1.getInt("id");
       tempStr += rs1.getString("name")+"<br>";
//       sql = "select * from category where parent_id = " + tempId + " order by orders";
//       stmt2 = conn.createStatement();
//       rs2 = stmt2.executeQuery(sql);
//       if(rs2.next())
//       {
//        tempStr += " ┣";
        tempStr = listCates(tempId,tempStr);
//       }
//       rs2.close();
//       stmt2.close();
      }
      rs1.close();
      stmt1.close();
     }
     catch (Exception ex)
     {
      System.out.println("Error: " + ex.toString());
     }
//     System.out.println(tempStr); //输出到Tomcat
     return tempStr;
}
}

编译上面的文件成类,jsp文件调用如下:
<%@page language="java" contentType="text/html;charset=gbk" import="java.util.*,java.io.*,list.listCate"%>
<title>栏目分类详细列表</title>
<%
out.println(listCate.listCates(0,""));     //输出到浏览器
%>

 

 

 

 

 

 

 

 

========================================================

有限分级

 

 

public class NodeList extends ActionInstance{


 @Override
 public int execute(ActionForm af) throws Exception {
  HttpServletRequest  request  =af.getHttpRequest();
  Connection conn = af.getConnection();
  
        List list = NodeListEncapsulation.getList(conn);     
  request.setAttribute("list", list);
  
  return 1;

  
      }
}

 

 

 

 

public class NodeListEncapsulation {

 public static List getList(Connection conn){
  
   //将大类小类封装成list->map->list_child->map_child
       StringBuffer sql = new StringBuffer();
       sql.append("select id,name,parent_id,grade from node order by grade");
    List<DataForm> list_all= DaoUtil.select(conn, sql.toString(), null);
       List<Map> list= new ArrayList<Map>();
//       List<Map> list_child = new ArrayList<Map>();
//       List<Map> list_grandchild = new ArrayList<Map>();
//       List<Map> list_grandchild_child = new ArrayList<Map>();   
      
       //list = xxx.getXXX(conn);  request.setA...
   
    for (DataForm df : list_all){
     Map map =new HashMap();
     int grade =(Integer)df.get("GRADE");
        if(grade==1){
          map.put("id", df.get("ID"));
          map.put("name", df.get("NAME"));
          list.add(map); 
        }//1,2->map
    }   
   
    for(Map map : list){

     int id =(Integer) map.get("id");
     List<Map> list_child = new ArrayList<Map>();
     for(DataForm df :list_all){
       if((Integer)df.get("PARENT_ID")==id){
          Map map_child = new HashMap();
          map_child.put("id", df.get("ID"));
          map_child.put("name",df.get("NAME"));
          map_child.put("parent_id",df.get("PARENT_ID"));         
          list_child.add(map_child);
          map.put("list_child", list_child);
       }
     }
     for(Map map_child : list_child){
      int id1= (Integer) map_child.get("id");
      List<Map> list_grandchild = new ArrayList<Map>();
      for(DataForm df :list_all){
       if((Integer)df.get("PARENT_ID")==id1){
        Map map_grandchild  = new HashMap();
        map_grandchild.put("id", df.get("ID"));
        map_grandchild.put("name",df.get("NAME"));
        map_grandchild.put("parent_id",df.get("PARENT_ID"));      
        list_grandchild.add(map_grandchild);
        map_child.put("list_grandchild", list_grandchild);
       }
      }
     for(Map map_grandchild : list_grandchild){
       int id2= (Integer) map_grandchild.get("id");
       List<Map> list_grandchild_child = new ArrayList<Map>();
       for(DataForm df :list_all){
        if((Integer)df.get("PARENT_ID")==id2){
         Map map_grandchild_child  = new HashMap();
         map_grandchild_child.put("id", df.get("ID"));
         map_grandchild_child.put("name",df.get("NAME"));
         map_grandchild_child.put("parent_id",df.get("PARENT_ID"));
         list_grandchild_child.add(map_grandchild_child);
         map_grandchild.put("list_grandchild_child", list_grandchild_child);
        }
       }
      }
     }

   }
 
  return list;
 }
}

 

 

 

jsp页面

 

<div>
      <c:forEach items="${list}" var="bigtype">
        <h2>${bigtype.name}</h2>
          <ul style="display:block;">  
            <c:forEach items="${bigtype.list_child}" var="childtype">
              <li>${childtype.name}</li>
                <c:forEach items="${childtype.list_grandchild}" var="grandchildtype">
                 <ul><li>${grandchildtype.name}</li>
                   <c:forEach items="${grandchildtype.list_grandchild_child}" var="grandchild_childtype">
                     <ul><li>${grandchild_childtype.name}</li></ul>
                   </c:forEach>
                 </ul>
                </c:forEach>
            </c:forEach>
         </ul>
      </c:forEach>
   </div>  

 

 

==================================================再来个无限级

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值