EasyUI(二)之权限划分

1、为什么要写权限,权限的目的是什么?
为了让不同的用户可以操作系统中不同资源,直接点说就是不同的用户可以看到左侧不同的菜单

今天目的:实现用户与菜单之间的关系(用户权限多对多)

具体思路:
1、菜单不同的原因在于,利用不同menuid进行查询,原本默认查询的是所有菜单,是通过-1去查的;
2、menuid由来:是登录用户id查询中间表数据所得来的

再上一篇博客的基础上改动代码:

MenuDao

package com.leiyuanlin.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.leiyuanlin.entity.TreeNode;
import com.leiyuanlin.util.JsonBaseDao;
import com.leiyuanlin.util.JsonUtils;
import com.leiyuanlin.util.PageBean;
import com.leiyuanlin.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.listMapAuth(paMap, pageBean);
		List<TreeNode> listTreeNode = new ArrayList<>();
		this.listMapToListTreeNode(listMap, listTreeNode);
		return listTreeNode;
	}
	
	/**
	 * [{'Menuid':001},{'Menuname':'学生管理'},{{'Menuid':002},{'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=" + menuId;
		}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 menuId = JsonUtils.getParamVal(paMap, "Menuid");
//		为什么将parentid改成menuId?
//		原因在于之前的方法,只能查询当前节点的所有子节点集合,不能将当前节点查询出来
//		002 ---> 002001,002002....
//		002,002001,002002....
		if(StringUtils.isNotBlank(menuId)) {
			sql += " and menuId in ("+menuId+")";
		}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);
		
//		将子节点添加到父节点当中,建立数据之间的父子关系
//		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':002},{'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.leiyuanlin.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.leiyuanlin.util.JsonBaseDao;
import com.leiyuanlin.util.JsonUtils;
import com.leiyuanlin.util.PageBean;
import com.leiyuanlin.util.StringUtils;

public class UserDao extends JsonBaseDao {
	
	/**
	 * 用户登录或者查询用户分页信息的公共方法
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	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);
	}
	
}

UserAction

package com.leiyuanlin.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.fasterxml.jackson.databind.ObjectMapper;
import com.leiyuanlin.dao.UserDao;
import com.leiyuanlin.entity.TreeNode;
import com.leiyuanlin.framework.ActionSupport;
import com.leiyuanlin.util.ResponseUtil;

public class UserAction extends ActionSupport {
	private UserDao userDao = new UserDao();
	
	/**
	 * 登录成功后跳转index.jsp
	 * @param req
	 * @param resp
	 * @return
	 */
	public String login(HttpServletRequest req,HttpServletResponse resp) {
//		系统中是否有当前用户
		try {
			Map<String, Object> map = null;
			try {
				map = this.userDao.list(req.getParameterMap(), null).get(0);
			} catch (Exception e) {
				req.setAttribute("msg", "用户不存在");
				return "login";
			}
//		有
//		查询用户菜单中间表,获取对应的menuid的集合
			if(map != null && map.size() > 0) {
//				[{Menuid:002,...},{Menuid:003}]
				StringBuilder sb = new StringBuilder();
				List<Map<String, Object>> menuIdArr = this.userDao.getMenuByUid(req.getParameterMap(), null);
				for (Map<String, Object> m : menuIdArr) {
//					002,003
					sb.append(","+m.get("menuId"));
				}
				req.setAttribute("menuIds", sb.substring(1));
				return "index";
			}else {
//		没有
				req.setAttribute("msg", "用户不存在");
				return "login";
			}
			
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			e.printStackTrace();
		}
		
		
		return null;
	}
}

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">
<title>后台主界面</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/default/easyui.css">   
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/icon.css">
  
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.min.js"></script>   
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/index.js"></script>  
</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="首页" style="padding:20px;display:none;">   
	       	 欢迎界面    
	    </div>   
</div>
	
	</div>
</body>
</html>

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" value="登录">
</form>
<span style="color: red;">${msg}</span>
</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
				}); 
			}
			
		}

	});
	
})

mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/menuAction" type="com.leiyuanlin.web.MenuAction">
	</action>
	
	<action path="/userAction" type="com.leiyuanlin.web.UserAction">
		<forward name="index" path="/index.jsp" redirect="false"></forward>
		<forward name="login" path="/login.jsp" redirect="false"></forward>
	</action>
</config>

效果图

用户001
在这里插入图片描述
在这里插入图片描述

用户000
在这里插入图片描述
在这里插入图片描述
感谢观看!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EasyUI权限管理系统是一种基于EasyUI框架开发的用于实现权限管理功能的系统。该系统主要用于管理用户对系统中各项功能和资源的访问权限,并通过EasyUI框架提供的丰富的界面组件和交互效果,使用户能够方便地进行权限设置和管理。 首先,EasyUI权限管理系统提供了灵活的用户管理功能。管理员可以在系统中添加用户,并为每个用户分配不同的权限。通过角色的方式,可以对用户进行分组,方便进行权限的管理和设置。 其次,系统提供了细粒度的权限控制。管理员可以根据实际需求,对系统中的各项功能和资源进行逐个设置权限,并为用户分配相应的角色和权限。这样,就可以确保每个用户只能访问其被授权的功能和资源,提高了系统的安全性和数据的保密性。 此外,EasyUI权限管理系统还提供了直观的界面展示和操作方式。通过EasyUI框架提供的丰富的界面组件和交互效果,管理员可以轻松地进行权限的设置和管理。同时,用户也可以方便地查看自己的权限和角色信息,确保对系统的访问和操作符合其权限。 最后,EasyUI权限管理系统具备良好的扩展性和可维护性。系统的设计和开发采用了模块化的结构,方便后期的功能扩展和维护。同时,系统采用了可配置的方式进行权限的管理和设置,使得系统的管理更具灵活性和可扩展性。 综上所述,EasyUI权限管理系统是一种功能强大、界面友好、操作简便的系统,旨在提供便捷的权限管理功能,帮助用户实现对系统中各项功能和资源的精细化控制和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值