1:为什么要写权限,权限的目的是什么?
是为了让不同的用户可以操作系统中不同资源
直接点说就是不同的用户可以看到左侧不同的菜单
今天目的:实现用户与菜单之间的关系(用户权限多对多)
思路如下:
1、菜单不同的原因在于,利用不同menuid进行查询,
原本默认查询的是所有菜单,是通过-1去查的;
2、menuid由来:是登录用户id查询中间表数据所得来的
3、二星权限设计(用户权限多对多)
?执行数据库脚本
?修改原有的实体类
?建立实体类
?创建dao
?修改原有的dao
?新增web的方法
?新增登入界面,跳入前端树形菜单
基于昨天的基础:
MenuDao:
package com.cjq.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import com.cjq.entity.TreeNode;
import com.cjq.util.JsonBaseDao;
import com.cjq.util.JsonUtils;
import com.cjq.util.PageBean;
import com.cjq.util.StringUtils;
/**
* 1、查询数据库所有数据用于easyui的tree树形展示(但是直接得来的数据格式easyui不识别)
* 2、递归查询节点集合,形成子父节点关系,具备层次结构
* 3、转格式
* @author cjq
*
*/
public class MenuDao extends JsonBaseDao{
//接受TreeNode数据
/**
* list<TreeNode> 加上object可以转换成easyui的tree控件识别的json串
* @param map
* @param pageBean
* @return
* @throws Exception
*/
public List<TreeNode> listTreeNode(Map<String, String[]> map,PageBean pageBean) throws Exception{
List<Map<String, Object>> listMenu = this.listMenuAuth(map, null);
List<TreeNode> listTreeNode = new ArrayList<TreeNode>();
this.listMapToListTreeNode(listMenu, listTreeNode);
return listTreeNode;
}
/**
* 按照不同的用户登录能够访问不同的菜单
* @param map
* @param pageBean
* @return
* @throws Exception
*/
public List<Map<String, Object>> listMenuAuth(Map<String, String[]> map,PageBean pageBean) throws Exception{
String sql = "select * from t_easyui_menu where true";
//当前节点id
String id = JsonUtils.getParamVal(map, "Menuid");
if(StringUtils.isNotBlank(id)) {
//查询登录用户
sql += " and menuid in ("+id+")";
}else {
sql +=" and menuid=000";
}
return super.executeQuery(sql, pageBean);
}
/**
* 【{Menuid:001,Menuname:学生管理,children:[]},{Menuid:001,Menuname:学生管理}】
* 接下来需要递归查询子节点的集合存入当前节点
* @param map
* @param pageBean
* @return
* @throws Exception
*/
public List<Map<String, Object>> listMenu(Map<String, String[]> map,PageBean pageBean) throws Exception{
String sql = "select * from t_easyui_menu where true";
//当前节点id
String id = JsonUtils.getParamVal(map, "Menuid");
if(StringUtils.isNotBlank(id)) {
//当前节点的ID当作子节点的父id进行查询
sql += " and parentid="+id;
}else {
sql +=" and parentid=-1";
}
return super.executeQuery(sql, pageBean);
}
/**
* 将后台数据库查出来的数据格式转换成前台easyui所能识别的数据
* @param map
* @param treeNode
* @throws Exception
*/
public void mapToTreeNode(Map<String, Object> map,TreeNode treeNode) throws Exception {
treeNode.setId(map.get("Menuid").toString());
treeNode.setText(map.get("Menuname").toString());
treeNode.setAttributes(map);
//得到treeNode.setChildren(children)
Map<String, String[]> childMap = new HashMap<String, String[]>();
childMap.put("Menuid",new String [] {treeNode.getId()});
//查询出当前节点所拥有的子节点的集合
List<Map<String, Object>> listMenu = this.listMenu(childMap, null);
List<TreeNode> listTreeNode = new ArrayList<TreeNode>();
this.listMapToListTreeNode(listMenu, listTreeNode);
treeNode.setChildren(listTreeNode);
}
public void listMapToListTreeNode(List<Map<String, Object>> list,List<TreeNode> listTreeNode) throws Exception {
TreeNode treeNode =null;
for (Map<String, Object> map : list) {
treeNode = new TreeNode();
this.mapToTreeNode(map, treeNode);
listTreeNode.add(treeNode);
}
}
}
UserDao:
package com.cjq.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;import com.cjq.util.JsonBaseDao;
import com.cjq.util.JsonUtils;
import com.cjq.util.PageBean;
import com.cjq.util.StringUtils;public class UserDao extends JsonBaseDao {
/**
* 用于查询用户分页列表所用
* 用于用户登录所用
* @param map
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<Map<String, Object>> list(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
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 InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> getMenu(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_usermenu where true";
String uid=JsonUtils.getParamVal(map, "uid");
if(StringUtils.isNotBlank(uid)) {
sql+=" and uid="+uid;
}
return super.executeQuery(sql, pageBean);
}
}
web层 :UserAction
package com.cjq.web;
import java.util.List;
import java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cjq.dao.UserDao;
import com.cjq.framework.ActionSupport;
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);
StringBuffer sb=new StringBuffer();
for (Map<String, Object> map : listMenu) {
sb.append(","+map.get("menuId"));
}
//,001,002
req.setAttribute("menuHid", sb.substring(1));
}else {
return "login";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "index";
}
}
配置mvc.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/menuAction" type="com.cjq.web.MenuAction">
</action>
<action path="/userAction" type="com.cjq.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
});
}
}
});
})
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/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui/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">
<!-- 接收从login.jsp中传过来的值 -->
<input type="hidden" id="menuIds" value="${menuIds }">
<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;">southregion
</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>
运行结果如下: