欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

easyui高级控件_01

程序员文章站 2024-02-03 22:01:28
...

1.首先思考:我们想一个用户对应多个菜单
然后一个菜单可以对应多个用户
其实这就是user与menu的多对多的关系

思路:
1、菜单不同的原因在于,利用不同menuid进行查询,原本默认查询的是所有菜单,是通过-1去查的;
2、menuid由来:是登录用户id查询中间表数据所得来的

下面进行代码验证:
先修改menuDao
这里增加了一个 listMenuSef,利用不同menuid进行查询,这样就不会影响其他的方法好了思路的第一步完成了

package com.zrh.dao;

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

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

public class MenuDao extends JsonBaseDao {
	/**
	 * 
	 * @param map   req.getParameterMap
	 * @param pageBean  分页
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	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>> 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);
		
	}
	/**
	 * 查询menu表的数据
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	//查东西不一定返回对象 可以返回map集合   JsonUtils是jquery里的工具类
	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
		String id = JsonUtils.getParamVal(map, "id");
		if(StringUtils.isNotBlank(id)) {
			sql = sql + " and parentid = "+id;
		}else {
			sql = sql + " and parentid = -1";
		}
		return super.executeQuery(sql, pageBean);
		
	}
	
	/**
	 * menu表的数据不符合easyui树形展示的数据格式
	 * 需要转换成easyUI所能识别的数据
	 * @param map
	 * @param treeNode
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	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);
		
		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);
	}
	
	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);
		}
		
	}

}

2、menuid由来:是登录用户id查询中间表数据所得来的
先写一个方法UserDao用来查询,第一个是登录查询用户表,第二个是通过中间表查询登录用户所对应的权限

package com.zrh.dao;

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

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

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 = 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
可以自动调用

package com.zrh.web;

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

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

import com.zking.framework.ActionSupport;
import com.zrh.dao.UserDao;

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) {
				//listMenu查出来的是中间表对应的所有权限
				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";
		
	}

}

去mvc.xml配置下,代码如下

<action path="/userAction" type="com.zrh.web.UserAction">
		<forward name="index" path="/index.jsp" redirect="false" />
	</action>

完了之后写一个登录界面

<%@ 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?methoName=login" method="post">
	uid:<input type="text" name="uid"><br>
	upwd:<input type="password" name="upwd"><br>
	<input type="submit" ><br>
</form>
</body>
</html>

输出结果为:
登录界面:
easyui高级控件_01

登录进去后,进入主界面:
easyui高级控件_01

总结:
分了权限之后,每个用户登录后都可以跳到不同界面去,解决了一个菜单不能对应多个用户的问题。