一直以来没工夫仔细琢磨菜单树怎么做,只知道递归,现在才有空闲实现一次。
数据结构:
调用部分的代码,浏览器地址为:http://127.0.0.1:8080/admin/resource/menu?userId=23123
buildTree的代码:
public JSONArray buildTree(List<Resource> resListAll, List<Resource> iteratedList, int parentId) { JSONArray arr = new JSONArray(); for (Resource resource : resListAll) { if (iteratedList.contains(resource)) { continue; } if (resource.getParentId() == parentId) { iteratedList.add(resource); String text = JSONObject.toJSONString(resource); JSONObject res = JSONObject.parseObject(text); arr.add(res); res.put("children", buildTree(resListAll, iteratedList, resource.getId())); } } return arr; }
结果:
{ "resourceTree": [{ "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "采购管理", "parentId": 0, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544149306000, "children": [{ "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "非日常采购", "parentId": 5, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 23, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "日常采购管理", "parentId": 5, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 22, "resourceType": "menu" }], "id": 5, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "场地管理", "parentId": 0, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544148935000, "children": [{ "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "合作酒店场地管理", "parentId": 4, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 21, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "会议室场地管理", "parentId": 4, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 20, "resourceType": "menu" }], "id": 4, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "支出管理", "parentId": 0, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544148987000, "children": [{ "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "投标支出管理", "parentId": 3, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [{ "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "星际投标", "parentId": 19, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [{ "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "仙女星云投标", "parentId": 30, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544443215000, "children": [], "id": 33, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "银河系投标", "parentId": 30, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544443211000, "children": [], "id": 32, "resourceType": "menu" }], "id": 30, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "国际投标", "parentId": 19, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 29, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "国内投标", "parentId": 19, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 28, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "省内投标", "parentId": 19, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 27, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "市内投标", "parentId": 19, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 26, "resourceType": "menu" }], "id": 19, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "日常支出管理", "parentId": 3, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 18, "resourceType": "menu" }], "id": 3, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "考勤管理", "parentId": 0, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544148905000, "children": [{ "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "会议考勤管理", "parentId": 2, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 17, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "日常考勤管理", "parentId": 2, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 16, "resourceType": "menu" }], "id": 2, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "系统管理", "parentId": 0, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544148843000, "children": [{ "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "签到管理", "parentId": 1, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [{ "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "离场签到", "parentId": 15, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 25, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "进场签到", "parentId": 15, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 24, "resourceType": "menu" }], "id": 15, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "页面管理", "parentId": 1, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 14, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "菜单管理", "parentId": 1, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 13, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "权限管理", "parentId": 1, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 12, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "", "orderNum": 0, "resourceDescription": "", "resourceName": "角色管理", "parentId": 1, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 11, "resourceType": "menu" }, { "resourceRemark": "", "resourceContent": "/", "orderNum": 0, "resourceDescription": "首页页面", "resourceName": "用户管理", "parentId": 1, "resourceStatus": "disabled", "depth": 0, "resourceUrl": "", "createTime": 1544444261000, "children": [], "id": 10, "resourceType": "menu" }], "id": 1, "resourceType": "menu" }] }