tree implements by hibernate
public String getNewsCateTreeHtml(){
       final Session session = super.getSession();
       final Transaction transaction = session.beginTransaction();
      
       String hql = "from NewsCat cat where cat.rootId = 0";
       StringBuffer output = new StringBuffer();
       NewsCat newsCat = new NewsCat();
       List catList = new ArrayList();
       try {
           Query query = session.createQuery(hql);  
           catList = query.list();
       } catch (HibernateException e) {
           e.printStackTrace();
       }
       transaction.commit();
       session.close();
       for(int i=0;i<catList.size();i++){
           newsCat = (NewsCat)catList.get(i);
           output.append("<tr><td>");
           output.append("No."+newsCat.getCatId());
           output.append(newsCat.getCatName());
           output.append("</td></tr>");
           output.append("\t\n");
           if(0 != newsCat.getIsLeaf()){
              this.tree(1, newsCat.getCatId(), output);
           }
       }
       return output.toString();
          
    }
   
    privatevoid tree(int level,int id,StringBuffer output){
       final Session session = super.getSession();
       final Transaction transaction = session.beginTransaction();
       String preStr = "";
       List subCatList = new ArrayList();
       NewsCat subNewsCat = new NewsCat();
       for(int i=0; i<level; i++) {
           preStr += "----";
       }
       String hql = "from NewsCat cat where cat.parentId = " + id;
       Query query = session.createQuery(hql);
       subCatList = query.list();
       transaction.commit();
       session.close();
       for(int i=0;i<subCatList.size();i++){
           subNewsCat = (NewsCat)subCatList.get(i);
           output.append("<tr><td>");
           output.append(subNewsCat.getCatId());
           output.append(preStr);
           output.append(subNewsCat.getCatName());
           output.append("</td></tr>");
           output.append("\t\n");
           if(0 != subNewsCat.getIsLeaf()){
              this.tree(level+1, subNewsCat.getCatId(), output);
           }
       }
    }
lunzi   2007-09-05 14:39:50 评论:0   阅读:149   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2008 powered by YuLog