java框架 初始化菜单树
Posted 游走在程序世界的小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java框架 初始化菜单树相关的知识,希望对你有一定的参考价值。
@RequestMapping("/tree") @DoLog(cnContent = "权限树初始化", value = false) //@Permission(name = "login.tree") public String tree(HttpServletRequest request, HttpServletResponse response, ZTreeComm zTree) throws Exception { try { //1、获取登陆人的信息 SysUser currentUser = getLoginUser(request); // 2.根据id获取权限列表,id为空时获取父权限列表(limit=roleid) List<ZTreeComm> tree = sysUserService.initAuthorityTree(zTree, currentUser); // 3.设置父节点 List<ZTreeComm> votree = new ArrayList<ZTreeComm>(); for (int i = 0; i < tree.size(); i++) { ZTreeComm ztree = (ZTreeComm) tree.get(i); int total = sysUserService.isParent(ztree); ztree.setIsParent(total != 0 ? Constants.IS_TRUE : Constants.IS_FALSE); votree.add(ztree); } //4、list排序。 Collections.sort(votree, new Comparator<ZTreeComm>() { public int compare(ZTreeComm arg0, ZTreeComm arg1) { return arg0.getSortNo().compareTo(arg1.getSortNo()); } }); return this.ajax(response, votree); } catch (Exception e) { this.logException(e); return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系!"); } }
@Override public List<ZTreeComm> initAuthorityTree(ZTreeComm ztree, SysUser sysUser) throws Exception { //1、获取角色字符串 List<SysRole> sysRoles = sysUser.getSysRoles(); String roleId = ""; if (CollectionHelp.isNotBank(sysRoles)) { for (Object obj : sysRoles) { SysRole role = (SysRole) obj; if (Constants.STATUS_ACTIVE.equals(role.getStatus())) { roleId = role.getRoleId() + "," + roleId; } } } ztree.setDiyParams(roleId); //因为ztree并没有roleid的属性,所以使用ztree的空属性 //2、根据ztree,获取权限信息 List<ZTreeComm> dataList = new ArrayList<ZTreeComm>(); if (StringUtils.isEmpty(ztree.getId())) { //id为空,初始加载,查询根节点. where parent_dept_id is null dataList = sqlSessionTemplate.selectList(SecurityMgrConstants.ZTREE_COMMON_MAPPER_NAMESPACE + ".getAuthorityRoot", ztree); } else { //id不为空,为下拉,查询子节点 . where parent_dept_id =id dataList = sqlSessionTemplate.selectList(SecurityMgrConstants.ZTREE_COMMON_MAPPER_NAMESPACE + ".getAuthorityById", ztree); } return dataList; }
获取用户的所有角色,并将角色id放入字符串中并存入ZtreeComm类中的自定义属性中去DiyParams
ZtreeComm应该是对应Sys_Authority表,但是表中并没有对应roleid的字段,所以实体类中将roleid以字符串的形式存储到ztree中去
接下来判断是否是根节点,//id为空,初始加载,查询根节点,//id不为空,为下拉,查询子节点
<!-- 查询部门根节点 --> <select id="getAuthorityRoot" resultMap="authorityTreeResultMap" parameterType="ZTreeComm"> SELECT * FROM SYS_AUTHORITY WHERE AUTHORITY_ID IN( SELECT AUTHORITY_ID FROM SYS_ROLE_AUTHORITY WHERE INSTR(#{diyParams},ROLE_ID)>0 ) AND PARENT_ID IS NULL AND AUTHORITY_TYPE=‘2‘ </select>
AUTHORITY_TYPE 权限类型: 1:普通权限 2:菜单 3:报表
获取权限集合后,判断每个权限哪些是父节点
for (int i = 0; i < tree.size(); i++) { ZTreeComm ztree = (ZTreeComm) tree.get(i); int total = sysUserService.isParent(ztree); ztree.setIsParent(total != 0 ? Constants.IS_TRUE : Constants.IS_FALSE); votree.add(ztree); }
<!-- 查询查看是否为父节点 --> <select id="isParent" resultType="integer" parameterType="ZTreeComm"> SELECT COUNT(*) FROM SYS_AUTHORITY WHERE PARENT_ID=#{id} AND AUTHORITY_TYPE=‘2‘ </select>
并给每个ztree的isparent赋值并存入新的集合中去
对新的集合进行排序
//4、list排序。 Collections.sort(votree, new Comparator<ZTreeComm>() { public int compare(ZTreeComm arg0, ZTreeComm arg1) { return arg0.getSortNo().compareTo(arg1.getSortNo()); } });
以上是关于java框架 初始化菜单树的主要内容,如果未能解决你的问题,请参考以下文章