easyui高级控件(一)

本文介绍EasyUI中关于权限管理的实现,包括一星和二星权限的设计。一星权限涉及每个用户只能查看对应的任务。二星权限设计则更复杂,通过多对多关系设置用户权限,涉及数据库脚本执行、实体类修改、新实体类建立及DAO层的操作。同时,文章还提到了在MVC框架中的配置以及前端界面的登录和树形菜单的呈现。
摘要由CSDN通过智能技术生成

easyui高级控件(一)
权限:就是相对应的人只能看到自己先对应的事,每一个都对应每一个事
这个是一星权限
在这里插入图片描述
二星权限
在这里插入图片描述
2、二星权限设计(用户权限多对多)
步骤
2.1执行数据库脚本
2.2修改原有的实体类
2.3建立实体类
2.4创建dao

先把原来的dao方法修改一下 (MenuDao)

package com.xieyigui.dao;

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

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

public class MenuDao extends JsonBaseDao {
	
	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.listMapToListTreeNode(listMenu, listTreeNode);
		return listTreeNode;
	}
	

	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)) {
			sql += " and menuid in ("+id+") ";
		}else {
			sql += " and menuid=000";
		}
		return super.executeQuery(sql, pageBean);
	}
	
	
	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);
	}
	

	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.listMapToListTreeNode(listMenu, listTreeNode);
		treeNode.setChildren(listTreeNode);
	}
	
	public void listMapToListTreeNode(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);
		}
	}
}


第二步就是新增学生的权限(UserDao)

package com.xieyigui.dao;

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

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

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);
	}
/**
 * 通过用户登录的唯一帐号,在用户权限中间表中获取菜单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);
	}
}


UserAction

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

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;
		
	}
}



一定要配置mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>

	<action  path="/menuAction" type="com.lj.web.MenuAction"></action>
	<action  path="/userAction" type="com.lj.web.UserAction">
	 <forward name="index" path="/index.jsp" redirect="false" />
     <forward name="login" path="/login.jsp" redirect="false" />
	</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>用户登录界面</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="登录">

</form>
  ${msg}
</body>
</html>


<%@ 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/metro/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>


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").tab('select',node.text)
	    	}else{
	    		$('#menuTab').tabs('add',{    
		    	    title:node.text,    
		    	    content:content,    
		    	    closable:true 
		    	}); 
	    	}
	    }
	}); 
	
})


结果
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值