java easyui权限管理_easyui--权限管理

1、权限目的:

是为了让不同的用户可以操作系统中不同资源

直接点说就是不同的用户可以操作不同的菜单

核心:实现菜单权限的核心思想就是控制用户登录后台所传递的menuId(与树形菜单分类列段关联的用户信息列段)

2、二星权限设计(用户权限多对多)

2.1、执行数据库脚本

用户登录信息表t_easyui_user_version2

61e75f89f05fddb9feca4b25b4472976.png

用户菜单中间表t_easyui_usermenu

753edccf9090ffcff35a6eae48c9fc14.png

菜单管理表请参照上一篇:easyUI--入门实例

2.2建立实体类

1 public classTreeNode {2 privateString id;3 privateString text;4 private List children=new ArrayList();5 private Map attributes=new HashMap();6

7

8 publicString getId() {9 returnid;10 }11

12 public voidsetId(String id) {13 this.id =id;14 }15

16 publicString getText() {17 returntext;18 }19

20 public voidsetText(String text) {21 this.text =text;22 }23

24 public ListgetChildren() {25 returnchildren;26 }27

28 public void setChildren(Listchildren) {29 this.children =children;30 }31

32 public MapgetAttributes() {33 returnattributes;34 }35

36 public void setAttributes(Mapattributes) {37 this.attributes =attributes;38 }39

40 @Override41 publicString toString() {42 return "TreeNode [id=" + id + ", text=" + text + ", children=" + children + ", attributes=" + attributes + "]";43 }44

45

46 }

2.3创建dao  UserDao

packagecom.yuan.dao;importjava.sql.SQLException;importjava.util.List;importjava.util.Map;importcom.yuan.util.JsonBaseDao;importcom.yuan.util.JsonUtils;importcom.yuan.util.PageBean;importcom.yuan.util.StringUtils;public class UserDao extendsJsonBaseDao {/*** 用户登录或者查询用户分页信息的公共方法

*@parampaMap

*@parampageBean

*@return*@throwsInstantiationException

*@throwsIllegalAccessException

*@throwsSQLException*/

public List> list(Map paMap,PageBean pageBean) throwsInstantiationException, IllegalAccessException, SQLException{

String sql="SELECT * FROM t_easyui_user_version2 WHERE TRUE ";

String uid=JsonUtils.getParamVal(paMap, "uid");

String upwd=JsonUtils.getParamVal(paMap, "upwd");if(StringUtils.isNotBlank(uid)) {

sql+=" AND uid="+uid;

}if(StringUtils.isNotBlank(upwd)) {

sql+=" AND upwd="+upwd;

}return super.executeQuery(sql, pageBean);

}/*** 根据当前用户登录的ID去查询对应的所菜单

*@parampaMap

*@parampageBean

*@return*@throwsInstantiationException

*@throwsIllegalAccessException

*@throwsSQLException*/

public List> getMenuByUid(Map paMap,PageBean pageBean) throwsInstantiationException, IllegalAccessException, SQLException{

String sql="SELECT * FROM t_easyui_usermenu WHERE TRUE ";

String uid=JsonUtils.getParamVal(paMap, "uid");if(StringUtils.isNotBlank(uid)) {

sql+=" AND uid="+uid;

}return super.executeQuery(sql, pageBean);

}

}

2.4修改原有的dao  MenuDao

主要修改是将listMap方法修改成listMapAuth,

.listMap方法只负责查询子节点,不包括本身

.listMapAuth方法可以查询子节点包括自身节点

packagecom.hmc.dao;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importcom.hmc.entity.TreeNode;importcom.hmc.util.JsonBaseDao;importcom.hmc.util.JsonUtils;importcom.hmc.util.PageBean;importcom.hmc.util.StringUtils;public class MenuDao extendsJsonBaseDao {/*** 给前台tree_data1_json的字符串

*@parampaMap 从前台jsp传递过来的参数集合

*@parampageBean

*@return*@throwsInstantiationException

*@throwsIllegalAccessException

*@throwsSQLException*/

public List listTreeNode(Map paMap,PageBean pageBean) throwsInstantiationException, IllegalAccessException, SQLException{

List> listMap = this.listMapAuth(paMap, pageBean);

List listTreeNode=new ArrayList();this.listMapToListTreeNode(listMap, listTreeNode);returnlistTreeNode;

}

//public List> listMap(Map paMap,PageBean pageBean) throwsInstantiationException, IllegalAccessException, SQLException{

// String sql="SELECT * FROM t_easyui_menu WHERE TRUE";

// String menuId=JsonUtils.getParamVal(paMap, "Menuid");

//if(StringUtils.isNotBlank(menuId)) {

// sql+=" AND parentid="+menuId;

// }

//else{

// sql+=" AND parentid=-1";

// }

//

这里面存放的是数据库中的菜单信息

// List> listMap = super.executeQuery(sql, pageBean);

//returnlistMap;

// }

//public List> listMapAuth(Map paMap,PageBean pageBean) throwsInstantiationException, IllegalAccessException, SQLException{

String sql="SELECT * FROM t_easyui_menu WHERE TRUE";

String menuId=JsonUtils.getParamVal(paMap, "Menuid");if(StringUtils.isNotBlank(menuId)) {

sql+=" AND menuId in ("+menuId+")";

}else{

sql+=" AND menuId=001";

}//这里面存放的是数据库中的菜单信息

List> listMap = super.executeQuery(sql, pageBean);returnlistMap;

}/*** {'Menuid':001,'Menuame':'学生管理'}

* {id:..,text:...}

*@parammap

*@paramtreeNode

*@throwsInstantiationException

*@throwsIllegalAccessException

*@throwsSQLException*/

private void MapToTreeNode(Map map,TreeNode treeNode) throwsInstantiationException, IllegalAccessException, SQLException {

treeNode.setId(map.get("Menuid")+"");

treeNode.setText(map.get("Menuname")+"");

treeNode.setAttributes(map);//将子节点添加到父节点当中,建立数据之间的父子关系//treeNode.setChildren(children);

Map childrenMap=new HashMap<>();

childrenMap.put("Menuid", newString[]{treeNode.getId()});

List> listMap = this.listMap(childrenMap, null);

ListlistTreeNode=new ArrayList<>();this.listMapToListTreeNode(listMap, listTreeNode);

treeNode.setChildren(listTreeNode);

}/*** [{'Menuid':001,'Menuame':'学生管理'},{'Menuid':002,'Menuame':'后勤管理'}]

*@paramlistMap

* tree_data1_json

*@paramlistTreeNode

*@throwsSQLException

*@throwsIllegalAccessException

*@throwsInstantiationException*/

private void listMapToListTreeNode (List> listMap,List listTreeNode) throwsInstantiationException, IllegalAccessException, SQLException{

TreeNode treeNode=null;for (Mapmap : listMap) {

treeNode=newTreeNode();

MapToTreeNode(map, treeNode);

listTreeNode.add(treeNode);

}

}

}

2.5新增web的方法

packagecom.yuan.web;importjava.sql.SQLException;importjava.util.List;importjava.util.Map;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.yuan.dao.UserDao;import com.***.framework.ActionSupport;public class UserAction extendsActionSupport {private UserDao userDao = newUserDao();/*** 登录成功后跳转index.jsp

*@paramrequest

*@paramresponse

*@return

*/

publicString login(HttpServletRequest request,HttpServletResponse response) {//系统中是否有当前登录用户

Map map = null;try{

map= this.userDao.list(request.getParameterMap(), null).get(0);

}catch(Exception e) {

request.setAttribute("msg", "用户不存在");return "login";

}try{//有就查询用户菜单中间表,获取对应menuid的集合

if(map!=null&&map.size()>0) {//得到[{Menuid:002,...},{Menuid:003,...}]//截成002,003

StringBuilder sb= newStringBuilder();

List> menuByUid = this.userDao.getMenuByUid(request.getParameterMap(), null);for (Mapm : menuByUid) {

sb.append(","+m.get("menuId"));

}

request.setAttribute("menuIds", sb.substring(1));return "index";

}else{//没有就重新跳回登陆界面,并提示用户不存在

request.setAttribute("msg", "用户不存在");return "login";

}

}catch (InstantiationException | IllegalAccessException |SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();return "login";

}

}

}

2.6新增登入界面,跳入前端树形菜单

Insert title hereuid:upwd:

${msg }

页面比较简单,可自行优化。

2.7修改js代码  index.js

$(function(){

$('#tt').tree({

url:'menuAction.action?methodName=menuTree&&Menuid='+$("#menuIds").val(),

onClick: function(node){//alert(node.text);//在用户点击的时候提示//add a new tab panel

var content = '';if($('#menuTab').tabs('exists',node.text)){//存在则执行选项卡选中已有选项卡的操作

$('#menuTab').tabs('select',node.text);

}else{

$('#menuTab').tabs('add',{//不存在执行新增的操作

title:node.text,

content:content,

closable:true,

});

}

}

});

})

在修改url时需要在原有的index.jsp页面添加一行接受web层传过来的用户登录时的menuid  代码

2.8配置mvc.xml文件

3、运行结果

3.1登录界面

ef1d58226de3def72584c619be184204.png

3.2  001用户登入

4034d2185176237928c3ba87e06a2673.png

3.3  002用户登入

2685a8aed9af00c57e0100f6bb84f8d8.png

3.3  003用户登入

f8addc2c4748dcfb4f56bb60ce41c556.png

3.4  000用户登入

c10bc12ca31cc66fe32505adead3cb92.png

谢谢观看^-^ !!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值