思考:
我们想一个用户对应多个菜单
然后一个菜单可以对应多个用户
其实这就是user与menu的多对多的关系
二星权限设计(用户权限多对多)
创建dao
public class UserDao extends JsonBaseDao{
/*
* 用户登录或者查询用户分页信息的公共方法
* */
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+=" and uid = "+uid;
}
if(StringUtils.isNotBlank(upwd)) {
sql+=" and upwd = "+upwd;
}
return super.executeQuery(sql, pageBean);
}
/*
* 根据当前用户登录的ID去查询对应的所有菜单
* */
public List<Map<String, Object>> getMenuByUid(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_usermenu where true ";
String uid=JsonUtils.getParamVal(paMap, "uid");
if(StringUtils.isNotBlank(uid)) {
sql+=" and uid = "+uid;
}
return super.executeQuery(sql, pageBean);
}
}
修改原有的dao
web层UserAction
public class UserAction extends ActionSupport{
private UserDao userDao=new UserDao();
/*
* 登录后成功跳转index.jsp
* */
public String login(HttpServletRequest req,HttpServletResponse resp) {
try {
// 系统中是否有当前用户
Map<String, Object> map =null;
try {
map= this.userDao.list(req.getParameterMap(), null).get(0);
} catch (Exception e) {
// TODO: handle exception
req.setAttribute("msg", "此用户不存在");
return "login";
}
// 有
if(map!=null && map.size()>0) {
// 查询用户的中间表,获取对应的menuId的集合
// [{Menuid:002,...},{Menuid:003...}]
// 002,003
StringBuilder sb=new StringBuilder();
List<Map<String, Object>> menuIdArrays = this.userDao.getMenuByUid(req.getParameterMap(), null);
for (Map<String, Object> m : menuIdArrays) {
sb.append(","+m.get("menuId"));
}
// ,002,003
req.setAttribute("menuIds", sb.substring(1));
return "index";
}else {
// 没有
req.setAttribute("msg", "此用户不存在");
return "login";
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/userAction.action?methodName=login" method="post">
uid:<input type="text" name="uid">
upwd:<input type="text" name="upwd">
<input type="submit">
<span style="color: red">${msg }</span>
</form>
</body>
</html>
在原有的jsp页面添加一个隐藏域,如果不使用隐藏域的话,js调用时会报错
<input type="hidden" id="menuIds" value="${menuIds }">
再在js中调用
url:'menuAction.action?methodName=menuTree&&Menuid='+$("#menuIds").val(),
展示页面,登录哪个用户就查看哪些权限:
某几个权限
所有权限