easyui高级控件之权限
在easyui入門篇基础进行权限管理
权限目的:
是为了让不同的用户可以操作系统中不同资源(直接点说就是不同的用户可以看到左侧不同的菜单)
思考:
我们想一个用户对应多个菜单
然后一个菜单可以对应多个用户
其实这就是user与menu的多对多的关系
思路:
1、菜单不同的原因在于,利用不同menuid进行查询,原本默认查询的是所有菜单,是通过-1去查的
2、menuid由来:是登录用户id查询中间表数据所得来的
权限树:
1,一星权限设计(用户权限多对一)
1.1执行数据库脚本
1.2建立实体类
1.3创建dao
1.4Web层创建
1.5更改展示的树形菜单
上图:
权限树:
2,二星权限设计(用户权限多对多)
2.1执行数据库脚本
2.2修改原有的实体类
2.3建立实体类
2.4创建dao
2.5修改原有的dao
2.6新增web的方法
2.7新增登入界面,跳入前端树形菜单
上图:
dao层:
MenuDao:
package com.xhh.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.xhh.entity.TreeNode;
import com.xhh.util.JsonBaseDao;
import com.xhh.util.JsonUtils;
import com.xhh.util.PageBean;
import com.xhh.util.StringUtils;
public class MenuDao extends JsonBaseDao {
/**
* 给前台返回tree_data1.json的字符串
*
* @param paMap 从前台jsp传递过来的参数集合
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<TreeNode> listTreeNode(Map<String, String[]> paMap, PageBean pageBean)
throws InstantiationException, IllegalAccessException, SQLException {
List<Map<String, Object>> listMap = this.listMap(paMap, pageBean);
List<TreeNode> listTreeNode = new ArrayList<>();
this.listMapToListTreeNode(listMap, listTreeNode);
return listTreeNode;
}
/**
* [{'Menuid':001,'Menuname':‘学生管理’},{{'Menuid':001,'Menuname':‘后勤管理’}}]
*
* @param paMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> listMap(Map<String, String[]> paMap, PageBean pageBean)
throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select * from t_easyui_menu where true";
String menuId = JsonUtils.getParamVal(paMap, "Menuid");
if (StringUtils.isNotBlank(menuId)) {
sql += " and parentid=" + sql;
} else {
sql += " and parentid=-1";
}
// 这里面存放的是数据库中菜单信息
List<Map<String, Object>> listMap = super.executeQuery(sql, pageBean);
return listMap;
}
public List<Map<String, Object>> listMapAuth(Map<String, String[]> paMap, PageBean pageBean)
throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select * from t_easyui_menu where true";
String id = JsonUtils.getParamVal(paMap, "Menuid");
if (StringUtils.isNotBlank(id)) {
sql += " and menuid in ("+id+") ";
} else {
sql += " and menuid=000";
}
// 这里面存放的是数据库中菜单信息
List<Map<String, Object>> listMap = super.executeQuery(sql, pageBean);
return listMap;
}
/**
* {'Menuid':001,'Menuname':‘学生管理’} --> {id:...,text:...}
*
* @param map
* @param treeNode
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void mapToTreeNode(Map<String, Object> map, TreeNode treeNode)throws InstantiationException, IllegalAccessException, SQLException {
treeNode.setId(map.get("Menuid") + "");
treeNode.setText(map.get("Menuname") + "");
treeNode.setAttributes(map);
;
// 将子节点添加到父节点当中,建立数据之间的父子关系 001
// treeNode.setChildren(children);
Map<String, String[]> childrenMap = new HashMap<>();
childrenMap.put("Menuid", new String[] { treeNode.getId() });
List<Map<String, Object>> listMap = this.listMap(childrenMap, null);
List<TreeNode> listTreeNode = new ArrayList<>();
this.listMapToListTreeNode(listMap, listTreeNode);
treeNode.setChildren(listTreeNode);
}
/**
* [{'Menuid':001,'Menuname':‘学生管理’},{{'Menuid':001,'Menuname':‘后勤管理’}}] -->
* tree_data1.json
*
* @param listMap
* @param listTreeNode
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void listMapToListTreeNode(List<Map<String, Object>> listMap, List<TreeNode> listTreeNode)
throws InstantiationException, IllegalAccessException, SQLException {
TreeNode treeNode = null;
for (Map<String, Object> map : listMap) {
treeNode = new TreeNode();
mapToTreeNode(map, treeNode);
listTreeNode.add(treeNode);
}
}
}
UserDao:
package com.xhh.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.xhh.util.JsonBaseDao;
import com.xhh.util.JsonUtils;
import com.xhh.util.PageBean;
import com.xhh.util.StringUtils;
public class UserDao extends JsonBaseDao{
/**
*
* 用于查詢用戶分頁列表所用
* 用於用戶登錄
* @param map
* @param pagebean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<Map<String, Object>> list(Map<String, String[]>map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql ="select * from t_easyui_user_version2 where true";
String uid=JsonUtils.getParamVal(map, "uid");
String upwd=JsonUtils.getParamVal(map, "upwd");
if(StringUtils.isNotBlank(uid)) {
sql +=" and uid ="+uid;
}
if(StringUtils.isNotBlank(upwd)) {
sql +=" and upwd ="+upwd;
}
return super.executeQuery(sql, pageBean);
}
/**
* 通過用戶登錄的唯一賬,在用戶權限中間獲取菜單Id集合
* @param map
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> getMenuByuser(Map<String, String[]>map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql ="select * from t_easyui_usermenu where true";
String uid=JsonUtils.getParamVal(map, "uid");
if(StringUtils.isNotBlank(uid)) {
sql +=" and uid ="+uid;
}
return super.executeQuery(sql, pageBean);
}
}
web层:
MenuAction:
package com.xhh.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xhh.dao.MenuDao;
import com.xhh.entity.TreeNode;
import com.xhh.util.ResponseUtil;
import com.xhh.framework.ActionSupport;
public class MenuAction extends ActionSupport{
private MenuDao menudao= new MenuDao();
public String menutree(HttpServletRequest req,HttpServletResponse resp) {
ObjectMapper om=new ObjectMapper();
try {
List<TreeNode> list = this.menudao.listTreeNode(req.getParameterMap(), null);
ResponseUtil.write(resp, om.writeValueAsString(list));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
}
UsetAction:
package com.xhh.web;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xhh.dao.UserDao;
import com.xhh.framework.ActionSupport;
public class UsetAction extends ActionSupport {
private UserDao userDao=new UserDao();
public String login(HttpServletRequest req, HttpServletResponse resp) {
String code = "index";
// 登錄
try {
List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), null);
if(list !=null && list.size()==1) {
// 用戶存在
List<Map<String, Object>> menulist = this.userDao.getMenuByuser(req.getParameterMap(), null);
StringBuilder sb =new StringBuilder();
for (Map<String, Object> map : menulist) {
sb.append(","+map.get("menuid"));
}
req.setAttribute("menuIds", sb.substring(1));
}else {
// 用不存在
req.setAttribute("msg", "用戶不存在");
code ="login";
}
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
code ="login";
}
return code;
}
}
xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!-- <action path="/cal_add" type="com.xhh.web.AddCalAction"> <forward name="rs"
path="/rs.jsp" redirect="false" /> </action> -->
<action path="/StudentAction" type="com.xhh.web.StudentAction">
<forward name="index" path="/index.jsp" redirect="false" />
<forward name="add" path="/StudentAction.action?method=getAll"
redirect="false" />
<forward name="update" path="/update.jsp" redirect="false" />
</action>
<action path="/menuAction" type="com.xhh.web.MenuAction">
</action>
<action path="/userAction" type="com.xhh.web.UsetAction">
<forward name="index" path="/index.jsp" redirect="false" />
<forward name="login" path="/login.jsp" redirect="false" />
</action>
</config>
index.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/index.js"></script>
<title>后台管理界面</title>
</head>
<body class="easyui-layout">
<input type="hidden" id="menuIds" value="${menuIds}">
<div data-options="region:'north',border:false"
style="height: 60px; background: #B3DFDA; padding: 10px">north
region</div>
<div data-options="region:'west',split:true,title:'West'"
style="width: 150px; padding: 10px;">
菜单管理
<ul id="tt"></ul>
</div>
<div
data-options="region:'east',split:true,collapsed:true,title:'East'"
style="width: 100px; padding: 10px;">east region</div>
<div data-options="region:'south',border:false"
style="height: 50px; background: #A9FACD; padding: 10px;">south
region</div>
<div data-options="region:'center',title:'Center'">
<div id="menuTab" class="easyui-tabs" style="">
<div title="首页" data-options="iconCls:'icon-reload',closable:true"
style="padding: 20px; display: none;">欢迎界面</div>
</div>
</div>
</body>
</html>
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 = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>';
if($('#menuTab').tabs('exists',node.text)){
//存在执行选项卡选中已有选项卡的操作
$('#menuTab').tabs('select',node.text);
}else{
//不存在执行新增的操作
$('#menuTab').tabs('add',{
title:node.text,
content:content,
closable:true,
});
}
}
});
})
login.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用戶登錄界面</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/userAction.action?methodName=login" method="post">
uid:<input type="text" name="uid"><br>
upwd:<input type="text" name="upwd"><br>
<input type="submit" value="ok">
</form>
</body>
</html>
测试结果:
登录002: