1、二星权限设计
从图中,我们可以看到,与一星表相比,多了一个中间表,这个中间表,调用的方法也就是多转了个弯。
代码如下:
package com.lx.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.lx.entity.TreeNode;
import com.lx.util.JsonBaseDao;
import com.lx.util.JsonUtils;
import com.lx.util.PageBean;
import com.lx.util.StringUtils;
/**
* 1、查询数据库所有数据用于easyui的tree树形展示(但是直接得来的数据格式easyui不识别)
* 2、递归查询节点集合,形成子父节点关系,具备层次结构
* 3、转格式
* @author Administrator
*
*/
public class MenuDao extends JsonBaseDao {
public List<TreeNode> listTreeNode(Map<String, String[]> map, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
List<Map<String, Object>> listMenu = this.listMenuAuth(map, pageBean);
List<TreeNode> listTreeNode = new ArrayList<TreeNode>();
this.listMapToListTreeNode(listMenu, listTreeNode);
return listTreeNode;
}
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, "Menuid");
if(StringUtils.isNotBlank(id)) {
// 当前节点的ID当作子节点父ID进行查询
sql += " and parentid="+id;
}else {
sql += " and parentid=-1";
}
return super.executeQuery(sql, pageBean);
}
public List<Map<String,Object>> listMenuAuth(Map<String, String[]> map, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from t_easyui_menu where true ";
String id = JsonUtils.getParamVal(map, "Menuid");
if(StringUtils.isNotBlank(id)) {
// 当前节点的ID当作子节点父ID进行查询
sql += " and menuid in ("+id+")";
}else {
sql += " and menuid=000";
}
return super.executeQuery(sql, pageBean);
}
public void mapToTreeNode(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);
// 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 InstantiationException, IllegalAccessException, SQLException {
TreeNode treeNode = null;
for (Map<String, Object> map : list) {
treeNode = new TreeNode();
this.mapToTreeNode(map, treeNode);
listTreeNode.add(treeNode);
}
}
}
然后写一个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>登录界面</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" value="提交">
<span style="color: red;">${msg }</span>
</form>
</body>
</html>
创建一个UserDao:
package com.lx.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.lx.util.JsonBaseDao;
import com.lx.util.JsonUtils;
import com.lx.util.PageBean;
import com.lx.util.StringUtils;
/**
* 用于查询用户分页列表所用
* 用于用户登录所用
* @author Administrator
*
*/
public class UserDao extends JsonBaseDao {
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);
}
*/
public List<Map<String, Object>> getMenusByUser(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");
String upwd=JsonUtils.getParamVal(map, "upwd");
if(StringUtils.isNotBlank(uid)) {
sql +="and uid ="+uid;
}
return super.executeQuery(sql, pageBean);
}
}
写一个web层:
package com.lx.web;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lx.dao.UserDao;
import com.lx.framework.ActionSupport;
public class UserAction extends ActionSupport {
private UserDao userDao = new UserDao();
public String login(HttpServletRequest req, HttpServletResponse resp) {
String code = "index";
// 登录
try {
List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), null);
if (list != null && list.size() == 1) {
// 用户存在
List<Map<String, Object>> menuList = this.userDao.getMenusByUser(req.getParameterMap(), null);
StringBuilder sb = new StringBuilder();
for (Map<String, Object> map : menuList) {
sb.append("," + map.get("menuId"));
}
req.setAttribute("menuIds", sb.substring(1));
} else {
// 用户不存在
req.setAttribute("msg", "没有此用户");
code = "login";
}
} catch (Exception e) {
e.printStackTrace();
code = "login";
}
return code;
}
}
配置一下xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!-- <action path="/regAction" type="test.RegAction">
<forward name="failed" path="/reg.jsp" redirect="false" />
<forward name="success" path="/login.jsp" redirect="true" />
</action> -->
<action path="/menuAction" type="com.lx.web.MenuAction">
</action>
<action path="/userAction" type="com.lx.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({ //登录成功后带上Menuid的值 去查询 对应的能够显示的菜单
//menuIds index.jsp传过来的变量
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">
<!-- ctrl+shift+r查找本工作区间打开的工程中的文件 -->
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/index.js"></script>
<title>后台管理主界面</title>
</head>
<!-- ctrl+shift+f 格式化-->
<body class="easyui-layout">
<input type="hidden" id="menuIds" value="${menuIds}">
<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="height: 800px;">
<div title="首页" style="padding:20px;display:none;">
欢迎界面
</div>
</div>
</div>
</body>
</html>