版权声明:仅供学习 https://blog.csdn.net/qq_41304019/article/details/82967775
今天带来的是我自己最近学的easyui的三星管理权限的一个登陆
要求:不同的用户登陆拥有的菜单树是不同的。
先上一个逻辑图缕下思路吧(数据库表结构可参照下图)
权限树
?执行数据库脚本
?建立实体类
?创建dao
?新增web的方法
?新增登入界面,跳入前端树形菜单
所谓权限:指的是系统中的资源,资源包括菜单资源(学习情况报表,账号审核...)以及按钮资源
所谓角色:指的是系统中的权限集合(1)
menuTree展示的相关代码(利用递归)
package com.zking.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.zking.entity.TreeNode;
import com.zking.util.BaseJsonDao;
import com.zking.util.JsonUtil;
import com.zking.util.PageBean;
import com.zking.util.StringUtils;
public class MenuTreeDao extends BaseJsonDao{
/**
* 查询后台需要属性展示的菜单表数据(所有表字段)
* 注意:该数据转换成json对象,是不符合easyui的tree组件展现的json格式
* @param paramMap
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<Map<String, Object>> menuList(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from t_easyui_menu where true";
//当前节点的id
String menuId = JsonUtil.getParamMap(paramMap, "Menuid");
if(StringUtils.isNotBlank(menuId)) {
sql += " and parentid in ("+menuId+")";
}else {
sql += " and parentid=-1";
}
return super.executeQuery(sql, pageBean);
}
/**
* 直接查出来的数据源是不能直接用于展示的,需要转换成可展示数据
* @param map
* @param treeNode
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void mapToTreeNode(Map<String, Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
//查出来的数据通过map集合赋值给treeNode实体类
treeNode.setId(map.get("Menuid").toString());
treeNode.setText(map.get("Menuname").toString());
treeNode.setAttributes(map);
Map<String, String[]> paraMap = new HashMap<>();
//把当前节点的id当作父id,查出所有的子节点
paraMap.put("Menuid", new String[] {treeNode.getId()});
List<Map<String, Object>> menuList = this.menuList(paraMap, null);
List<TreeNode> treeNodeList = new ArrayList<>();
mapListToTreeNodeList(menuList, treeNodeList);
//设置子节点
treeNode.setChildren(treeNodeList);
}
/**
*
* @param list 用来展示的
* @param treeNodeList 数据库查出来的数据
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void mapListToTreeNodeList(List<Map<String, Object>> list,List<TreeNode> treeNodeList) throws InstantiationException, IllegalAccessException, SQLException {
TreeNode treeNode = null;
for (Map<String, Object> map : list) {
treeNode = new TreeNode();
mapToTreeNode(map, treeNode);
treeNodeList.add(treeNode);
}
}
/**
* 这个的方法的返回值才是符合easyui的tree组件所需要的json格式
* @param paramMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<TreeNode> menuTreeList(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
List<Map<String, Object>> menuList = this.menuList(paramMap, pageBean);
List<TreeNode> treeNodeList = new ArrayList<>();
mapListToTreeNodeList(menuList, treeNodeList);
return treeNodeList;
}
}
我们把这个展示菜单树的dao包写好,在继续写登陆相关的代码
/**
* 三星权限用户登陆
* @param paramMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> tologin3(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String uid = JsonUtil.getParamMap(paramMap, "uid");
String upwd = JsonUtil.getParamMap(paramMap, "upwd");
String sql = "select * from t_easyui_user_version2 where true";
if(StringUtils.isNotBlank(uid)) {
sql += " and uid="+uid;
}
if(StringUtils.isNotBlank(upwd)) {
sql += " and upwd="+upwd;
}
return super.executeQuery(sql, null);
}
public List<Map<String, Object>> getMenuidFromUser2(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String zid = JsonUtil.getParamMap(paramMap, "zid");
String sql = "select * from t_juese where true";
if(StringUtils.isNotBlank(zid)) {
sql += " and zid="+zid;
}
return super.executeQuery(sql, null);
}
再来实现action层代码(登陆的action)
/**
* 三星权限的登陆
* @param req
* @param resp
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public String tologin3(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
List<Map<String, Object>> tologin3 = this.userDao.tologin3(req.getParameterMap(), null);
Map<String, Object> currentUser = tologin3.get(0);
if(null != currentUser) {
Map<String, String[]> paramMap = new HashMap<>();
paramMap.put("zid", new String[] {(String) currentUser.get("zid")});
//获取的中间表信息
List<Map<String, Object>> menuidFromUser = this.userDao.getMenuidFromUser2(paramMap, null);
StringBuffer sb = new StringBuffer();
for (Map<String, Object> map : menuidFromUser) {
sb.append(",").append(map.get("menuId"));
}
req.setAttribute("menuId", sb.toString().substring(1));
}
return "index";
}
menuTree的action
package com.zking.Action;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zking.dao.MenuTreeDao;
import com.zking.entity.TreeNode;
import com.zking.framework.ActionSupport;
import com.zking.util.ResponseUtil;
public class MenuTreeAction extends ActionSupport {
private MenuTreeDao menuTreeDao = new MenuTreeDao();
public String menuTreeList(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException, JsonProcessingException, IOException {
List<TreeNode> menuTreeList = this.menuTreeDao.menuTreeList(req.getParameterMap(), null);
ObjectMapper om = new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(menuTreeList));
return null;
}
}
完成以上代码一个三星管理登陆的权限就好了