Easyui(二)之权限划分
1:为什么要写权限,权限的目的是什么?
是为了让不同的用户可以操作系统中不同资源
直接点说就是不同的用户可以看到左侧不同的菜单
今天目的:实现用户与菜单之间的关系(用户权限多对多)
思路如下:
1、菜单不同的原因在于,利用不同menuid进行查询,
原本默认查询的是所有菜单,是通过-1去查的;
2、menuid由来:是登录用户id查询中间表数据所得来的
基于昨天的基础:
MenuDao
public class MenuDao extends JsonBaseDao {
/**
*
* @param map req.getparamerMap
* @param pageBean 分页
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
// 1查所有
public List<TreeNode> list(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
List<Map<String,Object>> listMenu = this.listMenuSef(map, pageBean);
List<TreeNode> treeNodeList=new ArrayList<>();
menuList2TreeNodeList(listMenu, treeNodeList);
return treeNodeList;
}
//5查小类
//查询不一定要对象 ,也可以集合 Map<String,Object>
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 ";
//拿到当前节点的Id id把ID当成父Id来查
String id=JsonUtils.getParamVal(map, "id");
//子节点由父Id来传
if(StringUtils.isNotBlank(id)) {
sql = sql + " and parentid = "+id;
}else {
//根节点数据库为负一
sql = sql + " and parentid = -1";
}
//查询所有节点
return super.executeQuery(sql, pageBean);
}
/**
* 1.查询menu表的数据
* @param map
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
//查询不一定要对象 ,也可以集合 Map<String,Object>
// 2查大类
public List<Map<String,Object>> listMenuSef(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
//数据库的列名与我们写的不同 所有需要查询
String sql="select * from t_easyui_menu where true ";
//拿到当前节点的Id id把ID当成父Id来查
String id=JsonUtils.getParamVal(map, "menuHid");
//子节点由父Id来传
if(StringUtils.isNotBlank(id)) {
sql = sql + " and menuid in ("+id+")";
}else {
//根节点数据库为负一
sql = sql + " and menuid = -1";
}
//查询所有节点
return super.executeQuery(sql, pageBean);
}
/**
* {Means:1,....[]}
* -->{id:1,....[]}
* menu表的数据格式不符合easyui树形展示的数据格式
* 需要转化成easyui所识别的数据格式
* @param map
* @param treeNode
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
//4转类型
private void menu2TreeNode(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[]> jspMap=new HashMap<>();
//当前节点作为父节点的id查
jspMap.put("id", new String[] {treeNode.getId()});
//查出当前Id的子节点 直接查找数据库
List<Map<String, Object>> listMenu = this.listMenu(jspMap, null);
//转换为TreeNode
List<TreeNode> treeNodeList=new ArrayList<>();
menuList2TreeNodeList(listMenu, treeNodeList);
treeNode.setChildren(treeNodeList);
}
/**
* [{Means:1,....[]},{Means:2,....[]}]
* -->[{id:1,....[]},{id:2,....[]}]
* @param mapList
* @param treeNodeList
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
//3遍历map集合查小类
private void menuList2TreeNodeList(List<Map<String, Object>>mapList,List<TreeNode> treeNodeList) throws InstantiationException, IllegalAccessException, SQLException {
//节约内存
TreeNode treeNode=null;
for (Map<String, Object> map : mapList) {
treeNode=new TreeNode();
menu2TreeNode(map, treeNode);
treeNodeList.add(treeNode);
}
}
}
login登录界面
<form action="${pageContext.request.contextPath }/UserAction.action?methodName=login" method="post">
uid:<input type="text" name="uid"/><br/>
upwd:<input type="password" name="upwd"/>
<input type="submit" value="查询">
</form>
UserDao
public class UserDao extends JsonBaseDao {
/**
* 登录查询用户表 登录
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
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=sql+" and uid = "+uid;
}
if(StringUtils.isNotBlank(upwd)) {
sql=sql+" and upwd = "+upwd;
}
return super.executeQuery(sql, pageBean);
}
/**
* 通过中间表 查询登录所对应的权限
* @param paMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> listMenu(String uid,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_usermenu where true";
if(StringUtils.isNotBlank(uid)) {
sql=sql+" and uid = "+uid;
}
return super.executeQuery(sql, pageBean);
}
}
UserAction
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";
}
}
index.js页面
$(function(){
$('#tt').tree({
url:'menuAction.action?methodName=treeMenu&&menuHid='+$("#menuHid").val(),
onClick:function(node){
var content = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>';
if($('#menuTabs').tabs('exists',node.text)){
$('#menuTabs').tabs('select',node.text);
}else{
$('#menuTabs').tabs('add',{
title:node.text,
content:content,
closable:true,
tools:[{
iconCls:'icon-mini-refresh',
handler:function(){
alert('refresh');
}
}]
});
}
}
});
})
页面展示为:
001学生管列的展示页面
//后勤管理,房屋租金的页面
//主页面