EasyUI权限
结合EasyUI入门,今天来给EasyUI写一个简单的权限
权限的目的:让不同的用户可以操作系统中不同资源
UserDao
- 用户登录或者查询用户分页的公共方法
- 根据当前用户登录的id去查对应的菜单
1 package com.easyui.dao; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 import java.util.Map; 6 7 import com.easyui.util.JsonBaseDao; 8 import com.easyui.util.JsonUtils; 9 import com.easyui.util.PageBean; 10 import com.easyui.util.StringUtils; 11 12 public class UserDao extends JsonBaseDao{ 13 /** 14 * 用户登录或者查询用户分页的公共方法 15 * @param paMap 16 * @param pageBean 17 * @return 18 * @throws InstantiationException 19 * @throws IllegalAccessException 20 * @throws SQLException 21 */ 22 public List<Map<String, Object>> list(Map<String, String[]> paMap,PageBean pageBean ) throws InstantiationException, IllegalAccessException, SQLException{ 23 String sql="select * from t_easyui_user_version2 where true"; 24 String uid=JsonUtils.getParamVal(paMap, "uid"); 25 String upwd=JsonUtils.getParamVal(paMap, "upwd"); 26 if(StringUtils.isNotBlank(uid)) { 27 sql+=" and uid="+uid; 28 } 29 if(StringUtils.isNotBlank(upwd)) { 30 sql+=" and upwd="+upwd; 31 } 32 return super.executeQuery(sql, pageBean); 33 } 34 35 36 /** 37 * 根据当前用户登录的id去查对应的菜单 38 * @param paMap 39 * @param pageBean 40 * @return 41 * @throws InstantiationException 42 * @throws IllegalAccessException 43 * @throws SQLException 44 */ 45 public List<Map<String, Object>> getMenuByUid(Map<String, String[]> paMap,PageBean pageBean ) throws InstantiationException, IllegalAccessException, SQLException{ 46 String sql="select * from t_easyui_usermenu where true"; 47 String uid=JsonUtils.getParamVal(paMap, "uid"); 48 if(StringUtils.isNotBlank(uid)) { 49 sql+=" and uid="+uid; 50 } 51 52 return super.executeQuery(sql, pageBean); 53 } 54 55 56 }
UserAction
- 登录成功后跳转index.jsp
- 系统中是否有当前登录用户
- 查询用户菜单中间表,获取对应menuid的集合
1 package com.easyui.web; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import com.fasterxml.jackson.databind.ObjectMapper; 10 import com.easyui.dao.UserDao; 11 import com.easyui.entity.TreeNode; 12 import com.easyui.util.ResponseUtil; 13 import com.zking.framework.ActionSupport; 14 15 public class UserAction extends ActionSupport{ 16 17 private UserDao userDao=new UserDao(); 18 /** 19 * 登录成功后跳转index.jsp 20 * @param request 21 * @param response 22 * @return 23 * @throws Exception 24 */ 25 public String login(HttpServletRequest request,HttpServletResponse response) throws Exception { 26 //系统中是否有当前登录用户 27 Map<String, Object> map = this.userDao.list(request.getParameterMap(), null).get(0); 28 29 // 有 30 //查询用户菜单中间表,获取对应menuid的集合 31 if(map!=null && map.size()>0) { 32 //[{Menuid:002,map...},{Menuid:003..}] 33 //[002,003] 34 StringBuilder sb=new StringBuilder(); 35 List<Map<String, Object>> menuIdArr = this.userDao.getMenuByUid(request.getParameterMap(), null); 36 System.out.println(menuIdArr); 37 for (Map<String, Object> m : menuIdArr) { 38 sb.append(","+m.get("menuId")); 39 //,002,003 40 } 41 request.setAttribute("menuIds", sb.substring(1)); 42 43 return "index"; 44 }else { 45 //没有 46 request.setAttribute("msg", "用户不存在"); 47 // 返回登录界面 48 return "login"; 49 } 50 51 } 52 }
MenuDao
1 package com.easyui.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.easyui.entity.TreeNode; 10 import com.easyui.util.JsonBaseDao; 11 import com.easyui.util.JsonUtils; 12 import com.easyui.util.PageBean; 13 import com.easyui.util.StringUtils; 14 15 public class MenuDao extends JsonBaseDao { 16 /** 17 * 给前台tree_data1_json的字符串 18 * @param paMap 从前台jsp传递过来的参数集合 19 * @param pageBean 20 * @return 21 * @throws SQLException 22 * @throws IllegalAccessException 23 * @throws InstantiationException 24 */ 25 public List<TreeNode> listTreeNode(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{ 26 List<Map<String, Object>> listMap = this.listMapAuth(paMap, pageBean); 27 List<TreeNode> listTreeNode=new ArrayList<TreeNode>(); 28 this.listMapToListTreeNode(listMap, listTreeNode); 29 return listTreeNode; 30 } 31 32 public List<Map<String, Object>> listMap(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{ 33 String sql="select * from t_easyui_menu where true"; 34 String menuId=JsonUtils.getParamVal(paMap, "Menuid"); 35 36 37 if(StringUtils.isNotBlank(menuId)) { 38 sql+=" and parentid="+menuId; 39 } 40 else { 41 sql+=" and parentid=-1"; 42 } 43 //这里面存放的是数据库中的菜单信息 44 List<Map<String, Object>> listMap = super.executeQuery(sql, pageBean); 45 return listMap; 46 } 47 48 49 public List<Map<String, Object>> listMapAuth(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{ 50 String sql="select * from t_easyui_menu where true"; 51 String menuId=JsonUtils.getParamVal(paMap, "Menuid"); 52 //为什么将parentid改成menid?、 53 //原因在之前的方法,只能查询当前节点的所有子集合,不能将当前节点给查出来 54 //002-->002001,002002,002003 55 if(StringUtils.isNotBlank(menuId)) { 56 sql+=" and Menuid in ("+menuId+")"; 57 } 58 else { 59 sql+=" and Menuid=000 "; 60 } 61 //这里面存放的是数据库中的菜单信息 62 List<Map<String, Object>> listMap = super.executeQuery(sql, pageBean); 63 return listMap; 64 } 65 /** 66 * {'Menuid':001,'Menuame':'学生管理'} 67 * --> 68 * {id:..,text:...} 69 * @param map 70 * @param treeNode 71 * @throws SQLException 72 * @throws IllegalAccessException 73 * @throws InstantiationException 74 */ 75 private void MapToTreeNode(Map<String, Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException { 76 treeNode.setId(map.get("Menuid")+""); 77 treeNode.setText(map.get("Menuname")+""); 78 treeNode.setAttributes(map); 79 80 //将子节点添加到父节点当中,建立数据之间的父子关系 81 //treeNode.setChildren(children); 82 Map<String, String[]> childrenMap=new HashMap<>(); 83 childrenMap.put("Menuid", new String[]{treeNode.getId()}); 84 List<Map<String, Object>> listMap = this.listMap(childrenMap, null); 85 List<TreeNode>listTreeNode=new ArrayList<>(); 86 this.listMapToListTreeNode(listMap, listTreeNode); 87 treeNode.setChildren(listTreeNode); 88 } 89 /** 90 * [{'Menuid':001,'Menuame':'学生管理'},{'Menuid':002,'Menuame':'后勤管理'}] 91 * @param listMap 92 * tree_data1_json 93 * @param listTreeNode 94 * @throws SQLException 95 * @throws IllegalAccessException 96 * @throws InstantiationException 97 */ 98 private void listMapToListTreeNode (List<Map<String, Object>> listMap,List<TreeNode> listTreeNode) throws InstantiationException, IllegalAccessException, SQLException{ 99 TreeNode treeNode=null; 100 for (Map<String, Object> map : listMap) { 101 treeNode=new TreeNode(); 102 MapToTreeNode(map, treeNode); 103 listTreeNode.add(treeNode); 104 } 105 } 106 }
配置xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <config> 3 <!-- <action path="/regAction" type="test.RegAction"> 4 <forward name="failed" path="/reg.jsp" redirect="false" /> 5 <forward name="success" path="/login.jsp" redirect="true" /> 6 </action> --> 7 8 <action path="/menuAction" type="com.hmc.web.MenuAction"> 9 </action> 10 <action path="/userAction" type="com.easyui.web.UserAction"> 11 <forward name="index" path="/index.jsp" redirect="false" /> 12 <forward name="login" path="/login.jsp" redirect="false" /> 13 14 </action> 15 </config>
登录界面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="${pageContext.request.contextPath}/userAction.action?methodName=login" method="post"> 11 uid:<input type="text" name="uid"><br> 12 upwd:<input type="text" name="uname"><br> 13 <input type="submit"> 14 </form> 15 <span style="color:red">${msg}</span> 16 </body> 17 </html>
index.js
1 $(function(){ 2 $('#tt').tree({ 3 url:'menuAction.action?methodName=menuTree&&Menuid='+$("#menuIds").val(), 4 onClick:function(node){ 5 6 var context='<iframe scrolling="no" frameborde="0" src="'+node.attributes.menuURL+'" width="100%" height="100%"></frame>' 7 if($('#menuTab').tabs('exists',node.text)){ 8 $('#menuTab').tabs('select',node.text); 9 }else{ 10 $('#menuTab').tabs('add',{ 11 title:node.text, 12 content:context, 13 closable:true, 14 15 }); 16 } 17 } 18 }); 19 20 })
效果图