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

easyui高级控件(二)

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

easyui高级控件

1、思路:

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

2、实现的功能

1、根据用户登录的权限
2、显示用户权限功能

3、权限树

1、一星权限设计(用户权限多对一)
1.1执行数据库脚本
1.2建立实体类
1.3创建dao
1.4Web层创建
1.5更改展示的树形菜单

4、代码实现

1、MenuDao

package com.DZY.dao;

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

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

public class MenuDao extends JsonBaseDao {

	/**
	 * 给前台返回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;
	}
	
	/**
	 * [{'menuId':001,'menuName':'学生管理'},{{'menuId':001,'menuName':'后勤管理'}}]
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	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");
		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':'后勤管理'}}]
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	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=-1";
		}
//		这里面存放的是数据库中的菜单信息
		List<Map<String, Object>> listMap = super.executeQuery(sql, pageBean);
		return listMap;
	}
	
	/**
	 * {'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);
		}
	}
}

**2、UserAction **

package com.DZY.web;

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

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

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

public class UserAction extends ActionSupport {
	/*private UserDao userDao = new UserDao();
	 
	public String login(HttpServletRequest req,HttpServletResponse resp) {
		String code = "index";//默认跳转首页
		//登录:
		try {
			List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), null);
			//1.用户存在
			if(list != null && list.size() == 1) {
				List<Map<String, Object>> menuList = this.userDao.getMenusByUser(req.getParameterMap(), null);
				//查到之后 用拼接的写法将menuId拼接传递出去
				StringBuilder sb =new StringBuilder();
				for (Map<String, Object> map : menuList) {
					sb.append(","+map.get("menuId"));
				}
				System.out.println(sb.substring(1));
				//,001,002
				req.setAttribute("menuIds", sb.substring(1));
				
			}else {
				//2.用户不存在
				req.setAttribute("msg", "用户不存在");
				code = "login";//失败跳登录
			}
		}catch (Exception e) {
			// TODO: handle exception
			code = "login";//失败跳登录
		}
		
		return code;
		
	
		
	}*/
private UserDao userDao=new UserDao();
	
	public String login(HttpServletRequest req,HttpServletResponse resp) {
		String code="index";
		//登录
		try {
			List<Map<String, Object>> list=this.userDao.list(req.getParameterMap(), null);
			if(list != null && list.size()==1) {
			//用户存在
				List<Map<String, Object>> menuList=this.userDao.getMenusByUser(req.getParameterMap(), null);
				StringBuilder sb=new StringBuilder();
				for (Map<String, Object> map : menuList) {
					sb.append(","+map.get("menuId"));
				}
				req.setAttribute("menuIds", sb.substring(1));
			}else {
			//用户不存在
				req.setAttribute("msg", "用户不存在");
				code="login";
			}
		} catch (Exception e) {
			e.printStackTrace();
			code="login";
		}
		return code;
	}

}

3、mvc.xml

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

4、web.xml

  <filter>
  	<filter-name>encodingFiter</filter-name>
  	<filter-class>com.DZY.util.EncodingFiter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>encodingFiter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  <servlet>
  	<servlet-name>actionServlet</servlet-name>
  	<servlet-class>com.zking.framework.ActionServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>actionServlet</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>

5、index.jsp页面

<%@ 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">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/static/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/static/easyui5/themes/icon.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath }/static/easyui5/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/static/easyui5/jquery.easyui.min.js"></script>
 <script type="text/javascript"
	src="${pageContext.request.contextPath }/static/js/index.js"></script>
 

<title>Insert title here</title>
</head>
<body class="easyui-layout">
<input type="hidden" id="menuIds" value="${menuIds}">
	<div data-options="region:'north',border:false"
		style="height: 60px; background: #B3DFDA; padding: 10px">north
		region</div>
	<div data-options="region:'west',split:true,title:'West'"
		style="width: 150px; padding: 10px;">
		菜单管理框架
		<ul id="tt"></ul>  
		</div>
		
	<div
		data-options="region:'east',split:true,collapsed:true,title:'East'"
		style="width: 100px; padding: 10px;">east region</div>
	<div data-options="region:'south',border:false"
		style="height: 50px; background: #A9FACD; padding: 10px;">south
		region</div>
	<div data-options="region:'center',title:'Center'">
	<div id="menuTab" class="easyui-tabs" style="width:500px;height:250px;">   
            <div title="首页" style="padding:20px;display:none;">欢迎界面</div> </div>
</body>
</html>

6、login.jsp页面

<%@ 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>登陆界面</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/userAction.action?methodName=login"  method="post">
uid:<input  type="text"  name="uid"><br>
upwd:<input  type="text"  name="upwd"><br>
<input  type="submit" value="登陆">
</form>
</body>
</html>

实现两种不同权限:
第一种权限:
easyui高级控件(二)
easyui高级控件(二)
第二种权限
easyui高级控件(二)
easyui高级控件(二)