基本思路:父节点装到一个list,子节点装到另外一个list,遍历父节点,再从父节点里面遍历子节点,拼接相应的html字符串。实现方法如下
public Map<String,Object> queryAccountMenu(Integer id) { List<Resources> list = this.resourcesMapper.queryAccountMenu(id); StringBuffer sb = new StringBuffer(); List<Resources> folderList = new ArrayList<Resources>(); List<Resources> menuList = new ArrayList<Resources>(); for(Resources r : list){ if(r.getType()==0){ folderList.add(r); }else if(r.getType()==2){ menuList.add(r); } } for(Resources res : folderList){ sb.append("<div class=\"accordionHeader\"><h2><span>Folder</span>" + res.getName() + "</h2></div>"); sb.append("<div class=\"accordionContent\"><ul class=\"tree treeFolder\">"); for(Resources menu : menuList){ if(menu.getParentId() == res.getId()){ sb.append("<li><a href=\"" + menu.getResUrl() + "\" target=\""+menu.getTarget()+"\" rel=\""+menu.getResKey()+"\">" + menu.getName() + "</a></li>"); } } sb.append("</ul></div>"); } HashMap<String, Object> map = new HashMap<String, Object>(); map.put("menuHtml", sb.toString()); map.put("menuList", list); return map; }