权限二星设置
目的:
为了让不同用户进入页面时所查看的东西不同,
核心思想:
就是控制用户登录后台所传递的menuId
在初入easyui的MenuDao方法中增加一个权限的方法listMapAuth方法
package com.hutao.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hutao.entity.TreeNode;
import com.hutao.util.JsonBaseDao;
import com.hutao.util.JsonUtils;
import com.hutao.util.PageBean;
import com.hutao.util.StringUtils;
public class MenuDao extends JsonBaseDao{
private List<TreeNode> listTreeNode;
/**
* 给前台返回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;
}
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");
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':'学生管理'},{{‘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’:001,'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);
}
}
}
写一个login的登陆jsp页面,使用下面的表进行登陆
新写一个userDao类实现查询方法以及通过当前用户id查找menu id的方法
package com.hutao.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.hutao.util.JsonBaseDao;
import com.hutao.util.JsonUtils;
import com.hutao.util.PageBean;
import com.hutao.util.StringUtils;
public class UserDao extends JsonBaseDao {
//查询方法
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);
}
//用当前用户id查找menuid
public List<Map<String,Object>> listMenu(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");
if (StringUtils.isNotBlank(uid)) {
sql += " and uid = "+uid;
}
return super.executeQuery(sql, pageBean);
}
}
需要的一张中间表通过id来查看用户有哪些权限
login登陆页面
<%@ 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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/userAction.action?methodName=login" method="post">
uid:<input type="text" name="uid">
upwd:<input type="text" name="upwd">
<input type="submit">
<span style="color: red;">${msg }</span>
</form>
</body>
</html>
web控制层
package com.hutao.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.hutao.dao.UserDao;
import com.hutao.entity.TreeNode;
import com.hutao.util.ResponseUtil;
import com.zking.framework.ActionSupport;
public class UserAction extends ActionSupport {
private UserDao u=new UserDao();
public String login(HttpServletRequest req, HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
Map<String,Object> map=null;
try {
try {
map=this.u.list(req.getParameterMap(),null).get(0);
} catch (Exception e) {
req.setAttribute("msg","这傻逼还没出生");
return "login";
}
if (map!=null && map.size() > 0) {
StringBuilder sb=new StringBuilder();
List<Map<String,Object>> menIdArr=this.u.listMenu(req.getParameterMap(),null);
for (Map<String, Object> m : menIdArr) {
sb.append(","+m.get("menuid"));
}
req.setAttribute("menuIds", sb.substring(1));
return "index";
}else {
req.setAttribute("msg","这傻逼还没生");
return "login";
}
} catch (Exception e) {
}
return "login";
}
}
index.jsp页面写入一个隐藏域
<input type="hidden" id="menuIds" value="${menuIds }">
在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,
});
}
}
});
})
效果
使用001的id进行登陆后对应的menuid也是001,所以对应的权限也就是学生管理权限,效果如下图所示: