easyui(二)

本文详细介绍了用户权限设计的一星和二星方案,包括数据库脚本执行、实体类创建、DAO层开发、Web层实现及树形菜单展示,探讨了用户与菜单的多对多关系处理,展示了具体的代码实现。
一.权限树
  1. 一星权限设计(用户权限多对一)
    步骤:
    执行数据库脚本
    建立实体类
    创建dao
    Web层创建
    更改展示的树形菜单
    弊端:一个菜单不能对应多个用户!
    针对以上弊端可以思考:我们想一个用户对应多个菜单
    然后一个菜单可以对应多个用户
    其实这就是user与menu的多对多的关系
    思路:
    1.1 菜单不同的原因在于,利用不同menuid进行查询,原本默认查询的是所有菜单,是通过-1去查的;
    1.2 menuid由来:是登录用户id查询中间表数据所得来的
  2. 二星权限设计(用户权限多对多)
    执行数据库脚本
select * from t_easyui_menu where parentid=001;

修改原有的实体类
建立实体类

package com.xfz.entity;

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

/**
 * @author ld
 * 针对easyUI树形展示的json格式经行了实体类的描述
 *
 */
public class TreeNode {
	private String id;
	private String text;
	private List<TreeNode> children=new ArrayList<TreeNode>();
	private Map<String, Object> attributes=new HashMap<String, Object>();
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public List<TreeNode> getChildren() {
		return children;
	}
	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}
	public Map<String, Object> getAttributes() {
		return attributes;
	}
	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}
	@Override
	public String toString() {
		return "TreeNode [id=" + id + ", text=" + text + ", children=" + children + ", attributes=" + attributes + "]";
	}
	

}

创建dao

package com.xfz.dao;

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

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

/**
 * @author ld
 * 用于查询分页列表所用
 * 用于用户登录所用
 */
public class UserDao extends JsonBaseDao{
	public List<Map<String, Object>> list(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_user_version2 where true";
		String uid=JsonUtils.getParamVal(map, "uid");
		String upwd=JsonUtils.getParamVal(map, "upwd");
		if(StringUtils.isNotBlank(uid)) {
			sql+=" and uid="+uid;
		}
		if(StringUtils.isNotBlank(upwd)) {
			sql+=" and upwd="+upwd;
		}
		return super.executeQuery(sql, pageBean);
	}
	
	/**
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 * 通过用户登录的唯一账号,在用户父权限中间表中获取菜单id的集合
	 */
	public List<Map<String, Object>> getMenusByUser(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_usermenu where true";
		String uid=JsonUtils.getParamVal(map, "uid");
		if(StringUtils.isNotBlank(uid)) {
			sql+=" and uid="+uid;
		}
		return super.executeQuery(sql, pageBean);
	}

}

修改原有的dao(增加了一个listMenuAuth方法,作用:按照不同的用户登录访问不同的用户菜单)

package com.xfz.dao;

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

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


/**
 * @author ld
 * 1、查询数据库所有数据用于easyui的tree树形展示(但是直接得来的数据格式easyui不识别)
 * 2、递归查询节点集合,形成子父节点关系,具备层次结构
 * 3、转格式
 */
public class MenuDao extends JsonBaseDao{
	/**
	 * @author ld
	 * List<TreeNode>加上ObjectMapper可以转换成easyui的tree控件识别的json串
	 *
	 */
	public List<TreeNode> listTreeNode(Map<String, String[]> map, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		List<Map<String, Object>> listMenu = this.listMenuAuth(map, pageBean);
		List<TreeNode> listTreeNode = new ArrayList<TreeNode>();
		this.listMapToTreeNode(listMenu, listTreeNode);
		return listTreeNode;
	}
	
/**
 * @param map
 * @param pageBean
 * @return
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws SQLException
 * List<Map<String, Object>>
 * 返的是:{Menuid:001,Menuname:学生管理,childen[]},{Menuid:001,Menuname:学生管理}
 * 接下来需要递归查询子节点的集合存入当前节点
 */
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, "Menuid");
		if(StringUtils.isNotBlank(id)) {
			//当前节点的id当做子节点父id进行查询
			sql+=" and parentid="+id;
		}else {
			sql+=" and parentid=-1";
		}
		return super.executeQuery(sql, pageBean);
	} 

/**
 * @param map
 * @param pageBean
 * @return
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws SQLException
 * List<Map<String, Object>>
 * 按照不同的用户登录访问不同的用户菜单
 */
public List<Map<String, Object>> listMenuAuth(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
	String sql="select * from t_easyui_menu where true";
	String id=JsonUtils.getParamVal(map, "Menuid");
	if(StringUtils.isNotBlank(id)) {
		//当前节点的id当做子节点父id进行查询
		sql+=" and menuid in ("+id+")";
	}else {
		sql+=" and menuid=000";
	}
	return super.executeQuery(sql, pageBean);
} 

/**
 * @param map
 * @param treeNode
 * 需要将后台的数据库查出来的数据格式转换成前台easyui所识别的数据
 * @throws SQLException 
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 */
public void mapToTreeNode(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[]> childMap=new HashMap<String, String[]>();
	childMap.put("Menuid", new String[] {treeNode.getId()});
	//查询出当前节点所拥有的子节点的集合
	List<Map<String, Object>> listMenu=this.listMenu(childMap, null);
	List<TreeNode> listTreeNode=new ArrayList<TreeNode>();
	this.listMapToTreeNode(listMenu, listTreeNode);
	treeNode.setChildren(listTreeNode);
}

public void listMapToTreeNode(List<Map<String, Object>> list, List<TreeNode> listTreeNode) throws InstantiationException, IllegalAccessException, SQLException {
	TreeNode treeNode=null;
	for (Map<String, Object> map : list) {
		treeNode=new TreeNode();
		this.mapToTreeNode(map, treeNode);
		listTreeNode.add(treeNode);
	}
}
}

新增web的方法

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

public class UserAction extends ActionSupport{
	private UserDao userDao=new UserDao();
	public String login(HttpServletRequest req,HttpServletResponse resp) {
		String code="index";
		//登录
		try {
			List<Map<String, Object>> list=this.userDao.list(req.getParameterMap(), null);
				if(list!=null && list.size()==1) {
					//用户存在
					List<Map<String, Object>> menuList=this.userDao.getMenusByUser(req.getParameterMap(), null);
					StringBuilder sb=new StringBuilder();
					for (Map<String, Object> map : menuList) {
						sb.append(","+map.get("menuId"));
					}
					//001,002
					req.setAttribute("menuIds", sb.substring(1));
				}else {
					//用户不存在
					req.setAttribute("msg", "用户不存在");
					code="login";
				}
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			code="login";
		}
		return code;
	}

}

新增登入界面,跳入前端树形菜单

<%@ 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>
</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="OK">
</form>
</body>
</html>

除此之外还要配置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.xfz.web.MenuAction">
	</action>
	<action path="/userAction" type="com.xfz.web.UserAction">
		<forward name="index" path="/index.jsp" redirect="false" />
		<forward name="login" path="/login.jsp" redirect="false" />
	</action>
</config>

修改一下原有的index.js

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

跳转到主页面

<%@ 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">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/easyui5/themes/default/easyui.css">   
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/easyui5/themes/icon.css">   
<script type="text/javascript" src="${pageContext.request.contextPath}/static/easyui5/jquery.min.js"></script>   
<script type="text/javascript" src="${pageContext.request.contextPath}/static/easyui5/jquery.easyui.min.js"></script>  
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/index.js"></script>  
<title>后台管理主界面</title>
</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="height: 800px;">   
    <div title="首页" style="padding:20px;display:none;">   
        默认首页展示内容    
    </div>   
</div>  
	</div>
</body>
</html>
二.显示效果:

用户001进入的效果
在这里插入图片描述
在这里插入图片描述
用户002进入的效果
在这里插入图片描述
在这里插入图片描述
用户003进入的效果
在这里插入图片描述
在这里插入图片描述
用户000进入的效果
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值