很多时候我们在前段展现时要用到多级菜单,刚好今天做了个简单的,整理一下;
首先我们要确定要展现的菜单结构:
--根菜单
--一级菜单A
--二级菜单A
--三级菜单A
--一级菜单B
与Hibernate构造无限极菜单不同的是,这个菜单传到前端时已经构造好,不是类似AJAX请求动态生成菜单;
好了 直奔主题:
import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class MenuTest { /** * @param args */ public static void main(String[] args) { MenuTest menuTest = new MenuTest(); List menuList = initMenu();//初始化菜单 Map menuMap = menuTest.rebuildMenus(menuList);//重构菜单 menuTest.showMenu(menuMap);//打印菜单 } /** * 重构菜单,构造成需要的结构 * @param menuList * @return */ private Map rebuildMenus(List menuList) { List rootMenuList=new ArrayList();//这个List用来存放根菜单 for (int j = 0; j < menuList.size(); j++) { Map menu = (Map) menuList.get(j); String parentId = menu.get("parentId").toString(); if (null==parentId || "".equals(parentId)) { rootMenuList.add(menu); } } menuList.removeAll(rootMenuList);//将根菜单从menuList中除去 HashMap menuMap = new HashMap(); for (int j = 0; j < rootMenuList.size(); j++) {//遍历根菜单,每一个跟菜单都遍历到最后一个子菜单(叶子节点) Map menu = (Map) rootMenuList.get(j); menu = this.pase(menu, menuList); menuMap.put(menu.get("id"), menu); } return menuMap; } /** * 递归构造菜单Map * @param menu * @param menuList * @return */ private Map pase(Map menu, List menuList) { for (int j = 0; j < menuList.size(); j++) { Map menu2 = (Map) menuList.get(j); if (menu2.get("parentId").toString().equals(menu.get("id").toString())) { if(menu.get("children") == null){ menu.put("children", new ArrayList()); } ((List)menu.get("children")).add(menu2); pase(menu2, menuList); } } return menu; } /** * 打印菜单 * @param menuMap */ private void showMenu(Map menuMap){ Iterator it = menuMap.keySet().iterator(); while(it.hasNext()){ Map menu = (Map) menuMap.get(it.next()); System.out.println(menu.get("id")+"--"+menu.get("name")); this.buildChildMenu(menu); } } /** * 构造子菜单 * @param menu */ private void buildChildMenu(Map menu){ List childList = (List) menu.get("children"); if(null!=childList){ for(int i=0;i<childList.size();i++){ Map childMap = (Map) childList.get(i); System.out.println(childMap.get("id")+"--"+childMap.get("name")); this.buildChildMenu(childMap); } } } /** * 初始化菜单,实际项目中这部分数据是从数据库中获取 * @return */ public static List initMenu(){ List menuList = new ArrayList(); Map menuMap = new HashMap(); menuMap.put("id", "0000"); menuMap.put("name", "根菜单"); menuMap.put("parentId","");//根菜单的父菜单为空 Map menuMap1 = new HashMap(); menuMap1.put("id", "1000"); menuMap1.put("name", "一级菜单A"); menuMap1.put("parentId","0000"); Map menuMap2 = new HashMap(); menuMap2.put("id", "2000"); menuMap2.put("name", "一级菜单B"); menuMap2.put("parentId","0000"); Map menuMap3 = new HashMap(); menuMap3.put("id", "1100"); menuMap3.put("name", "二级菜单A"); menuMap3.put("parentId","1000"); Map menuMap4 = new HashMap(); menuMap4.put("id", "1110"); menuMap4.put("name", "三级菜单A"); menuMap4.put("parentId","1100"); menuList.add(menuMap); menuList.add(menuMap1); menuList.add(menuMap2); menuList.add(menuMap3); menuList.add(menuMap4); return menuList; } }