重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
小编给大家分享一下SpringMVC+ZTree如何实现树形菜单权限配置,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
成都创新互联主营肥东网站建设的网络公司,主营网站建设方案,成都app开发,肥东h5重庆小程序开发搭建,肥东网站营销推广欢迎肥东等地区企业咨询
计划在开源项目里加入权限配置的功能,打算加入zTree实现树形结构。
Team的Github开源项目链接:https://github.com/u014427391/jeeplatform欢迎star(收藏)
zTree 是一个依靠 jQuery 实现的多功能 “树插件”。优异的性能、灵活的配置、多种功能的组合是 zTree 最大优点。
zTree下载链接:http://www.treejs.cn/v3/main.php#_zTreeInfo
角色信息实体类:
package org.muses.jeeplatform.core.entity.admin; import javax.persistence.*; import java.io.Serializable; import java.util.HashSet; import java.util.Set; /** * @description 角色信息实体类 * @author Nicky * @date 2017年3月16日 */ @Table(name="sys_role") @Entity public class Role implements Serializable{ /** 角色Id**/ private int roleId; /** 角色描述**/ private String roleDesc; /** 角色名称**/ private String roleName; /** 角色标志**/ private String role; private Setpermissions = new HashSet (); @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public int getRoleId() { return roleId; } public void setRoleId(int roleId) { this.roleId = roleId; } @Column(length=100) public String getRoleDesc() { return roleDesc; } public void setRoleDesc(String roleDesc) { this.roleDesc = roleDesc; } @Column(length=100) public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } @Column(length=100) public String getRole() { return role; } public void setRole(String role) { this.role = role; } //修改cascade策略为级联关系 @OneToMany(targetEntity=Permission.class,cascade=CascadeType.MERGE,fetch=FetchType.EAGER) @JoinTable(name="sys_role_permission", joinColumns=@JoinColumn(name="roleId",referencedColumnName="roleId"), inverseJoinColumns=@JoinColumn(name="permissionId",referencedColumnName="id",unique=true)) public Set getPermissions() { return permissions; } public void setPermissions(Set permissions) { this.permissions = permissions; } @Override public boolean equals(Object obj) { if (obj instanceof Role) { Role role = (Role) obj; return this.roleId==(role.getRoleId()) && this.roleName.equals(role.getRoleName()) && this.roleDesc.equals(role.getRoleDesc()) && this.role.equals(role.getRole()); } return super.equals(obj); } }
权限信息实体类:
package org.muses.jeeplatform.core.entity.admin; import java.io.Serializable; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.OneToOne; import javax.persistence.Table; /** * @description 权限操作的Vo类 * @author Nicky * @date 2017年3月6日 */ @Table(name="sys_permission") @Entity public class Permission implements Serializable { private int id; private String pdesc; private String name; private static final long serialVersionUID = 1L; private Menu menu; private Setoperations = new HashSet (); public Permission() { super(); } @GeneratedValue(strategy = GenerationType.IDENTITY) @Id public int getId() { return this.id; } public void setId(int id) { this.id = id; } @Column(length=100) public String getPdesc() { return this.pdesc; } public void setPdesc(String pdesc) { this.pdesc = pdesc; } @Column(length=100) public String getName() { return this.name; } public void setName(String name) { this.name = name; } @OneToOne(targetEntity=Menu.class,cascade=CascadeType.REFRESH,fetch=FetchType.EAGER) @JoinColumn(name="menuId",referencedColumnName="menuId") public Menu getMenu() { return menu; } public void setMenu(Menu menu) { this.menu = menu; } @ManyToMany(targetEntity=Operation.class,cascade=CascadeType.MERGE,fetch=FetchType.EAGER) @JoinTable(name="sys_permission_operation",joinColumns=@JoinColumn(name="permissionId",referencedColumnName="id"),inverseJoinColumns=@JoinColumn(name="operationId",referencedColumnName="id")) public Set getOperations() { return operations; } public void setOperations(Set operations) { this.operations = operations; } }
实现菜单信息实体类,用JPA来实现
package org.muses.jeeplatform.core.entity.admin; import javax.persistence.*; import java.io.Serializable; import java.util.List; /** * @description 菜单信息实体 * @author Nicky * @date 2017年3月17日 */ @Table(name="sys_menu") @Entity public class Menu implements Serializable { /** 菜单Id**/ private int menuId; /** 上级Id**/ private int parentId; /** 菜单名称**/ private String menuName; /** 菜单图标**/ private String menuIcon; /** 菜单URL**/ private String menuUrl; /** 菜单类型**/ private String menuType; /** 菜单排序**/ private String menuOrder; /**菜单状态**/ private String menuStatus; private List
实现JpaRepository接口
package org.muses.jeeplatform.core.dao.repository.admin; import org.muses.jeeplatform.core.entity.admin.Role; import org.springframework.data.jpa.repository.JpaRepository; /** * Created by Nicky on 2017/12/2. */ public interface RoleRepository extends JpaRepository{ }
实现JpaRepository接口
package org.muses.jeeplatform.core.dao.repository.admin; import org.muses.jeeplatform.core.entity.admin.Menu; import org.springframework.data.jpa.repository.JpaRepository; /** * Created by Nicky on 2017/6/17. */ public interface MenuTreeRepository extends JpaRepository{ }
角色Service类:
package org.muses.jeeplatform.service; import com.google.common.collect.Lists; import org.muses.jeeplatform.core.dao.repository.admin.RolePageRepository; import org.muses.jeeplatform.core.entity.admin.Role; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import java.util.List; /** * Created by Nicky on 2017/7/30. */ @Service public class RolePageService { @Autowired RolePageRepository roleRepository; /** * 构建PageRequest对象 * @param num * @param size * @param asc * @param string * @return */ private PageRequest buildPageRequest(int num, int size, Sort.Direction asc, String string) { return new PageRequest(num-1, size,null,string); } /** * 获取所有的菜单信息并分页显示 * @param pageNo * 当前页面数 * @param pageSize * 每一页面的页数 * @return */ public PagefindAll(int pageNo, int pageSize, Sort.Direction dir, String str){ PageRequest pageRequest = buildPageRequest(pageNo, pageSize, dir, str); Page roles = roleRepository.findAll(pageRequest); return roles; } public List findAllRole(){ Iterable roles = roleRepository.findAll(); List myList = Lists.newArrayList(roles); return myList; } /** * 根据角色id查找角色信息 * @param roleId * @return */ public Role findByRoleId(String roleId){ return roleRepository.findOne(Integer.parseInt(roleId)); } /** * 保存角色信息 * @param role */ public void doSave(Role role){ roleRepository.save(role); } }
菜单Service类:
package org.muses.jeeplatform.service; import org.muses.jeeplatform.annotation.redisCache; import org.muses.jeeplatform.common.RedisCacheNamespace; import org.muses.jeeplatform.core.dao.repository.admin.MenuTreeRepository; import org.muses.jeeplatform.core.entity.admin.Menu; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * Created by Nicky on 2017/6/17. */ @Service public class MenuTreeService { @Autowired MenuTreeRepository menuTreeRepository; /** * 查询所有的菜单 * @return */ @Transactional //@RedisCache public ListfindAll(){ return menuTreeRepository.findAll(); } }
在Controller类里通过角色id获取该角色可以查看的菜单:
/** * 跳转到角色授权页面 * @param roleId * @param model * @return */ @RequestMapping(value = "/goAuthorise" ) public String goAuth(@RequestParam String roleId, Model model){ ListmenuList = menuTreeService.findAll(); Role role = roleService.findByRoleId(roleId); Set hasPermissions = null; if(role != null){ hasPermissions = role.getPermissions(); } for (Menu m : menuList) { for(Permission p : hasPermissions){ if(p.getMenu().getMenuId()==m.getMenuId()){ m.setHasSubMenu(true); } } } model.addAttribute("roleId" , roleId); JSONArray jsonArray = JSONArray.fromObject(menuList); String json = jsonArray.toString(); json = json.replaceAll("menuId","id").replaceAll("parentId","pId"). replaceAll("menuName","name").replaceAll("hasSubMenu","checked"); model.addAttribute("menus",json); return "admin/role/role_auth"; }
在前端通过zTree实现树形菜单展示,通过勾选然后实现角色授权:
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>" rel="external nofollow" > Insert title here plugins/zTree/3.5/zTreeStyle.css" rel="external nofollow" type="text/css">
以上是“SpringMVC+ZTree如何实现树形菜单权限配置”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!