bootstrap-treeview 后台拼装JSON

来源:互联网 发布:ant执行linux命令 编辑:程序博客网 时间:2024/05/16 06:26
  1. /* 初始化数据  
  2.  * @param specTableList  
  3.  * @return  
  4.  */  
  5. @SuppressWarnings({ "unchecked""rawtypes" })  
  6. public List initList(List  specTableList){  
  7.     List dataList = new ArrayList();    
  8.     HashMap dataRecord = new HashMap();    
  9.     for(int i = 0; i < specTableList.size(); i++) {  
  10.         VO VO = (VO)specTableList.get(i);  
  11.         dataRecord = new HashMap();    
  12.         dataRecord.put("id",VO.getSpecId());    
  13.         dataRecord.put("text", VO.getSpecName());  
  14.         String parentID = VO.getParentId();  
  15.         if (StringUtils.isEmpty(parentID)) {  
  16.             parentID = "0";  
  17.         }  
  18.         dataRecord.put("parentId",parentID);   
  19.         dataList.add(dataRecord);  
  20.     }  
  21.     return dataList;  
  22. }  
  23.   
  24.   
  25. class  Node {  
  26.     public String id;  
  27.     public String text;  
  28.     public String parentId;  
  29.       
  30.     /**  
  31.       * 孩子节点列表  
  32.       */    
  33.      private Children children = new Children();    
  34.          
  35.      // 先序遍历,拼接JSON字符串    
  36.      public String toString() {      
  37.       String result = //"["  
  38.       "{"   
  39.       + "\"text\":\"" + text + "\","  
  40.       + "\"href\":\"" + id +"\"";    
  41.       if (children != null && children.getSize() != 0) {  
  42.         if (result.contains("nodes")) {  
  43.             result += ",";  
  44.         }else{  
  45.             result += ",\"nodes\":" + children.toString();    
  46.         }  
  47.       }    
  48.       return result + "}";    
  49.      }    
  50.          
  51.      // 兄弟节点横向排序    
  52.      public void sortChildren() {    
  53.       if (children != null && children.getSize() != 0) {    
  54.        children.sortChildren();    
  55.       }    
  56.      }    
  57.          
  58.      // 添加孩子节点    
  59.      public void addChild(Node node) {    
  60.       this.children.addChild(node);    
  61.      }    
  62. }  
  63.   
  64.  class  Children {  
  65.     private List list = new ArrayList();  
  66.       
  67.     public int getSize(){  
  68.         return list.size();  
  69.     }  
  70.     public void addChild(Node node){  
  71.         list.add(node);  
  72.     }  
  73.       
  74.      // 拼接孩子节点的JSON字符串    
  75.      public String toString() {    
  76.       String result = "[";      
  77.       for (Iterator it = list.iterator(); it.hasNext();) {    
  78.        result += ((Node) it.next()).toString();    
  79.        result += ",";    
  80.       }    
  81.       result = result.substring(0, result.length() - 1);    
  82.       result += "]";    
  83.       return result;    
  84.      }    
  85.          
  86.      // 孩子节点排序    
  87.      public void sortChildren() {    
  88.       // 对本层节点进行排序    
  89.       // 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器    
  90.       Collections.sort(list, new NodeIDComparator());    
  91.       // 对每个节点的下一层节点进行排序    
  92.       for (Iterator it = list.iterator(); it.hasNext();) {    
  93.        ((Node) it.next()).sortChildren();    
  94.       }    
  95.      }    
  96.          
  97.     /**  
  98.      * 节点比较器  
  99.      */    
  100.     class NodeIDComparator implements Comparator {    
  101.      // 按照节点编号比较    
  102.      public int compare(Object o1, Object o2) {    
  103.       int j1 = Integer.parseInt(((Node)o1).id);    
  104.          int j2 = Integer.parseInt(((Node)o2).id);    
  105.          return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));    
  106.      }     
  107.     }     
  108. }  
  109.    
  1.    public String init() throws Exception{  
  2.         List  specTableList = null;  
  3.         List list =  cacheMap.get("cacheList");  
  4.         if (list!=null && list.size()>0) {  
  5.             specTableList = cacheMap.get("cacheList");  
  6.         }else{  
  7.             specTableList = specTableBo.queryTableList();  
  8.             cacheMap.put("cacheList", specTableList);  
  9.         }  
  10.         List dataList = initList(specTableList);  
  11.         // 节点列表(散列表,用于临时存储节点对象)    
  12.         HashMap nodeList = new HashMap();    
  13.           // 根节点    
  14.         Node root = new  Node();  
  15.         root.id = "0";  
  16.         root.text= "根节点";  
  17.         root.parentId = "";       
  18.         nodeList.put(root.id, root);  
  19.           // 根据结果集构造节点列表(存入散列表)    
  20.         for (Iterator it = dataList.iterator(); it.hasNext();) {    
  21.            Map dataRecord = (Map) it.next();    
  22.            Node node = new Node();    
  23.            node.id = (String) dataRecord.get("id");    
  24.            node.text = (String) dataRecord.get("text");    
  25.            node.parentId = (String) dataRecord.get("parentId");    
  26.            nodeList.put(node.id, node);    
  27.         }    
  28.           // 构造无序的多叉树    
  29.         Set entrySet = nodeList.entrySet();    
  30.         for (Iterator it = entrySet.iterator(); it.hasNext();) {    
  31.             Node node = (Node) ((Map.Entry) it.next()).getValue();    
  32.             if (node.parentId == null || node.parentId.equals("")) {    
  33.                 root = node;    
  34.             } else {    
  35.                 ((Node) nodeList.get(node.parentId)).addChild(node);    
  36.             }    
  37.         }    
  38.           // 输出无序的树形菜单的JSON字符串    
  39.         System.out.println(root.toString());       
  40.         return "["+root.toString()+"]";  
  41.     }  
0 0