欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

easyui(二)

程序员文章站 2024-02-05 11:39:28
...

实现简单的权限树形菜单

功能:不同权限的用户看到的菜单不同

今天的功能是在上次easyui(一)的代码基础上是实现了。
今天在上次菜单表t_easyui_menu的基础上加了两张表:t_easyui_user_version2(用户表)
t_easyui_usermenu(用户权限中间表)

思路:
首先登陆获取到用户的uid,由uid去用户权限表里找到这个用户所对应的menuId,然后我们直接通过这些menuId来递归找到它们对应的子节点,而不再是通过最大的节点开始找,于是就能实现不同权限的用户进入看到了菜单不同。

需要用户登录,所以需要UserDao

package com.xy.dao;

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

import com.xy.util.JsonBaseDao;
import com.xy.util.JsonUtils;
import com.xy.util.PageBean;
import com.xy.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);
	}
	/**
	 * 通过中间表查询登录用户所对应的权限
	 * @param paMap
	 * @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 true";
		if(StringUtils.isNotBlank(uid)) {
			sql+=" and uid="+uid;
		}
		return super.executeQuery(sql, pageBean);
	}
}

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

package com.xy.dao;

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

import com.xy.entity.TreeNode;
import com.xy.util.JsonBaseDao;
import com.xy.util.JsonUtils;
import com.xy.util.PageBean;
import com.xy.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 InstantiationException, IllegalAccessException, SQLException{
		List<Map<String, Object>> listMenu = this.listMenuSef(map, pageBean);
		List<TreeNode> treeNodeList=new ArrayList<>();
		menuList2TreeNodeList(listMenu, treeNodeList);
		return treeNodeList;
	}
	
	/**
	 * 根据传入的id找到它所对应的菜单,然后再由这些菜单成为最大节点依次找到下面的子节点
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> listMenuSef(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_menu where true ";
		String id=JsonUtils.getParamVal(map, "menuHid");
		if(StringUtils.isNotBlank(id)) {
			sql+=" and menuid in ("+id+")";
		}
		else {
			sql+=" and menuid = -1";
		}
		return super.executeQuery(sql, pageBean);
	}
	
	/**
	 * 1、查询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 true ";
		String id=JsonUtils.getParamVal(map, "id");
		if(StringUtils.isNotBlank(id)) {
			sql+=" and parentid = "+id;
		}
		else {
			sql+=" and parentid = -1";
		}
		return super.executeQuery(sql, pageBean);
	}
	
	
	/**
	 * menu表的数据不符合easyui树形展示的数据格式
	 * 需要转换成easyui所能识别的数据格式
	 * @param map
	 * @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);
		
		Map<String, String[]> jspMap=new HashMap<>();
		//获取到了当前节点的Id
		jspMap.put("id", new String [] {treeNode.getId()});
//		调方法把id当成parentid来查询子节点
		List<Map<String, Object>> listMenu = this.listMenu(jspMap, null);
		List<TreeNode> treeNodeList=new ArrayList<>();
		menuList2TreeNodeList(listMenu, treeNodeList);
		treeNode.setChildren(treeNodeList);
	}
	
	
	/**
	 * 转化数据格式
	 * @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);
		}
	}
}

UserAction用户控制器

package com.xy.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.xy.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.list(req.getParameterMap(), null);
			if(list!=null&&list.size()>0) {
				List<Map<String, Object>> listMenu = this.userDao.listMenu(req.getParameter("uid"), null);
				StringBuffer sb=new StringBuffer();
				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.xy.web.MenuAction">
		<forward name="index" path="/index.jsp" redirect="false"></forward>
	</action>
	<action path="/UserAction" type="com.xy.web.UserAction">
		 <forward name="index" path="/index.jsp" redirect="false" />
	</action> 
</config>

然后我们看一些效果如何,我们先登录001
easyui(二)
只有学生管理这个权限
easyui(二)
我们再登录002,就只有后勤管理
easyui(二)
最后再登录000,所有菜单都能看见
easyui(二)