二星权限设计
权限目的:
是为了让不同的用户可以操作系统中不同资源
直接点说就是不同的用户可以看到左侧不同的菜单
dao包:
package com.zhoutubing.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.zhoutubing.util.JsonBaseDao;
import com.zhoutubing.util.JsonUtils;
import com.zhoutubing.util.PageBean;
import com.zhoutubing.util.StringUtils;
public class UserDao extends JsonBaseDao {
/**
* 用户登陆或者查询用户分页信息的公共方法
* @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);
}
/**
* 根据当前用户登陆的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");
if(StringUtils.isNotBlank(uid)) {
sql += " and uid = "+uid;
}
return super.executeQuery(sql, pageBean);
}
}
修改原有的dao
package com.zhoutubing.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.zhoutubing.entity.TreeNode;
import com.zhoutubing.util.JsonBaseDao;
import com.zhoutubing.util.JsonUtils;
import com.zhoutubing.util.PageBean;
import com.zhoutubing.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");
// 为什么将parentid改成menuId?
// 原因在之前的方法,只能查询当前节点的所有子节点集合,不能将当前节点给查询出来
if(StringUtils.isNotBlank(menuId)) {
sql +=" and menuId in ("+menuId+")"+menuId;
}else {
sql +=" and parentid=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);
}
}
}
web层:
package com.zhoutubing.web;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhoutubing.dao.UserDao;
import com.zhoutubing.framework.ActionSupport;
public class UserAction extends ActionSupport {
private UserDao userDao=new UserDao();
/**
* 登录成功后跳转index.jsp
* @param req
* @param resp
* @return
* @throws Exception
*/
public String login(HttpServletRequest req,HttpServletResponse resp) throws Exception {
//系统中是否有当前登陆用户
// 有
Map<String, Object> map=null;
// 查询用户菜单中间表,获取对应menuid的集合
try {
map = this.userDao.list(req.getParameterMap(), null).get(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
req.setAttribute("msg", "用户不存在");
return "login";
}
if(map!=null && map.size()>0) {
// [{Menuid:002,...},{Menuid:003,...}]
// 002,003
StringBuilder sb=new StringBuilder();
List<Map<String,Object>> menuIdArr = this.userDao.getMenuByUid(req.getParameterMap(), null);
for (Map<String, Object> m : menuIdArr) {
sb.append(","+m.get("menuId"));
}
// ,002,003
req.setAttribute("menuIds", sb.substring(1));
return "index";
}else {
// 没有
req.setAttribute("msg", "用户不存在");
return "login";
}
}
}
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,
});
}
}
});
});
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" >
</form>
<span style="color: red" >${msg }</span>
</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/js/public/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/public/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">
<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="">
<div title="首页" style="padding:20px;display:none;">
欢迎界面
</div>
<input type="hidden" id="menuIds" value="${menuIds }" >
</div>
</div>
</body>
</html>
mvc.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/menuAction" type="com.zhoutubing.web.MenuAction">
</action>
<action path="/userAction" type="com.zhoutubing.web.UserAction">
<forward name="index" path="/index.jsp" redirect="false" />
<forward name="login" path="/login.jsp" redirect="false" />
</action>
</config>
ID为001的用户登录及权限:
ID为002的用户登录及权限:
ID为003的用户登录及权限:
ID为000的用户登录及权限:
不存在用户登入时,会有提示: