EasyUi 权限
思考:
我们想一个用户对应多个菜单
然后一个菜单可以对应多个用户
其实这就是user与menu的多对多的关系
思路:
1、菜单不同的原因在于,利用不同menuid进行查询,原本默认查询的是所有菜单,是通过-1去查的;
2、menuid由来:是登录用户id查询中间表数据所得来的
2、二星权限设计(用户权限多对多)
执行数据库脚本
修改原有的实体类
建立实体类
创建dao
修改原有的dao
新增web的方法
新增登入界面,跳入前端树形菜单
1、一星权限设计(用户权限多对一)
1.1、执行数据库脚本
1.2、建立实体类
1.3、创建dao
1.4、Web层创建
1.5、更改展示的树形菜单
2、二星权限设计(用户权限多对多)
2.1、执行数据库脚本
2.2、修改原有的实体类
2.3、建立实体类
2.4、创建dao
2.5、修改原有的dao
2.6、新增web的方法
演示代码
UserDao:
package com.pyc.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.pyc.util.JsonBaseDao;
import com.pyc.util.JsonUtils;
import com.pyc.util.PageBean;
import com.pyc.util.StringUtils;
public class UserDao extends JsonBaseDao {
public List<Map<String, Object>> list(Map<String,String[]> paMap,PageBean pageBean) throws InstantiationException, 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去查询对应的所有菜单
* @param paMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> getMenuByUid(Map<String,String[]> paMap,PageBean pageBean) throws InstantiationException, 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);
}
}
Web文件:
UserAction.Java::
package com.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.dao.UserDao;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zking.mvc.framework.ActionSupport;
public class userAction extends ActionSupport {
private UserDao userDao=new UserDao();
/**
* 登录成功后调转index.jsp
* @param req
* @param resq
* @return
*/
public String login(HttpServletRequest req,HttpServletResponse resq) {
Map<String, Object> map;
try {
map = this.userDao.list(req.getParameterMap(), null).get(0);
} catch (Exception e) {
// TODO Auto-generated catch block
req.setAttribute("msg", "用户不存在");
return "login";
}
//系统中是有有当前用户登录
try {
//有的话
if(map!=null && map.size()>0) {
//[{Menid:002....},{Menuid:003,.....}]
//但是必须得到数值 像 002,003;
StringBuilder sb=new StringBuilder();
List<Map<String, Object>> menuIdArr=this.userDao.getMenuByUid(req.getParameterMap(), null);
for (Map<String, Object> m : menuIdArr) {
sb.append(","+m.get("menuId"));
//002,003
}
req.setAttribute("menuIds", sb.substring(1));
System.out.println("获取menuid"+sb.substring(1));
return "index";
}else {
req.setAttribute("msg", "用户不存在");
return "login";
}
//查询用户菜单中间表,获取对应menuid的集合
//没有
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "login";
}
}
}
Conf文件:
Mvc.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!-- <action path="/regAction" type="test.RegAction">
<forward name="failed" path="/reg.jsp" redirect="false" />
<forward name="success" path="/login.jsp" redirect="true" />
</action> -->
<action path="/menuAction" type="com.web.MenuAction">
</action>
<action path="/userAction" type="com.web.userAction">
<forward name="index" path="/index.jsp" redirect="false" />
<forward name="login" path="/login.jsp" redirect="false" />
</action>
</config>
index.js
$(function(){
$('#tt').tree({
url:'menuAction.action?methodName=menuTree&&Menuid=+$("#menuIds").val(),
onClick: function(node){
var content = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>';
//alert(node.attributes.menuURL); // 在用户点击的时候提示
if($("#menuTab").tabs('exists',node.text)){
$("#menuTab").tabs('select',node.text)
//alert("不存在");
}else{
//alert("存在");
$('#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>Insert title here</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" >
</form>
<span style="color: red;">${msg}</span>
</body>
</html>
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">
<!-- ctrl+shift+r查找本工作区间打开的工程中的文件 -->
<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>
<!-- ctrl+shift+f 格式化-->
<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="height: 800px;">
<div title="首页" style="padding:20px;display:none;">
默认首页展示内容
</div>
</div>
</div>
</body>
</html>
权限等级展示
001:
002:
003: