权限设计

2019年6月30日 37点热度 0条评论 来源: hou_yi_tao

二星权限设计(用户权限多对多)

1创建dao

package com.houyitao.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.houyitao.util.JsonBaseDao;
import com.houyitao.util.JsonUtils;
import com.houyitao.util.PageBean;
import com.houyitao.util.StringUtils;

public class UserDao extends JsonBaseDao {
	/**
	 * 用户登录或者查询用户分页信息的公共分页方法
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
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(uid)) {
		sql+=" and upwd="+upwd;
	}
	return super.executeQuery(sql, pageBean);
	
}

/**
 * 根据当前用户等的id查询对应的菜单
 * @param paMap
 * @param pageBean
 * @return
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws SQLException
 */
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);
	
}

}

2.修改原有的dao

package com.houyitao.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.houyitao.entity.TreeNode;
import com.houyitao.util.JsonBaseDao;
import com.houyitao.util.JsonUtils;
import com.houyitao.util.PageBean;
import com.houyitao.util.StringUtils;

public class MenuDao extends JsonBaseDao{
	private List<TreeNode> listTreeNode;
	/**
	 * 给前台返回tree_data1.json的字符串
	 * @param paMap  从前台jsp传递过来的参数集合
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<TreeNode> listTreeNode(Map<String,String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		
		List<Map<String, Object>> listMap = this.listMapAuth(paMap, pageBean);
		List<TreeNode> listTreeNode=new ArrayList<>();
		this.listMapTOListTreeNode(listMap, listTreeNode);
		return listTreeNode;
		
	}
	public List<Map<String, Object>> listMap(Map<String,String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select *  from t_easyui_menu where true ";
		String menuId= JsonUtils.getParamVal(paMap, "Menuid");
		if(StringUtils.isNotBlank(menuId)) {
			sql +=" and parentid="+menuId;
		}else {
			sql +=" and parentid=001";
		}
		//这里面放的是数据库中的菜单信息
		List<Map<String, Object>> listMap=super.executeQuery(sql, pageBean);
		return listMap;
	}
	
	public List<Map<String, Object>> listMapAuth(Map<String,String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select *  from t_easyui_menu where true ";
		String menuId= JsonUtils.getParamVal(paMap, "Menuid");
//		为什么将menuId改成menuId
//		原因在于之前的方法只能查询当前节点的所有子节点集合,不能将当前节点给查询出来
		if(StringUtils.isNotBlank(menuId)) {
			sql +=" and menuId in ("+menuId+")";
		}else {
			sql +=" and menuId=000";
		}
		//这里面放的是数据库中的菜单信息
		List<Map<String, Object>> listMap=super.executeQuery(sql, pageBean);
		return listMap;
	}
	/**
	 * [{'Menuid' : 001,'Menuname':'学生管理'},{
  {‘Menuid’:001,'Menuname':'后勤管理'}}]
	 * -->
	 * {id:...,text:...}
	 * @param map
	 * @param treenode
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private void mapTOTreeNode(Map<String, Object> map,TreeNode treenode) throws InstantiationException, IllegalAccessException, SQLException {
		treenode.setId(map.get("Menuid")+"");
		treenode.setText(map.get("Menuname")+"");
		treenode.setAttributes(map);
//		将子节点添加到父节点当中,建立数据之间的父子关系
//		treenode.setChildren(children);
		Map<String, String[]> childrenMap=new HashMap<>();
		childrenMap.put("Menuid", new String[] {treenode.getId()});
		List<Map<String, Object>> listMap = this.listMap(childrenMap, null);
		List<TreeNode> listTreeNode=new ArrayList<>();
		this.listMapTOListTreeNode(listMap, listTreeNode);
		treenode.setChildren(listTreeNode);
	}
	/**
	 * [{'Menuid' : 001,'Menuname':'学生管理'},{
  {‘Menuid’:001,'Menuname':'后勤管理'}}]
	 * -->
	 * tree_data1.json
	 * @param listMap
	 * @param listTreeNode
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private void listMapTOListTreeNode(List<Map<String, Object>> listMap,List<TreeNode> listTreeNode) throws InstantiationException, IllegalAccessException, SQLException {
		TreeNode treeNode=null;
		for (Map<String, Object> map : listMap) {
			treeNode=new TreeNode();
			mapTOTreeNode(map, treeNode);
			listTreeNode.add(treeNode);
		}
	}
	
}

3.新增web的方法

package com.houyitao.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.houyitao.dao.MenuDao;
import com.houyitao.dao.UserDao;
import com.houyitao.entity.TreeNode;
import com.houyitao.util.ResponseUtil;
import com.zking.framework.ActionSupport;

public class UserAction extends ActionSupport {
	private UserDao userDao=new UserDao();
	/**
	 * 登陆成功侯跳转index.jsp
	 * @param req
	 * @param resp
	 * @return
	 * @throws JsonProcessingException
	 * @throws Exception
	 */
	
	public String menuTree(HttpServletRequest req, HttpServletResponse resp) throws JsonProcessingException, Exception {
		try {
		//系统中是否有当前登陆用户
		Map<String,Object> map=null;
		try {
			map = this.userDao.list(req.getParameterMap(), null).get(0);
		} catch (Exception e) {
			return "login";
		}
		//有:
		//查询用户菜单中间表 获取对应的menuid集合
		if(map!=null&&map.size()>0) {
			StringBuilder sb=new StringBuilder();
			List<Map<String, Object>> menuIdArr = this.userDao.getMenuByUid(req.getParameterMap(), null);
		    for (Map<String, Object> m : menuIdArr) {
				sb.append(","+m.get("menuid"));
			}
		    req.setAttribute("menuIds", sb.substring(1));
			return "index";
		}else {
//			没有
			req.setAttribute("msg", "用户或密码输入错误");
			return "login";
		}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "login";
		}
		
	}
}

4.新增登入界面,跳入前端树形菜单

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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?methoName=login" method="post">
uid:<input  type="text" name="uid"/>
upwd:<input  type="text" name="upwd"/>
<input type="submit"/>
</form>
<span style="color: red">${msg}</span>
</body>
</html>

5.mvc.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/menuAction" type="com.houyitao.web.MenuAction">
	
	</action>
	
	<action path="/userAction" type="com.houyitao.web.UserAction">
		<forward name="index" path="/index.jsp" redirect="false" />
		<forward name="login" path="/login.jsp" redirect="false" />
	
	</action>
</config>

结果:


    原文作者:hou_yi_tao
    原文地址: https://blog.csdn.net/hou_yi_tao/article/details/94359589
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。