easyui(高级控件2)

为什么要权限:


    权限目的:
        是为了让不同的用户可以操作系统中不同资源
    直接点说就是不同的用户可以看到左侧不同的菜单    

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

dao层:

MenuDao:

package com.lww.dao;

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

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

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

UserDao

package com.lww.dao;

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

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

public class UserDao extends JsonBaseDao {
    
    /**
     * 用户登录或者查询用户分页信息的公共方法
     */
    public List> list(Map 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> getMenuByUid(Map 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);
    }
    
}

UserAction:

package com.lww.web;

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

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

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lww.dao.UserDao;
import com.lww.entity.TreeNode;
import com.lww.framework.ActionSupport;
import com.lww.util.ResponseUtil;

public class UserAction extends ActionSupport {
    private UserDao userDao = new UserDao();
    
    /**
     * 登录成功后跳转index.jsp

     */
    public String login(HttpServletRequest req,HttpServletResponse resp) {
//        系统中是否有当前用户
        try {
            Map map = null;
            try {
                map = this.userDao.list(req.getParameterMap(), null).get(0);
            } catch (Exception e) {
                req.setAttribute("msg", "用户不存在");
                return "login";
            }

//        查询用户菜单中间表,获取对应的menuid的集合
            if(map != null && map.size() > 0) {
                StringBuilder sb = new StringBuilder();
                List> menuIdArr = this.userDao.getMenuByUid(req.getParameterMap(), null);
                for (Map m : menuIdArr) {
                    sb.append(","+m.get("menuId"));
                }
                req.setAttribute("menuIds", sb.substring(1));
                return "index";
            }else {
                req.setAttribute("msg", "用户不存在");
                return "login";
            }
            
        } catch (InstantiationException | IllegalAccessException | SQLException e) {
            e.printStackTrace();
        }
        
        
        return null;
    }
}
 

index.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




后台主界面
  

  
  

 



    

north region

    

        菜单管理
        
     
        

        
    east region

        
    south region

        

        
        
        
        


    login.jsp页面:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>




    Insert title here



        uid:

        upwd:

        

    ${msg}

     

    js:

    $(function(){
        $('#tt').tree({    
            url:'menuAction.action?methodName=menuTree&&Menuid= '+$("#menuIds").val(),
            onClick: function(node){
    //            alert(node.text);   在用户点击的时候提示
                // add a new tab panel
                var content = '';
                if($('#menuTab').tabs('exists',node.text)){
    //                存在,执行选项卡选中已有选项卡的操作
                    $('#menuTab').tabs('select',node.text);
                }else{
    //                不存在,执行新增的操作
                    $('#menuTab').tabs('add',{    
                        title:node.text,    
                        content:content,    
                        closable:true
                    }); 
                }

                }

        });
        
    })
     

    mvc.xml:



        
        

        
        
            
            
        

     

    效果:

    000用户:

    easyui(高级控件2)_第1张图片

     

    111用户:

    easyui(高级控件2)_第2张图片

    easyui(高级控件2)_第3张图片

     

     

     

    你可能感兴趣的:(easyui(高级控件2))