java动态菜单生成

工具类
package com.sunflower.rm.web.utils;
import com.sunflower.rm.web.domain.model.response.AriticleCategoryResponseDto;
import java.util.ArrayList;
import java.util.List;

public class TreeUtil {
    private List<AriticleCategoryResponseDto> menuList = new ArrayList<>();
    public TreeUtil(List<AriticleCategoryResponseDto> menuList) {
        this.menuList=menuList;
    }
    //建立树形结构
    public List<AriticleCategoryResponseDto> builTree() {
         List<AriticleCategoryResponseDto> treeMenus =new  ArrayList<>();
         //this.menuList=treeMenus;
         for (AriticleCategoryResponseDto menuNode : getRootNode()) {
             menuNode = buildChilTree(menuNode);
             treeMenus.add(menuNode);
         }
         return treeMenus;
    }
    //递归,建立子树形结构
    private AriticleCategoryResponseDto buildChilTree(AriticleCategoryResponseDto pNode) {
         List<AriticleCategoryResponseDto> chilMenus = new ArrayList<>();
         for (AriticleCategoryResponseDto menuNode : menuList) {
             if (pNode.getId().equals(menuNode.getCategoryId())) {
                 chilMenus.add(buildChilTree(menuNode));
             }
         }
         pNode.setChildren(chilMenus);
         return pNode;
    }
     //获取根节点
     private List<AriticleCategoryResponseDto> getRootNode() {
         List<AriticleCategoryResponseDto> rootMenuLists = new ArrayList<>();
         for (AriticleCategoryResponseDto menuNode : menuList) {
             if (menuNode.getCategoryId()==0) {
                 rootMenuLists.add(menuNode);
            }
         }
         return rootMenuLists;
    }
}

service调用:
 // 菜单获取
 public JSONObject getMenuInfoById() {
   JSONObject jsonObject = new JSONObject();
    //原始数据
   List<AriticleCategoryResponseDto> menuList=eipCategoryMapper.selectMenu();
   //菜单数据
    List<AriticleCategoryResponseDto> childList=new ArrayList<>();
   /*让我们创建树*/
   TreeUtil tree=new TreeUtil(menuList);
   childList= tree.builTree();
   jsonObject.put("code", 200);
   jsonObject.put("children", childList);
   jsonObject.put("message", "动态菜单获取成功!");
   logger.info("动态菜单获取成功!");
   return jsonObject;
}

最终显示

	"children": [
	    {
	      "id": 42,
	      "label": "首页",
	      "type": 1,
	      "categoryId": 0,
	      "children": [
	        {
	          "id": 43,
	          "label": "新闻",
	          "type": 1,
	          "categoryId": 42,
	          "children": [
	            {
	              "id": 44,
	              "label": "新闻列表",
	              "type": 2,
	              "categoryId": 43,
	              "children": []
	            }
	          ]
	        },
	        {
	          "id": 45,
	          "label": "公告",
	          "type": 1,
	          "categoryId": 42,
	          "children": [
	            {
	              "id": 46,
	              "label": "公告列表",
	              "type": 2,
	              "categoryId": 45,
	              "children": []
	            }
	          ]
	        },
	        {
	          "id": 47,
	          "label": "通知",
	          "type": 1,
	          "categoryId": 42,
	          "children": [
	            {
	              "id": 48,
	              "label": "通知列表",
	              "type": 2,
	              "categoryId": 47,
	              "children": []
	            }
	          ]
	        },
	        {
	          "id": 49,
	          "label": "单位简介",
	          "type": 1,
	          "categoryId": 42,
	          "children": [
	            {
	              "id": 50,
	              "label": "院简介",
	              "type": 2,
	              "categoryId": 49,
	              "children": []
	            },
	            {
	              "id": 51,
	              "label": "发展历程",
	              "type": 2,
	              "categoryId": 49,
	              "children": []
	            },
	            {
	              "id": 52,
	              "label": "院领导",
	              "type": 2,
	              "categoryId": 49,
	              "children": []
	            }
	          ]
	        }
	      ]
	    }
	  ],
	  "message": "动态菜单获取成功!"
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值