JAVA递归生成树形菜单

	
	//获取app功能导航菜单
	public void get_app_menu(){
	
		String org_id = getPara("org_id");
		String app_id = getPara("app_id");
		String role_id = getPara("role_id");
         //查出所有的菜单
		Listmenus = AppModel.dao.getAllMenu(org_id, app_id, role_id);
		ListrootMenus = new ArrayList();
		//获取最顶级的菜单
        for (Record nav:menus) {
			if("0".equals(nav.getStr("menu_pid"))){
				rootMenus.add(nav);
				
			}
		}
		//获取子菜单
		for (Record nav:rootMenus) {
			ListlistChild = getChild(nav.getStr("menu_id"), menus);
			nav.set("childs", listChild);
		}
		map.put("data", rootMenus);
		renderJson(map);
		
	}
	
    //递归获取子菜单方法
	public ListgetChild(String id,Listallmenu ){
		//存放子菜单的集合
		Listlistchild = new ArrayList();
		for (Record nav:allmenu) {
			if(nav.getStr("menu_pid").equals(id)){
				listchild.add(nav);
			}
			
		}
		//递归
		for(Record nav:listchild){
			nav.set("childs", getChild(nav.getStr("menu_id"),allmenu));
		}
		if(listchild.size()==0){
			return null;
		}
		
		return listchild;
	}

参考链接:https://www.cnblogs.com/zuoxh/p/9754460.html

你可能感兴趣的:(Java)