Recursively generate a Menu as shown in the figure, write two class data model menus, and create a tree menuitree. This is achieved through the following process:
1. First get all root nodes from the menu data.
2. Establish secondary subtrees for root nodes and splice them.
3. Recursively establish a secondary subtree for the child node and connect it until the "tree" above the end node is spliced.
First, write the data model Menu. Each Menu has its own id, parent node parentId, Menu name text, and secondary Menu children.
1 import java.util.List; 2 3 public class Menu { 4 private String id; 5 private String parentId; 6 private String text; 7 private String url; 8 private String yxbz; 9 private List<Menu> children; 10 public Menu(String id,String parentId,String text,String url,String yxbz) { 11 this.id=id; 12 this.parentId=parentId; 13 this.text=text; 14 this.url=url; 15 this.yxbz=yxbz; 16 } 17 /*Omit get\set*/ 18 }
Create a tree like class menuteree. Method getRootNode gets all the root nodes, method builTree summarizes the root nodes to create a tree structure, buildChilTree builds a secondary tree for the node and splices the current tree, recursively calls buildChilTree to continuously open branches and leaves for the current tree until no new subtree is found. Complete the recursion and get the tree structure.
1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class MenuTree { 5 private List<Menu> menuList = new ArrayList<Menu>(); 6 public MenuTree(List<Menu> menuList) { 7 this.menuList=menuList; 8 } 9 10 //Establish tree structure 11 public List<Menu> builTree(){ 12 List<Menu> treeMenus =new ArrayList<Menu>(); 13 for(Menu menuNode : getRootNode()) { 14 menuNode=buildChilTree(menuNode); 15 treeMenus.add(menuNode); 16 } 17 return treeMenus; 18 } 19 20 //Recursion, building subtree structure 21 private Menu buildChilTree(Menu pNode){ 22 List<Menu> chilMenus =new ArrayList<Menu>(); 23 for(Menu menuNode : menuList) { 24 if(menuNode.getParentId().equals(pNode.getId())) { 25 chilMenus.add(buildChilTree(menuNode)); 26 } 27 } 28 pNode.setChildren(chilMenus); 29 return pNode; 30 } 31 32 //Get root node 33 private List<Menu> getRootNode() { 34 List<Menu> rootMenuLists =new ArrayList<Menu>(); 35 for(Menu menuNode : menuList) { 36 if(menuNode.getParentId().equals("0")) { 37 rootMenuLists.add(menuNode); 38 } 39 } 40 return rootMenuLists; 41 } 42 }
Finally, insert some data to try the effect. The resulting json can generate the figure menu.
1 import java.util.ArrayList; 2 import java.util.List; 3 import com.alibaba.fastjson.JSON; 4 5 public class Hello { 6 public static void main(String []args) { 7 List<Menu> menuList= new ArrayList<Menu>(); 8 /*Insert some data*/ 9 menuList.add(new Menu("GN001D000","0","system management","/admin","Y")); 10 menuList.add(new Menu("GN001D100","GN001D000","Privilege management","/admin","Y")); 11 menuList.add(new Menu("GN001D110","GN001D100","Password modification","/admin","Y")); 12 menuList.add(new Menu("GN001D120","GN001D100","New user","/admin","Y")); 13 menuList.add(new Menu("GN001D200","GN001D000","System monitoring","/admin","Y")); 14 menuList.add(new Menu("GN001D210","GN001D200","Online users","/admin","Y")); 15 menuList.add(new Menu("GN002D000","0","Subscription area","/admin","Y")); 16 menuList.add(new Menu("GN003D000","0","Unknown area","/admin","Y")); 17 /*Let's create a tree*/ 18 MenuTree menuTree =new MenuTree(menuList); 19 menuList=menuTree.builTree(); 20 /*Turn to json to see the effect*/ 21 String jsonOutput= JSON.toJSONString(menuList); 22 System.out.println(jsonOutput); 23 } 24 }
@luqp address: https://www.cnblogs.com/lucky-pin/p/10740037.html