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

【easyui二】

程序员文章站 2024-02-05 12:01:16
...

easyui二:

权限树:
在一星权限树是user与menu是一对多的关系,它的弊端是一个菜单不能对应多个用户。
如果我们像一个用户对应多个菜单然后一个菜单对应多个用户。其实这就是user与menu是多对多的关系。这样就延伸到我们的一下权限树的升级版二星权限树。

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

首先我们今天的权限设置是在昨天的基础(也就是我昨天写的【easyui入门】)上加上一些代码:
首先先添加一个用户的dao方法:
UserDao:

package com.lihao.dao;

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

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

public class UserDao extends JsonBaseDao{
	
	/**
	 * 登陆查询用户表  登陆
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	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 = sql+" and uid = "+uid;
		}
		if(StringUtils.isNotBlank(upwd)) {
			sql = 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 = sql+" and uid = "+uid;
		}
		return super.executeQuery(sql, pageBean);
	}
}

然后添加一个UserAction用户控制器:
UserAction:

package com.lihao.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.lihao.dao.UserDao;
import com.lihao.framework.ActionSupport;

public class UserAction extends ActionSupport{
private UserDao ud=new UserDao();
	
	public String login(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
		List<Map<String, Object>> list = this.ud.list(req.getParameterMap(), null);
		if(list!=null&&list.size()>0) {
			List<Map<String, Object>> listMenu = this.ud.listMenu(req.getParameter("uid"), null);
			StringBuilder sb=new StringBuilder();
			for (Map<String, Object> map : listMenu) {
				sb.append(","+map.get("menuId"));
			}
			//,001,002,.....
			req.setAttribute("menuHid", sb.substring(1));
		}
		else {
			return "login";
		}
		return "index";
	}
}


然后就是在【easyui入门】中提到的MenuDao的代码上加了一个根据menuId查找菜单的方法(listMenuSef)
MenuDao:

package com.lihao.dao;

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

import com.lihao.entity.TreeNode;
import com.lihao.util.JsonBaseDao;
import com.lihao.util.JsonUtils;
import com.lihao.util.PageBean;
import com.lihao.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;
	}
	
	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");
		System.out.println(map);
		if(StringUtils.isNotBlank(id)) {
			sql = sql+" and menuid in ("+id+")";
		}else {
			sql = sql+" and menuid = -1";
		}
		return super.executeQuery(sql, pageBean);
	}
	
	
	/**
	 * 查询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 = sql+" and parentid = "+id;
		}else {
			sql = sql+" and parentid = -1";
		}
		return super.executeQuery(sql, pageBean);
	}
	
	/**
	 * {Menuid:1,...[]}
	 * ->{id:1,...[]}
	 * menu表的数据不符合easyui树形展示的数据格式
	 * 需要转换成easyui所能识别的数据格式
	 * @param map
	 * @param treeNode
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public 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
	 */
	public 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在mvc.xml中配置:
mvc.xml:

<action path="/menuAction" type="com.lihao.web.MenuAction">
		<forward name="index" path="/index.jsp" redirect="false" />
	</action>
	<action path="/userAction" type="com.lihao.web.UserAction">
		<forward name="index" path="/index.jsp" redirect="false" />
	</action>

就完成了,接下来我们就是看效果是否能够实现了:
首先我们进入登陆界面用id为001的用户登陆:
【easyui二】
通过超看我们知道只有学生管理这个权限
【easyui二】
最后我们再登录000,所有菜单都能看见:
【easyui二】