@RequestMapping(value = "save", method = RequestMethod.POST) public String save(@ModelAttribute("e") Menu menu) throws Exception { if (menu.getId() == null || menu.getId().equals("")) { if (menu.getUrl() == null) { menu.setUrl(""); } menuService.insert(menu); } else { menuService.update(menu); } return selectList(RequestHolder.getRequest(), menu); }
// 根据父ID加载子节点 private void loadChildrenByPid(MenuItem item, Menu menu, String url, User u) { Map<String, String> param = new HashMap<String, String>(); if (u != null && u.getRid() != null) param.put("rid", u.getRid()); param.put("pid", menu.getPid()); // 加载菜单节点 List<Menu> data = menuService.selectList(param); if (data == null || data.size() == 0) { return; } if (item.getChildren() == null) item.setChildren(new ArrayList<MenuItem>()); // 创建菜单节点 for (int i = 0; i < data.size(); i++) { Menu entry = data.get(i); MenuItem addItem = new MenuItem(entry.getName(), null); addItem.setId(entry.getId()); addItem.setPid(entry.getPid()); addItem.setMenuType(entry); String url0 = null; if (url != null) { addItem.setUrl(url); url0 = url; } else { addItem.setUrl(entry.getUrl()); url0 = entry.getUrl(); } // // System.out.println("entry.getType()="+entry.getType()+",MenuType.button="+MenuType.button); if (entry.getType().equals("button")) { addUserResourceMenusButton(url0); } else { item.getChildren().add(addItem); } } // 根据菜单节点进行递归加载 for (int i = 0; i < item.getChildren().size(); i++) { MenuItem childItem = item.getChildren().get(i); if (!childItem.isButton()) { Menu itemMenu = new Menu(); itemMenu.setPid(childItem.getId()); // itemMenu.setMenuType(entry); loadChildrenByPid(childItem, itemMenu, url, u); } } }
/** * 根据角色ID,加载用户菜单资源 * * @param u * @param pid * @param url * @return */ private List<MenuItem> loadMenus(User u, String pid, String url) { /* * 首先,加载顶级目录或页面菜单 */ Map<String, String> param = new HashMap<String, String>(); if (u != null && u.getRid() != null) { param.put("rid", u.getRid()); // 角色ID } param.put("pid", pid); // 菜单父ID List<Menu> menus = menuService.selectList(param); // 创建菜单集合 List<MenuItem> root = new ArrayList<MenuItem>(); // 循环添加菜单到菜单集合 for (Iterator<Menu> it = menus.iterator(); it.hasNext(); ) { Menu entry = it.next(); MenuItem item = new MenuItem(entry.getName(), null); item.setId(entry.getId()); item.setPid(entry.getPid()); item.setMenuType(entry); // if(item.getType().equals(MenuType.page)){ // item.setIcon("http://127.0.0.1:8082/myshop/resource/images/letter.gif"); // } if (url != null) { item.setUrl(url); } else { item.setUrl(entry.getUrl()); } root.add(item); } /* * 其次,加载子菜单 或 按钮功能 */ for (int i = 0; i < root.size(); i++) { MenuItem item = root.get(i); if (!item.isButton()) { Menu mm = new Menu(); mm.setPid(item.getId()); loadChildrenByPid(root.get(i), mm, url, u); } else { // addUserResourceMenusButton(item.getUrl()); } } return root; }
/** * 添加/修改菜单 修改选中的菜单,为该菜单添加子菜单 * * @return * @throws Exception */ @RequestMapping(value = "addOrUpdate", method = RequestMethod.POST) @ResponseBody public String addOrUpdate(HttpServletRequest request) throws Exception { // 选中菜单的信息 String updateP = request.getParameter("updateP"); String id = request.getParameter("id"); String name = request.getParameter("name"); String orderNum = request.getParameter("orderNum"); String type = request.getParameter("type"); // 要添加的子菜单 String url = request.getParameter("url"); String n_name = request.getParameter("n_name"); String n_url = request.getParameter("n_url"); String parentOrChild = request.getParameter("parentOrChild"); String n_orderNum = request.getParameter("n_orderNum"); String n_type = request.getParameter("n_type"); Menu itemMenu = null; if (n_name != null && !n_name.trim().equals("")) { itemMenu = new Menu(); // 添加子菜单 if (parentOrChild.equals("0")) { // 顶级模块 itemMenu.setPid("0"); itemMenu.setType(MenuType.module.toString()); } else if (parentOrChild.equals("1")) { // 顶级页面 itemMenu.setPid("0"); itemMenu.setType(MenuType.page.toString()); } else if (parentOrChild.equals("2")) { // 子模块 itemMenu.setPid(id); itemMenu.setType(MenuType.module.toString()); } else if (parentOrChild.equals("3")) { // 子页面 itemMenu.setPid(id); itemMenu.setType(MenuType.page.toString()); } else if (parentOrChild.equals("4")) { // 功能 itemMenu.setPid(id); itemMenu.setType(MenuType.button.toString()); } else { throw new IllegalAccessException("添加菜单异常。"); } itemMenu.setName(n_name); itemMenu.setUrl(n_url); itemMenu.setOrderNum(Integer.valueOf(n_orderNum)); itemMenu.setType(n_type); } // 修改父菜单 Menu m = new Menu(); m.setId(id); m.setName(name); m.setUrl(url); m.setOrderNum(Integer.valueOf(orderNum)); m.setType(type); menuService.addOrUpdate(updateP, m, itemMenu); return "ok"; }
@Override public void insertAfter(Menu e) { // TODO Auto-generated method stub e.clear(); }