easyui高级控件(一)
今天我们基于我上篇博客的代码完成简单树形菜单权限
它的目的在于>>> 让不同身份的用户拥有不同的权限
首先我们准备三张表,用户表,用户菜单中间表,菜单表
我们传递id通过中间表返回菜单表对应权限,不是昨天的从最大的子节点开始找,而是通过中间表的menuId来递归找到它们对应的子节点,这样不同角色就能操作不同权限特点就是用户权限多对多
开始实践>>>
写一个简单的登录界面
重写MenuDao
加一个根据menuId返回字节点菜单的方法,在分页方法里调取menuId返回字节点菜单的方法
我写的方法名叫这个listMenuSef(Map<String, String[]> map, PageBean pageBean)
package com.chen.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.chen.entity.TreeNode;
import com.chen.util.JsonBaseDao;
import com.chen.util.JsonUtils;
import com.chen.util.PageBean;
import com.chen.util.StringUtils;
public class MenuDao extends JsonBaseDao {
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;
}
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, "id"); // 获得当前节点的id
if (StringUtils.isNotBlank(id)) {
sql = sql + " and parentid = "+id ;
} else {
sql = sql + " and parentid = -1";// 没有数据则返回根节点
}
return super.executeQuery(sql, pageBean);
}
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";
String id = JsonUtils.getParamVal(map, "menuHid");
if (StringUtils.isNotBlank(id)) {
sql = sql + " and menuid in ("+id+") " ;
} else {
sql = sql + " and menuid = -1";// 没有数据则返回根节点
}
return super.executeQuery(sql, pageBean);
}
public 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);
Map<String, String[]> jspMap = new HashMap<>();
jspMap.put("id", new String[] {treeNode.getId()});
List<Map<String, Object>> listMenu = this.listMenu(jspMap, null);
List<TreeNode> treeNodeList = new ArrayList<>();
menuList2TreeNodeList(listMenu, treeNodeList);
treeNode.setChildren(treeNodeList);
}
public 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);
}
}
}
写一个 UserDao 在里面写好登录方法
package com.chen.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.chen.util.JsonBaseDao;
import com.chen.util.JsonUtils;
import com.chen.util.PageBean;
import com.chen.util.StringUtils;
public class UserDao extends JsonBaseDao {
/**
* 登录查询用户表
* @param paMap
* @param pageBean
* @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 调取userdao里的方法进行返回对应的子节点
package com.chen.dao;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.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);//调用查询中间表的方法
StringBuilder sb = new StringBuilder();
for (Map<String, Object> map : listMenu) {
sb.append(","+map.get("menuId"));
}
req.setAttribute("menuHid", sb.substring(1));
}else {
return "login";
}
} catch (Exception e) {
e.printStackTrace();
}
return "index";
}
}
重写js文件里的url
url:'menuAction.action?methodName=treeMenu&&menuHid='+$("#menuHid").val()
最后在index.jsp里body部分加入隐藏域串值接收 login 界面传递过来的值
<input type="hidden" id= "menuHid" value="${menuHid }" />
配置mvc.xml
<action path="/menuAction" type="com.chen.web.MenuAction">
<forward name="index" path="/index.jsp" redirect="false" />
</action>
<action path="/userAction" type="com.chen.web.UserAction">
<forward name="index" path="/index.jsp" redirect="false" />
</action>
运行看效果
id 001
只能看到学生管理
id 002 只能看到后勤管理
id 003 可以看到001和002都带有的操作
id 000 则全都可以看到
欧克~简单的权限管理就完成了