EasyUi 权限

权限目的:

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

直接点说就是不同的用户可以看到左侧不同的菜单

1、一星权限设计(用户权限多对一)
1.1、执行数据库脚本
1.2、建立实体类
1.3、创建dao
1.4、Web层创建
1.5、更改展示的树形菜单

思路:
1、菜单不同的原因在于,利用不同menuid进行查询,原本默认查询的是所有菜单,是通过-1去查的;
2、menuid由来:是登录用户id查询中间表数据所得来的

2、二星权限设计(用户权限多对多)
2.1、执行数据库脚本
2.2、修改原有的实体类
2.3、建立实体类
2.4、创建dao
2.5、修改原有的dao
2.6、新增web的方法

UserDao.java代码如下:

package com.lg.dao;

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

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

public class UserDao extends JsonBaseDao {
	/**
	 * 用于查询用户分页列表所用
	 * 用于用户登录所用
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws Exception 
	 */
	public List<Map<String, Object>> list(Map<String, String[]> map,PageBean pageBean) throws Exception{
		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的集合
	 * 
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, Object>> getMenusByUser(Map<String, String[]> map,PageBean pageBean) throws Exception{
		String sql = "select * from t_easyui_usermenu where true ";
		String uid = JsonUtils.getParamVal(map, "uid");
		String upwd = JsonUtils.getParamVal(map, "upwd");
		if(StringUtils.isNotBlank(uid)) {
			sql += " and uid =" +uid;
		}
		return super.executeQuery(sql, pageBean);
	}
}


Web文件:

UserAction.Java代码如下:

 package com.lg.web;
    
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.lg.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.size() == 1 && list != null) {
    				// 用户存在
    				List<Map<String, Object>> menusList = this.userDao.getMenusByUser(req.getParameterMap(), null);
    				StringBuilder sb = new StringBuilder();
    				for (Map<String, Object> map : menusList) {
    					sb.append("," + map.get("menuId"));
    					System.out.println(sb.toString());
    				}
    				// ,001,002
    				req.setAttribute("menuIds", sb.substring(1));
    			} else {
    				// 用户不存在
    				req.setAttribute("msg", "用户不存在");
    				code = "login";
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    			code = "login";
    
    		}
    		return code;
    	}
    
    }

Conf文件:
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.lg.web.MenuAction">
	</action>
	<action path="/userAction" type="com.lg.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   
		    		});  
	    		}
	    		
	    	}
	});  
	
})


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>

Index.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">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/static/easyui5/themes/black/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>


图片结果(ID=003):
在这里插入图片描述
图片结果(ID=001):
在这里插入图片描述
登陆界面:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值