在easyui上实现权限的管理
所谓权限:指的是系统中的资源,资源包括菜单资源(学习情况报表,账号审核...)以及按钮资源
所谓角色:指的是系统中的权限集合(每一个角色对应着哪些权限集合)
1、一星权限设计(用户权限多对一)
?执行数据库脚本sql
?建立实体类
?创建dao方法
?Webservlet层创建
?展示对应的树形菜单
实现思路:通过账号和密码查询 是为了获取菜单的id 获取到里面的menuid后 可加载对应的菜单或以及子菜单
弊端:一个菜单不能对应多个用户!
2、二星权限设计(用户权限多对多)
?执行数据库脚本
?修改原有的实体类
?建立实体类
?创建dao
?修改原有的dao
?新增web的方法
?新增登入界面,跳入前端树形菜单
实现思路:用户查询登陆表 有数据代表已注册 通过uid 查到中间表(一个uid查中间表的方法) 获取到一个权限集合 对应多个数据 再进行遍历uid查到的集合 然后拼接每一个map集合的menuid(这个menuid的集合就是那些数字 方法用in可得到多组父子关系)
首先我们先建立三张表
1用户表(t_easyui_user_version2)
2.用户菜单中间表(t_easyui_usermenu)
3.菜单表(t_easyui_menu)
写一个登录界面
1 <body> 2 <form action="${pageContext.request.contextPath }/userAction.action?methodName=login" method="post"> 3 uid:<input type="text" name="uid"><br> 4 upwd:<input type="text" name="upwd"><br> 5 <input type="submit" ><br> 6 </form> 7 </body>
写一个dao方法来验证登录
1 package com.liuwenwu.dao; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 import java.util.Map; 6 7 import com.liuwenwu.util.JsonBaseDao; 8 import com.liuwenwu.util.JsonUtils; 9 import com.liuwenwu.util.PageBean; 10 import com.liuwenwu.util.StringUtils; 11 12 public class UserDao extends JsonBaseDao { 13 14 /** 15 * 登录 查询用户表 16 * @return 17 * @throws SQLException 18 * @throws IllegalAccessException 19 * @throws InstantiationException 20 */ 21 public List<Map<String, Object>> list(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{ 22 String sql="select * from t_easyui_user_version2 where true"; 23 String uid=JsonUtils.getParamVal(paMap, "uid"); 24 String upwd=JsonUtils.getParamVal(paMap, "upwd"); 25 if(StringUtils.isNotBlank(uid)) { 26 sql=sql+" and uid="+uid; 27 } 28 if(StringUtils.isNotBlank(upwd)) { 29 sql=sql+" and upwd="+upwd; 30 } 31 return super.executeQuery(sql, pageBean); 32 }
写一个dao方法根据用户ID来查询用户对应的权限
1 /** 2 * 通过中间表查询登录用户所对应的权限 3 * @param paMap 4 * @param pageBean 5 * @return 6 * @throws InstantiationException 7 * @throws IllegalAccessException 8 * @throws SQLException 9 */ 10 public List<Map<String, Object>> listMenu(String uid,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{ 11 String sql="select * from t_easyui_usermenu where true"; 12 if(StringUtils.isNotBlank(uid)) { 13 sql=sql+" and uid="+uid; 14 } 15 return super.executeQuery(sql, pageBean); 16 }
写一个web层自控制器来调用dao方法
1 package com.liuwenwu.web; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 import java.util.Map; 6 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import com.liuwenwu.dao.UserDao; 11 import com.zking.framework.ActionSupport; 12 13 public class UserAction extends ActionSupport { 14 15 private UserDao userDao=new UserDao(); 16 17 public String login(HttpServletRequest req,HttpServletResponse resp){ 18 try { 19 List<Map<String, Object>> list=this.userDao.list(req.getParameterMap(), null); 20 if(list!=null&&list.size()>0) { 21 List<Map<String, Object>> listMenu=this.userDao.listMenu(req.getParameter("uid"), null); 22 StringBuilder sb=new StringBuilder(); 23 for (Map<String, Object> map : listMenu) { 24 sb.append(","+map.get("menuId")); 25 } 26 // ,001,002 27 req.setAttribute("menuHid", sb.substring(1)); 28 }else { 29 return"login"; 30 } 31 } catch (Exception e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace(); 34 } 35 36 return "index"; 37 } 38 39 }
配置MVC文件
1 <action path="/menuAction" type="com.liuwenwu.web.MenuAction"> 2 <forward name="index" path="/index.jsp" redirect="false" /> 3 </action> 4 5 <action path="/userAction" type="com.liuwenwu.web.UserAction"> 6 <forward name="index" path="/index.jsp" redirect="false" /> 7 </action>
在昨天的tree结构展示dao里面加一个方法
1 package com.liuwenwu.dao; 2 3 import java.sql.SQLException; 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import com.liuwenwu.entity.TreeNode; 10 import com.liuwenwu.util.JsonBaseDao; 11 import com.liuwenwu.util.JsonUtils; 12 import com.liuwenwu.util.PageBean; 13 import com.liuwenwu.util.StringUtils; 14 15 public class MenuDao extends JsonBaseDao { 16 17 /** 18 * @param map req.getParameterMap 19 * @param pageBean 分页 20 * @return 21 * @throws Exception 22 */ 23 public List<TreeNode> list(Map<String, String[]> map,PageBean pageBean) throws Exception{ 24 List<Map<String, Object>> listMenu=this.listMenuSef(map, pageBean); 25 List<TreeNode> treeNodeList=new ArrayList<>(); 26 menuList2TreeNodeList(listMenu, treeNodeList); 27 return treeNodeList; 28 } 29 30 public List<Map<String, Object>> listMenuSef(Map<String, String[]> map,PageBean pageBean)throws Exception{ 31 String sql=" select * from t_easyui_menu where true"; 32 String id=JsonUtils.getParamVal(map, "menuHid"); 33 if(StringUtils.isNotBlank(id)) { 34 sql=sql +" and menuid in("+id+")"; 35 } 36 else { 37 sql=sql+" and menuid =-1"; 38 } 39 40 return super.executeQuery(sql, pageBean); 41 42 } 43 44 /** 45 * 查询Menu表的数据 46 * @param map 47 * @param pageBean 48 * @return 49 */ 50 public List<Map<String, Object>> listMenu(Map<String, String[]> map,PageBean pageBean)throws Exception{ 51 String sql=" select * from t_easyui_menu where true"; 52 String id=JsonUtils.getParamVal(map, "id"); 53 if(StringUtils.isNotBlank(id)) { 54 sql=sql +" and parentid ="+id; 55 } 56 else { 57 sql=sql+" and parentid =-1"; 58 } 59 60 return super.executeQuery(sql, pageBean); 61 } 62 63 /** 64 * {Menuid:1,....[]} 65 * ->{id:1,....[]} 66 * menu表的数据不符合easyui树形展示的数据格式 67 * 需要转换成easyui所能识别的数据格式 68 * @param map 69 * @param reTreeNode 70 * @throws Exception 71 */ 72 private void menu2TreeNode(Map<String, Object> map, TreeNode treeNode) throws Exception { 73 treeNode.setId(map.get("Menuid").toString()); 74 treeNode.setText(map.get("Menuname").toString()); 75 treeNode.setAttributes(map); 76 77 Map<String, String[]> jspMap=new HashMap<>(); 78 jspMap.put("id", new String[] {treeNode.getId()}); 79 this.listMenu(jspMap, null); 80 List<Map<String, Object>> listMenu=this.listMenu(jspMap, null); 81 List<TreeNode> treeNodeList=new ArrayList<>(); 82 menuList2TreeNodeList(listMenu, treeNodeList); 83 treeNode.setChildren(treeNodeList); 84 } 85 86 87 /** 88 * [{Menuid:1,....[]},{Menuid:2,....[]}] 89 * ->[{id:1,....[]},{id:2,....[]}] 90 * @param mapList 91 * @param treeNodeList 92 * @throws Exception 93 */ 94 private void menuList2TreeNodeList(List<Map<String, Object>> mapList, List<TreeNode> treeNodeList)throws Exception { 95 TreeNode treeNode =null; 96 for (Map<String, Object> map : mapList) { 97 treeNode =new TreeNode(); 98 menu2TreeNode(map, treeNode); 99 treeNodeList.add(treeNode); 100 } 101 } 102 103 }
实现效果:001用户可查看 学生管理
002用户可查看 后勤管理和房屋租金