Springmvc+ztree Implement tree Menu permissions configuration

Source: Internet
Author: User
Tags int size jquery library

Plan to add the function of permission configuration in open source project, and intend to join Ztree to implement tree structure.

Team's GitHub Open source Project Link: https://github.com/u014427391/jeeplatform
Welcome to Star (collection)

Ztree is a versatile "tree plugin" that relies on jQuery. The combination of excellent performance, flexible configuration and multiple functions is the biggest advantage of Ztree.

Ztree Download Link: http://www.treejs.cn/v3/main.php#_zTreeInfo

Role Information entity class:

Package Org.muses.jeeplatform.core.entity.admin;import Javax.persistence.*;import Java.io.serializable;import Java.util.hashset;import java.util.set;/** * @description Role Information entity class * @author Nicky * @date March 16, 2017 */@Table (name= "Sys_r    Ole ") @Entitypublic class role implements serializable{/** roles id**/private int roleid;    /** role Description **/private String roledesc;    /** role name **/private String roleName;    /** role Flag **/private String role;    Private set<permission> permissions = new hashset<permission> ();    @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; }//Modify the Cascade policy as a cascading relationship @OneToMany (Targetentity=permission.class,cascade=cascadetype.merge,fetch=fetchtype.eager) @J Ointable (name= "sys_role_permission", [email protected] (name= "Roleid", Referencedcolumnname= "RoleId"), [email  protected] (name= "PermissionID", referencedcolumnname= "id", unique=true)) public set<permission>    GetPermissions () {return permissions;    } public void SetPermissions (set<permission> 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); }}

Permission information entity class:

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 permission Operation Vo class * @author Nicky * @date March 6, 2017 */@Table (name= "sys_permission") @    Entitypublic class Permission implements Serializable {private int id;    Private String Pdesc;    private String name;    Private static final long serialversionuid = 1L;    Private menu Menu;    Private set<operation> operations = new hashset<operation> ();    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 ", [email protected] (name=" PermissionID ", referencedcolumnname=" id "),[email  Protected] (name= "OperationID", referencedcolumnname= "id")) public set<operatioN> getoperations () {return operations;    } public void Setoperations (set<operation> operations) {this.operations = operations; }}

Implement the menu information entity class, implemented with JPA

Package Org.muses.jeeplatform.core.entity.admin;import Javax.persistence.*;import Java.io.serializable;import java.util.list;/** * @description Menu Information Entity * @author Nicky * @date March 17, 2017 * * @Table (name= "Sys_menu") @Entitypublic class M        ENU implements Serializable {/** menu id**/private int menuId;        /** Superior id**/private int parentid;        /** Menu Name **/private String menuName;        /** menu icon **/private String Menuicon;        /** Menu url**/private String menuurl;        /** Menu Type **/private String menutype;    /** Menu Sort **/private String menuorder;    /** Menu State **/private String menustatus;    Private List<menu> submenu;    Private String target;    Private Boolean hassubmenu = false;    Public Menu () {super (); } @Id @GeneratedValue (strategy=generationtype.identity) public int getmenuid () {return this.menuid    ;    } public void Setmenuid (int menuId) {this.menuid = MenuId; } @Column (length=100) public INT Getparentid () {return parentid;    } public void Setparentid (int parentid) {this.parentid = ParentID;    } @Column (length=100) public String getmenuname () {return this.menuname;    } public void Setmenuname (String menuName) {this.menuname = MenuName;    } @Column (length=30) public String Getmenuicon () {return this.menuicon;    } public void Setmenuicon (String menuicon) {This.menuicon = Menuicon;    } @Column (length=100) public String Getmenuurl () {return this.menuurl;    } public void Setmenuurl (String menuurl) {this.menuurl = Menuurl;    } @Column (length=100) public String Getmenutype () {return this.menutype;    } public void Setmenutype (String menutype) {this.menutype = Menutype;    } @Column (length=10) public String Getmenuorder () {return menuorder; } public void Setmenuorder (String menuorder) {This.menuorder = MenUorder;    } @Column (length=10) public String getmenustatus () {return menustatus;    } public void Setmenustatus (String menustatus) {this.menustatus = Menustatus;    } @Transient Public list<menu> GetSubMenu () {return submenu;    } public void Setsubmenu (List<menu> submenu) {this.submenu = submenu;    The public void Settarget (String target) {this.target = target;    } @Transient Public String Gettarget () {return target;    The public void Sethassubmenu (Boolean hassubmenu) {this.hassubmenu = Hassubmenu;    } @Transient public Boolean gethassubmenu () {return hassubmenu; }}

Implementing the Jparepository Interface

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<Role,Integer> {}

Implementing the Jparepository Interface

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<Menu,Integer>{}

Role service class:

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.    */@Servicepublic class Rolepageservice {@Autowired rolepagerepository rolerepository;    /** * Build pagerequest Object * @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); }/** * Get all the menu information and page display * @param pageno * Current page number * @parAM pageSize * Number of pages per page * @return */public page<role> findAll (int pageno, int pageSize, Sort.        Direction dir, String str) {pagerequest pagerequest = buildpagerequest (PageNo, PageSize, dir, str);        page<role> roles = Rolerepository.findall (pagerequest);    return roles;        } public list<role> Findallrole () {iterable<role> roles = Rolerepository.findall ();        list<role> myList = lists.newarraylist (roles);    return myList; /** * Find role information by role ID * @param roleid * @return * * * Public role Findbyroleid (String Roleid) {RE    Turn Rolerepository.findone (Integer.parseint (Roleid));    }/** * Save role Information * @param role * * public void Dosave (role role) {rolerepository.save (role); }}

Menu Service class:

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. */@Servicepublic class MenuTreeService {    @Autowired    MenuTreeRepository menuTreeRepository;    /**     * 查询所有的菜单     * @return     */    @Transactional    //@RedisCache    public List<Menu> findAll(){        return menuTreeRepository.findAll();    }}

In the Controller class, get the menu that the role can view through the role ID:

/** * Jump to role Authorization page * @param roleid * @param model * @return */@RequestMapping (value = "/goauthorise" public string Goauth (@RequestParam string roleid, model model) {list<menu> menulist = menutreeservice.fi        Ndall ();        Role role = Roleservice.findbyroleid (Roleid);        Set<permission> 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"; }

The

Implements a tree menu display on the front end through ztree, by ticking and then implementing the role authorization:

<%@ 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+ "/";%> <! DOCTYPE html>

Team's GitHub Open source Project Link: https://github.com/u014427391/jeeplatform
Welcome to Star (collection)

Springmvc+ztree Implement tree Menu permissions configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.