packagecom.sys.menutree;importcom.sys.dao.MenuMapper;importcom.sys.domain.Menu;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.ResponseBody;import java.util.*;/*** @Author:jimisun
* @Description:
* @Date:Created in 18:02 2018/8/8
* @Modified By:*/@Controller
@RequestMapping("/menu")public classMenuController {
@AutowiredprivateMenuMapper menuMapper;
@RequestMapping(value= "/findTree", method =RequestMethod.POST)
@ResponseBodypublic Map findTree(@RequestBody(required = false) Menu menu) {//构建返回数据
Map data = new HashMap();try{//查询到的所有菜单
List
List
if (menu != null && menu.getId() != null) {//父节点为传递的id为根节点
for(Menu nav : allMenu) {if(nav.getParentid().equals(menu.getId())) {
rootMenu.add(nav);
}
}
}else{//父节点是0的,为根节点。
for(Menu nav : allMenu) {if (nav.getParentid().equals(0)) {
rootMenu.add(nav);
}
}
}//根据Menu类的order排序
Collections.sort(rootMenu);//为根菜单设置子菜单,getClild是递归调用的
for(Menu nav : rootMenu) {//获取根节点下的所有子节点 使用getChild方法
List
nav.setChildren(childList);
}
data.put("success", "true");
data.put("list", rootMenu);returndata;
}catch(Exception e) {
data.put("success", "false");
data.put("list", newArrayList());returndata;
}
}/*** 递归设置栏目的子节点
*
*@paramid 父节点id
*@paramallMenu 节点列表
*@return
*/
private List
List
if(nav.getParentid().equals(id)) {
childList.add(nav);
}
}//递归设置子节点
for(Menu nav : childList) {
nav.setChildren(getChild(nav.getId(), allMenu));
}//排序
Collections.sort(childList);//如果节点下没有子节点,返回一个空List(递归退出)
if (childList.size() == 0) {return new ArrayList
}returnchildList;
}
}