easyui权限

2019年6月30日 23点热度 0条评论 来源: Hマ

权限目的:

是为了让不同的用户可以操作系统中不同资源
直接点说就是不同的用户可以看到左侧不同的菜单

实现菜单权限的核心思想就是控制用户登录后台所传递的menuId

思路:

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

MenuDao

package com.huangwen.dao;

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

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

public class MenuDao extends JsonBaseDao{
	/**
	 * 给前台返回tree_data1.json的字符串
	 * @param paMap	从前台jsp传递过来的参数集合
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<TreeNode> listTreeNode(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		List<Map<String, Object>> listMap = this.listMapAuth(paMap, pageBean);
		List<TreeNode> listTreeNode = new ArrayList<>();
		this.listMapToListTreeNode(listMap, listTreeNode);
		return listTreeNode;
	}
	
	/**
	 * [{'Menuid':001},{'Menuname':'学生管理'},{
  {'Menuid':002},{'Menuname':'后勤管理'}}]
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> listMap(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql = "select * from t_easyui_menu where true";
		String menuId = JsonUtils.getParamVal(paMap, "Menuid");
		if(StringUtils.isNotBlank(menuId)) {
			sql += " and parentid=" + menuId;
		}else {
			sql += " and parentid=-1";
		}
		
//		这里面存放的是数据库中菜单信息
		List<Map<String, Object>> listMap = super.executeQuery(sql, pageBean);
		return listMap;
	}
	
	
	public List<Map<String, Object>> listMapAuth(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql = "select * from t_easyui_menu where true";
		String menuId = JsonUtils.getParamVal(paMap, "Menuid");
//		为什么将parentid改成menuId?
//		原因在于之前的方法,只能查询当前节点的所有子节点集合,不能将当前节点查询出来
//		002 ---> 002001,002002....
//		002,002001,002002....
		if(StringUtils.isNotBlank(menuId)) {
			sql += " and menuId in ("+menuId+")";
		}else {
			sql += " and menuId=000";
		}
//		这里面存放的是数据库中菜单信息
		List<Map<String, Object>> listMap = super.executeQuery(sql, pageBean);
		return listMap;
	}
		
	/**
	 * {'Menuid':001},{'Menuname':'学生管理'}
	 * -->
	 * {id:...,text:...}
	 * @param map
	 * @param treeNode
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private void mapToTreeNode(Map<String, Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
		treeNode.setId(map.get("Menuid")+"");
		treeNode.setText(map.get("Menuname")+"");
		treeNode.setAttributes(map);
		
//		将子节点添加到父节点当中,建立数据之间的父子关系
//		treeNode.setChildren(children);
		Map<String, String[]> childrenMap = new HashMap<>();
		childrenMap.put("Menuid", new String[] {treeNode.getId()});
		List<Map<String, Object>> listMap = this.listMap(childrenMap, null);
		List<TreeNode> listTreeNode = new ArrayList<>();
		this.listMapToListTreeNode(listMap, listTreeNode);
		treeNode.setChildren(listTreeNode);
	}
	
	/**
	 * [{'Menuid':001},{'Menuname':'学生管理'},{
  {'Menuid':002},{'Menuname':'后勤管理'}}]
	 * -->
	 * tree_data1.json
	 * @param listMap
	 * @param listTreeNode
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private void listMapToListTreeNode(List<Map<String, Object>> listMap,List<TreeNode> listTreeNode) throws InstantiationException, IllegalAccessException, SQLException {
		TreeNode treeNode = null;
		for (Map<String, Object> map : listMap) {
			treeNode = new TreeNode();
			mapToTreeNode(map, treeNode);
			listTreeNode.add(treeNode);
		}
	}
}

UserDao

package com.huangwen.dao;

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

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

public class UserDao extends JsonBaseDao{
	
	/**
	 *list方法: 用户登录或者查询用户分页信息的公用方法
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	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+=" and uid="+uid;
		}if(StringUtils.isNotBlank(upwd)) {
			sql+=" and upwd="+upwd;
		}
		return super.executeQuery(sql, pageBean);
		
	}
	/**
	 *getMenuByUid方法:根据当前用户登录的id去查询对应的所有菜单
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> getMenuByUid(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_usermenu where true";
		String uid=JsonUtils.getParamVal(paMap, "uid");
		String upwd=JsonUtils.getParamVal(paMap, "upwd");
		if(StringUtils.isNotBlank(uid)) {
			sql+=" and uid="+uid;
		}
		return super.executeQuery(sql, pageBean);	
	}
}

UserAction

package com.huangwen.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.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huangwen.dao.UserDao;
import com.huangwen.entity.TreeNode;
import com.huangwen.framework.ActionSupport;
import com.huangwen.util.ResponseUtil;

public class UserAction extends ActionSupport{
	private UserDao userDao=new UserDao();
	/**
	 * 登录成功后跳转index.jsp
	 * @param req
	 * @param resp
	 * @return
	 */
	public String login(HttpServletRequest req,HttpServletResponse resp) {
//		判断系统中是否有当前登录用户
		try {
			Map<String, Object> map=null;
			try {
			 map = this.userDao.list(req.getParameterMap(), null).get(0);	
			} catch (Exception e) {
				req.setAttribute("msg", "用户不存在");
				return "login";
			}
//		如果有,查询用户菜单中间表,获取对应menuId的集合·
			if(map!=null && map.size()>0) {
				StringBuilder sb=new StringBuilder();
				List<Map<String, Object>> menuByUid = this.userDao.getMenuByUid(req.getParameterMap(), null);
				for (Map<String, Object> map2 : menuByUid) {
					sb.append(","+map2.get("menuId"));
				}
				req.setAttribute("menuIds", sb.substring(1));
				return "index";
			}else {
				req.setAttribute("msg", "用户不存在");
				return "login";
			}
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "login";
		}
		return null;
	}
}

index.js

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

mvc.xml

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

<action path="/userAction" type="com.huangwen.web.MenuAction">
	<forward name="index" path="/index.jsp" redirect="false"/>
	<forward name="login" path="/login.jsp" redirect="false"/>
</action>

index.jsp
在index.jsp界面写一个隐藏域方便index.js调用,避免index.js页面报错

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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/userAction.action?methodName=login" method="post">
	账号:<input type="text" name="uid"><br>
	密码:<input type="text" name="upwd"><br>
	<input type="submit" value="登录">
</form>
	<span style="color: red">${msg}</span>
</body>
</html>


    原文作者:Hマ
    原文地址: https://blog.csdn.net/weixin_45176963/article/details/94357826
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。