EasyUi权限管理

接上一章EasyUi(链接)入门设计,设计效果如下图,我们再进行权限划分,也就是每个人能看到的树形菜单都不同

在这里插入图片描述

权限树

思路及步骤:

  1. 建立UserDao
  2. 修改MenuDao
  3. 修改 index.js
  4. 创建UserAction控制器
  5. 配置mvc.xml
  6. 新增登入界面(login.jsp),跳入相应树形菜单

菜单不同的原因在于,利用不同menuid进行查询,menuid:是登录用户id查询中间表数据所得来的

1、数据:三张表(上一章的菜单表 t_easyui_menu,新增用户表 t_easyui_user_version2 和用户权限表 t_easyui_usermenu)点击下载 提取码: puc5

在这里插入图片描述

2、建UserDao

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

3、修改MenuDao,主要是增加了一个listMenuSef方法,分权处理

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;
	}
	
	/**
	 * 查询menu表的数据
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	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, "id");
		String id = JsonUtils.getParamVal(map, "menuHid");
		if(StringUtils.isNotBlank(id)) {
			sql = sql + " and menuid in ("+id+")";
		}
		else {
			sql = sql + " and menuid = -1";
		}
		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, "id");
		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 
	 */
	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<>();
		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,....[]}]
	 * @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);
		}
	}
}

4、修改 index.js

在index.jsp中,传值menuHid

<input type="hidden" id="menuHid" value="${menuHid }">

修改 index.js

$(function(){
	$('#tt').tree({    
	    url:'menuAction.action?methodName=treeMenu&&menuHid='+$("#menuHid").val(),
	    onClick:function(node){
	    	var content = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>';
	    	if($('#menuTabs').tabs('exists',node.text)){
	    		$('#menuTabs').tabs('select',node.text);
	    	}
	    	else{
	    		$('#menuTabs').tabs('add',{    
		    	    title:node.text,    
		    	    content:content,    
		    	    closable:true,    
		    	    tools:[{ 
		    	        iconCls:'icon-mini-refresh',    
		    	        handler:function(){    
		    	            alert('refresh');    
		    	        }    
		    	    }]    
		    	}); 
	    	}
	    }
	}); 
})

5、UserAction控制器(判断有无此人,获取其权限,以及跳转页面)

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);
				StringBuilder sb=new StringBuilder();
				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";
	}
}

6、配置mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
	
	<action path="/menuAction" type="com.Tang.web.MenuAction">
		<forward name="index" path="/index.jsp" redirect="false" />
	</action>
	<action path="/userAction" type="com.Tang.web.UserAction">
		<forward name="index" path="/index.jsp" redirect="false" />
	</action>
</config>

7、界面

login.jsp(登录界面)

<body>
<form action="${pageContext.request.contextPath }/userAction.action?methodName=login" method="post">
	ID:<input typr="text" name="uid"><br>
	UPWD:<input typr="text" name="uname"><br>
	<input type="submit"><br>
</form>
</body>

index.jsp(树形首页)

<body class="easyui-layout">

	<input type="hidden" id="menuHid" value="${menuHid }">
	
	<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="menuTabs" class="easyui-tabs" style="width:500px;height:250px;">   
    <div title="Tab1" style="padding:20px;display:none;">   
        	你好,欢迎使用   
    </div>   
       </div>   
</div> 
	</div>
</body>

8、效果

如果001登录
在这里插入图片描述
则显示相应的树形菜单
在这里插入图片描述
如果002登录
在这里插入图片描述
则与001的树形菜单不同
在这里插入图片描述
所以,本章实现了根据用户的不同,登录后看到的树形菜单和内容也就不同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值