This article mainly introduces the example code for building a tree menu in Java (supporting multi-level menus). It is very good and has reference value. Friends in need can refer to the
renderings: support multi-level menus menu.

Menu entity class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Menu {
private String id;
private String name;
private String parentId;
private String url;
private String icon;
private int order;
private List<Menu> children;
}
|
Copy after login
Menus generally need to be sorted, we sort according to the order field of Menu:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public Comparator<Menu> order(){
Comparator<Menu> comparator = new Comparator<Menu>() {
@Override
public int compare(Menu o1, Menu o2) {
if (o1.getOrder() != o2.getOrder()){
return o1.getOrder() - o2.getOrder();
}
return 0;
}
};
return comparator;
}
|
Copy after login
Generating tree Method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public Map<String,Object> findTree(){
Map<String,Object> data = new HashMap<String,Object>();
try {
List<Menu> allMenu = menuDao.findTree();
List<Menu> rootMenu = new ArrayList<Menu>();
for (Menu nav : allMenu) {
if (nav.getParentId().equals( "0" )){
rootMenu.add(nav);
}
}
Collections.sort(rootMenu, order());
for (Menu nav : rootMenu) {
List<Menu> childList = getChild(nav.getId(), allMenu);
nav.setChildren(childList);
}
data.put( "success" , "true" );
data.put( "list" , rootMenu);
return data;
} catch (Exception e) {
data.put( "success" , "false" );
data.put( "list" , new ArrayList());
return data;
}
}
|
Copy after login
Get submenu:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public List<Menu> getChild(String id,List<Menu> allMenu){
List<Menu> childList = new ArrayList<Menu>();
for (Menu nav : allMenu) {
if (nav.ParentId().equals(id)){
childList.add(nav);
}
}
for (Menu nav : childList) {
nav.setChildren(getChild(nav.getId(), allMenu));
}
Collections.sort(childList,order());
if (childList.size() == 0){
return new ArrayList<Menu>();
}
return childList;
}
|
Copy after login
The final JSON string returned is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | {
"success" : "true" ,
"list" : [
{
"id" : "1" ,
"name" : "Java" ,
"parentid" : "0" ,
"url" : "http://www.php.cn" ,
"order" : 1,
"children" : [
{
"id" : "2" ,
"name" : "并发编程" ,
"parentid" : "1" ,
"url" : "http://www.php.cn" ,
"order" : 1,
"children" : []
},
{
"id" : "3" ,
"name" : "多线程" ,
"parentid" : "1" ,
"url" : "http://www.php.cn" ,
"order" : 2,
"children" : [
"id" : "4" ,
"name" : "Thread" ,
"parentid" : "3" ,
"url" : "http://www.php.cn" ,
"order" : 1,
"children" :[]
]
}
]
},
{
"id" : "5" ,
"name" : "Python" ,
"parentid" : "0" ,
"url" : "http://www.php.cn" ,
"order" : 2,
"children" : []
}
]
}
|
Copy after login
The above is the detailed content of Example code for building a tree menu (including multi-level menu) in Java. For more information, please follow other related articles on the PHP Chinese website!