easyui权限

在上一篇博客中介绍了easyui入门
接下来介绍easyui权限

需求

因为每个人的权限不同,所以能够使用的功能肯定不一样
例:
在这里插入图片描述
权限目的:
是为了让不同的用户可以操作系统中不同资源
直接点说就是不同的用户可以看到左侧不同的菜单

实现菜单权限的核心思想就是控制用户登录后台所传递的menuId

权限树

1、一星权限设计(用户权限多对一)
?执行数据库脚本
?建立实体类
?创建dao
?Web层创建
?更改展示的树形菜单

2、二星权限设计(用户权限多对多)
?执行数据库脚本
?修改原有的实体类
?建立实体类
?创建dao
?修改原有的dao
?新增web的方法
?新增登入界面,跳入前端树形菜单

实现二星权限设计(用户权限多对多)

数据库三张表
t_easyui_user_version2
在这里插入图片描述
t_easyui_usermenu
在这里插入图片描述
t_easyui_menu
在这里插入图片描述

建UserDao

package com.dao;

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

import com.util.JsonBaseDao;
import com.util.JsonUtils;
import com.util.PageBean;
import com.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);
	}

}

修改MenuDao,主要是增加了一个listMenuSef方法,分权处理

package com.dao;

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

import com.entity.TreeNode;
import com.util.JsonBaseDao;
import com.util.JsonUtils;
import com.util.PageBean;
import com.util.StringUtils;

public class MenuDao extends JsonBaseDao{
	/**
	 *  给前台返回tree_date1.json的字符串
	 * @param paMap  从前台传递过来的参数集合
	 * @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;
	}
	
	
	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,002003...
//		002,002001,002002,002003...
//		
		
		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;
	}
	
	
	
	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);
	
	}
	
	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);
			}
	}
 
 
}

修改index.js,修改url

$(function(){
	$('#tt').tree({    
	    url:'menuAction.action?methodName=menuTree&&Menuid='+$("#menuIds").val(),
	    onClick: function(node){
//			alert(node.text);  // 在用户点击的时候提示
//			// add a new tab panel    $.extends
			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
				});  
			}
		}
	});  

})

在index.jsp中,传值menuids
如果没有这行代码,index.jsp无法使用$("#menuIds").val()

<input type="hidden" id="menuIds" value="${menuIds }">

UserAction控制器(判断有无此人,获取其权限,以及跳转页面)

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

public class UserAction extends ActionSupport {
	private UserDao userDao = new UserDao();
	/**
	 * 登陆成功后跳转index.jsp
	 * @param req
	 * @param resp
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	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:002,.....}]
//				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));
				return "index";
			}else {
//			没有
				req.setAttribute("msg", "用户不存在");
				return "login";
			}
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "login";
		}
	}

}

配置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>
		<forward name="login" path="/login.jsp" redirect="false"></forward>
	</action>
</config>

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">
<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
在这里插入图片描述
index.jsp
在这里插入图片描述
如果用户不存在时点击提交按钮:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值