easyui(二)

权限树

基于easyui(一),在之前菜单表t_easyui_menu的基础上加了两张表:t_easyui_user_version2(用户表) ,t_easyui_usermenu(用户权限表)

在这里插入图片描述

权限目的
是为了让不同的用户可以操作系统中不同资源

思路
1、菜单不同的原因在于,利用不同menuid进行查询,原本默认查询的是所有菜单,是通过最大的节点-1去查的;
2,menuid由来:是登录用户id查询中间表数据所得来的,登陆获取到用户的uid,由uid去用户权限表里找到这个用户所对应的menuId,然后直接通过这些menuId来递归找到它们对应的子节点,而不再是通过最大的节点开始找,就能实现不同权限的用户进入看到了菜单不同。

用户登录 需要UserDao


package com.li.dao;

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

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

public class UserDao extends JsonBaseDao{

	/**
	 * 登陆查询用户表
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> login(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql = "select * from t_easyui_user_version2 where 1=1 ";
		String uid = JsonUtils.getParamVal(map, "uid");
		String upwd = JsonUtils.getParamVal(map, "upwd");
		if (StringUtils.isNotBlank(uid)) {
			sql = sql+" and uid = "+uid;
		}
		if (StringUtils.isNotBlank(upwd)) {
			sql = sql+" and upwd = "+upwd;
		}
		return super.executeQuery(sql, pageBean);
	}
	/**
	 * 通过中间表查询登陆用户所对应的权限
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> listMenu(String uid,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql = "select * from t_easyui_usermenu where 1=1 ";
		if (StringUtils.isNotBlank(uid)) {
			sql = sql+" and uid = "+uid;
		}
		return super.executeQuery(sql, pageBean);
	}
	
}


修改MenuDao

MenuDao在上次的代码上加了一个根据menuId查找菜单的方法


package com.li.dao;

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

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

public class MenuDao extends JsonBaseDao {
	/**
	 * 
	 * @param map req.getParameterMap
	 * @param pageBean 分页
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<TreeNode> list(Map<String, String[]> map,PageBean pageBean) throws Exception {
		List<Map<String,Object>> listMenu = this.listMenuSef(map, pageBean);//放入用户的权限id
		List<TreeNode> treeNodeList = new ArrayList<>();
		menuList2TreeNodeList(listMenu, treeNodeList);
		return treeNodeList;
	}
	/**
	 * 查询menu表的数据
	 * 
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<Map<String, Object>> listMenu(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException {
		String sql = "select * from t_easyui_menu where 1=1 ";
		String id = JsonUtils.getParamVal(map, "id");//改成menuHid就获取不到menu2TreeNode传入的id
		if (StringUtils.isNotBlank(id)) {
			sql = sql+" and parentid = "+id;
		}else {
			sql = sql+" and parentid = -1";
		}
		return super.executeQuery(sql, pageBean);
	}
	//
	public List<Map<String, Object>> listMenuSef(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException {
		String sql = "select * from t_easyui_menu where 1=1 ";
		String id = JsonUtils.getParamVal(map, "menuHid");//获取页面传输过来的menuHid
		if (StringUtils.isNotBlank(id)) {
			sql = sql+" and menuid in ("+id+") ";
		}else {
			sql = sql+" and menuid = -1";
		}
		return super.executeQuery(sql, pageBean);
	}
	/**
	 * {Menuid:1,.....[]}
	 * -->{id:1,.....[]}
	 * menu表的数据格式不符合easyui树形展示的数据格式
	 * 需要转换成easyui所能识别的数据格式
	 * @param map
	 * @param treeNode
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private void menu2TreeNode(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);
		
//		treeNode.setChildren(children);
		Map<String, String[]> jspMap = new HashMap<>();
		jspMap.put("id", new String[] {treeNode.getId()});
		List<Map<String, Object>> listMenu = this.listMenu(jspMap, null);
		List<TreeNode> treeNodeList = new ArrayList<>();
		menuList2TreeNodeList(listMenu, treeNodeList);
		treeNode.setChildren(treeNodeList);
		
		
	}
	
	/**
	 *  [{Menuid:1,.....[]},{Menuid:2,.....[]}]
	 * -->[{id:1,.....[]},{id:2,.....[]}]
	 * @param mapList
	 * @param treeNodeList
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	//把数据转为可以展示的格式
	private void menuList2TreeNodeList(List<Map<String, Object>> mapList,List<TreeNode> treeNodeList) throws InstantiationException, IllegalAccessException, SQLException {
		TreeNode treeNode = null;
		
		for (Map<String, Object> map : mapList) {
				treeNode = new TreeNode();
				menu2TreeNode(map, treeNode);
				treeNodeList.add(treeNode);
		}
	}
	
}

并且在index.jsp页面添加一个inpu用于传值

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

修改index.js

url:'menuAction.action?methodName=treeMenu&&menuHid='+$("#menuHid").val(),

UserAction用户控制器


package com.li.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.li.dao.UserDao;
import com.zking.framework.ActionSupport;

public class UserAction extends ActionSupport{
	private UserDao userDao = new UserDao();
	public String login(HttpServletRequest req,HttpServletResponse resp) {
		try {
			List<Map<String, Object>> list = this.userDao.login(req.getParameterMap(), null);
			if (list != null && list.size() > 0) {//判断是否登陆成功
				List<Map<String, Object>> listMenu = this.userDao.listMenu(req.getParameter("uid"), null);
				StringBuilder sb = new StringBuilder();
				for (Map<String, Object> map : listMenu) {//遍历获取权限字符串
					sb.append(","+map.get("menuId"));
				}
				req.setAttribute("menuHid", sb.substring(1));//去除逗号
			}else {//失败返回登陆
				return "login";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "index";
	}
}


mvc.xml文件配置

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

测试
登录001

在这里插入图片描述
001权限只有学生管理
在这里插入图片描述
再登陆一个有全部权限000
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值