easyui(控件权限树)
二星权限设计
思路:
-
菜单不同的原因在于,利用不同menuid进行查询,原本默认查询的是所有菜单,是通过-1去查的;
-
menuid由来:是登录用户id查询中间表数据所得来的
今天类容是在 https://blog.csdn.net/wx1762813417/article/details/97619466 这篇博客上增加一些登入权限
MenuDao
与上一篇博客比MenuDao相比 ,如果要加权限需要改变的方法和sql语句。
这个方法的意思是:
获取前台传来的访问权限值,如果值不为空的化就拼接sql语句查询出数据库中包含的权限,为空就默认得到一个权限,在 t_easyui_menu 数据库中得到获取权限值,然后调用父类的方法,把数据查询出来。
package com.hyf.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.hyf.entity.TreeNode;
import com.hyf.util.JsonBaseDao;
import com.hyf.util.JsonUtils;
import com.hyf.util.PageBean;
import com.hyf.util.StringUtils;
public class MenuDao extends JsonBaseDao {
/**
* List<TreeNode> 加上objectMapper 可以转换成easyui的tree控件识别的json
*
* @param map
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<TreeNode> listTreeNode(Map<String, String[]> map, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException {
List<Map<String, Object>> listMenu = this.listMenuAuth(map, pageBean);
List<TreeNode> listTreeNode = new ArrayList<TreeNode>();
this.listMapToListTreeNode(listMenu, listTreeNode);
return listTreeNode;
}
/**
* 按照不同的用户访问不同的菜单
* @param map
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> listMenuAuth(Map<String, String[]> map, PageBean pageBean)
throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select * from t_easyui_menu where true ";
String id = JsonUtils.getParamVal(map, "Menuid");
System.out.println("--------"+id);
if (StringUtils.isNotBlank(id)) {
sql +=" and menuid in ("+id+")";
} else {
sql += "and menuid=000";
}
return super.executeQuery(sql, pageBean);
}
public void mapToTreeNode(Map<String,Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
treeNode.setId(map.get("Menuid").toString());
treeNode.setText(map.get("Menuname").toString());
treeNode.setAttributes(map);
Map<String, String[]> childMap = new HashMap<String, String[]>();
childMap.put("Menuid",new String [] {treeNode.getId()});
// 查询当前节点所拥有的子节点集合
List<Map<String, Object>> listMenu = this.listMenu(childMap, null);
List<TreeNode> listTreeNode = new ArrayList<TreeNode>();
this.listMapToListTreeNode(listMenu, listTreeNode);
treeNode.setChildren(listTreeNode);
}
public void listMapToListTreeNode(List<Map<String, Object>> list,List<TreeNode> listTreeNode) throws InstantiationException, IllegalAccessException, SQLException {
TreeNode treeNode = null;
for (Map<String, Object> map : list) {
treeNode = new TreeNode();
this.mapToTreeNode(map, treeNode);
listTreeNode.add(treeNode);
}
}
}
UserDao dao方法
list() 方法意思:
获取到页面传来的用户账号(uid)和密码(upwd),然后所用工具类(StringUtils)调用方法判断 uid 和 upwe 不等与 null 和 “” ,如果不为空就可以拼接字符串查询数据,得到查询数据在 UserAction 类进行处理。当查询数据为空 或者数据有多条时,就会进行处理,满足条件就跳到index.jsp 页面中,反之 重新登入。
getMenusByUser()方法意思:
通过用户登入的唯一账号,在用户权限中中间表中获取菜单ID的集合
package com.hyf.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.hyf.util.JsonBaseDao;
import com.hyf.util.JsonUtils;
import com.hyf.util.PageBean;
import com.hyf.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>> getMenusByUser(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);
}
}
UserAction
类方法意思:
得到用户权限数据,如果有多个权限 ,就用 StringBuilder 拼接。然后把值保存,转发到index.jsp页面中。如果不满足登录条件就 返回 到logIn.jsp页面中。
package com.hyf.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.hyf.dao.UserDao;
import com.zking.framework.ActionSupport;
public class UserAction 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);
StringBuilder sb = new StringBuilder();
if(list !=null && list.size()==1) {
List<Map<String, Object>> menusList = this.userDao.getMenusByUser(req.getParameterMap(), null);
for (Map<String, Object> map : menusList) {
sb.append(","+map.get("menuId"));
}
req.setAttribute("menuIds", sb.substring(1));
}else {
req.setAttribute("msg", "用户不存在");
code = "login";
}
} catch (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
code = "login";
}
return code;
}
}
配置 mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/menuAction" type="com.hyf.web.MenuAction">
</action>
<action path="/userAction" type="com.hyf.web.UserAction">
<forward name="index" path="/index.jsp" redirect="false" />
<forward name="login" path="/login.jsp" redirect="false"></forward>
</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/icon.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/static/easyui5/themes/black/easyui.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>Insert title here</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="height:800px;">
<div title="首页" style="padding:20px;display:none;">
默认展示
</div>
</div>
</div>
</body>
</html>
index.js
通过隐藏域把权限值带入js 中
主要!!!!!!!
通过Id 获取value 中的权限值
$(function(){
$('#tt').tree({
url:'menuAction.action?methodName=menuTree&&Menuid='+$("#menuIds").val(),
onClick: function (node){
//alert(node.attributes.menuURL); //在用户点击时候提醒
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>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>
<button type="submit">登入</button>
</form>
</body>
</html>
网页输出:两个用户比较