easyui高级控件
1、 权限树
1、一星权限设计(用户权限多对一)
2、执行数据库脚本
3、建立实体类
4、创建dao
5、Web层创建
6、更改展示的树形菜单
思考:我们想一个用户对应多个菜单然后一个菜单可以对应多个用户
其实这就是user与menu的多对多的关系
思路:
1、菜单不同的原因在于,利用不同menuid进行查询,原本默认查询的是所有菜单,是通过-1去查的;
2、menuid由来:是登录用户id查询中间表数据所得来的
2、二星权限设计(用户权限多对多)
1、执行数据库脚本
2、修改原有的实体类
3、建立实体类
4、创建dao
5、修改原有的dao
6、新增web的方法
7、新增登入界面,跳入前端树形菜单
TreeNode 建立entity实体类:
package com.xiaoyi.entity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*针对easyui属性展示的json格式串进行实体类描述
*/
public class TreeNode {
private String id;
private String text;
private List<TreeNode> children=new ArrayList<>();
private Map<String, Object> attributes=new HashMap<String, Object>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
public Map<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
@Override
public String toString() {
return "TreeNode [id=" + id + ", text=" + text + ", children=" + children + ", attributes=" + attributes + "]";
}
}
MenuDao 修改一下原有的dao包:
package com.xiaoyi.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.xiaoyi.entity.TreeNode;
import com.xiaoyi.util.JsonBaseDao;
import com.xiaoyi.util.JsonUtils;
import com.xiaoyi.util.PageBean;
import com.xiaoyi.util.StringUtils;
/**1.查询数据库所有数据 用于easyui的tree树形展示,(但是直接得来的书籍格式easyui不识别)
* 2.递归查询节点集合,形成子父节点关系,具备层次结构
* 3.转格式
*
*/
public class MenuDao extends JsonBaseDao{
/**List<TreeNode>加上objectMapper可以转化成easyUI的tree控件识别的json串
* @param map
* @param pageBean
* @return
* List<Map<String, Object>> -->{Menuid:001,Menuname:学生管理}
* 接下来需要递归查询子节点的集合存入当前节点
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
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.listMapToTreeNode(listMenu, listTreeNode);
return listTreeNode;
}
/**
* 按照不同用户登录能够访问不同菜单
* @param map
* @param pageBean
* @return
* List<Map<String, Object>> -->{Menuid:001,Menuname:学生管理}
* 接下来需要递归查询子节点的集合存入当前节点
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
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);
}
/**
* List<Map<String, Object>> -->{Menuid:001,Menuname:学生管理}
* 接下来需要递归查询子节点的集合存入当前节点
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
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);
}
/**
* 需要将后台数据库查出来的数据格式转换成前台
* @param map
* @param treeNode
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
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[]>chilMap=new HashMap<String, String[]>();
chilMap.put("Menuid", new String[] {treeNode.getId()});
//查询出当前节点所拥有的子节点的集合
List<Map<String, Object>> listMenu=this.listMenu(chilMap, null);
List<TreeNode> listTreeNode= new ArrayList<TreeNode>();
this.listMapToTreeNode(listMenu, listTreeNode);
treeNode.setChildren(listTreeNode);
}
public void listMapToTreeNode(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);
}
}
}
UserDao 用于用户登录所用
package com.xiaoyi.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.xiaoyi.util.JsonBaseDao;
import com.xiaoyi.util.JsonUtils;
import com.xiaoyi.util.PageBean;
import com.xiaoyi.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>> 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");
if(StringUtils.isNotBlank(uid)) {
sql += " and uid ="+uid;
}
return super.executeQuery(sql, pageBean);
}
}
UserAction 新增用户登录的web界面
package com.xiaoyi.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.xiaoyi.dao.UserDao;
import com.zking.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 (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
code="login";
}
return code;
}
}
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>
uname:<input type="text"name="upwd"><br>
<input type="submit"value="ok">
</form>
</body>
</html>
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
});
}
}
});
})
效果: