easyui-- Rights Management

1, the purpose of permissions:  

   It is to allow different users can operate the system in different resources
   directly point that different users can operate different menus 
 
  core: realize menu privileges core idea is to control the user logs back menuId passed (and the associated segment tree menu categories are listed user information column section)

 

2, Award permission design (many-user rights)

2.1, the implementation of database scripts

User login information table t_easyui_user_version2

Intermediate user menu table t_easyui_usermenu

 

On the menu management table refer to a: easyUI-- entry example

 


2.2 The establishment of the entity class  

 1 public class TreeNode {
 2   private String id;
 3   private String text;
 4   private  List<TreeNode> children=new ArrayList<TreeNode>();
 5   private  Map<String, Object> attributes=new HashMap<String, Object>();
 6   
 7   
 8 public String getId() {
 9     return id;
10 }
11 
12 public void setId(String id) {
13     this.id = id;
14 }
15 
16 public String getText() {
17     return text;
18 }
19 
20 public void setText(String text) {
21     this.text = text;
22 }
23 
24 public List<TreeNode> getChildren() {
25     return children;
26 }
27 
28 public void setChildren(List<TreeNode> children) {
29     this.children = children;
30 }
31 
32 public Map<String, Object> getAttributes() {
33     return attributes;
34 }
35 
36 public void setAttributes(Map<String, Object> attributes) {
37     this.attributes = attributes;
38 }
39 
40 @Override
41 public String toString() {
42     return "TreeNode [id=" + id + ", text=" + text + ", children=" + children + ", attributes=" + attributes + "]";
43 }
44   
45 
46 }

 

2.3 创建 dao UserDao

Package com.yuan.dao; 

Import java.sql.SQLException;
 Import java.util.List;
 Import a java.util.Map; 

Import com.yuan.util.JsonBaseDao;
 Import com.yuan.util.JsonUtils;
 Import com.yuan .util.PageBean;
 Import com.yuan.util.StringUtils; 

public  class UserDao the extends JsonBaseDao { 

    / ** 
     public method * user login user paging information or queries 
     * @param PAMAP 
     * @param pageBean 
     * @return 
     * @throws an InstantiationException is 
     *@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 (upwd)) { 
            SQL + = "the AND upwd =" + upwd; 
        } 
        return  Super .executeQuery (SQL, pageBean); 
    } 
    
    / ** 
     * The ID of the current logged in user to query corresponding to the menu 
     * @ param PAMAP 
     * @param pageBean 
     * @return 
     * @throws an InstantiationException is 
     * @throws IllegalAccessException 
     * @throws SQLException
      * / 
    public List <the Map <String, Object >> getMenuByUid (the 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.4 modify the original dao MenuDao

The main modification is to modify the method to listMapAuth listMap,

.listMap method is only responsible for querying the child nodes, do not include itself

.ListMapAuth child node query method can include its own node

Package com.hmc.dao; 

Import java.sql.SQLException;
 Import of java.util.ArrayList;
 Import the java.util.HashMap;
 Import java.util.List;
 Import a java.util.Map; 

Import com.hmc.entity.TreeNode ;
 Import com.hmc.util.JsonBaseDao;
 Import com.hmc.util.JsonUtils;
 Import com.hmc.util.PageBean;
 Import com.hmc.util.StringUtils; 

public  class MenuDao the extends JsonBaseDao { 
    
    
    / ** 
     * to the front tree_data1_json string 
     * @param parameter paMap jsp pass from the front desk over the collection 
     *@param pageBean
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws SQLException
     */
    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<TreeNode>();
       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=-1"; // } // // //这里面存放的是数据库中的菜单信息 // 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"); if(StringUtils.isNotBlank (menuId)) { SQL + = "in the AND menuId (" + menuId + ")" ; } the else { SQL + = "the AND menuId = 001" ; } // This menu information which is stored in the database List <the Map <String, Object >> listMap = Super .executeQuery (SQL, pageBean); return listMap; } / ** * { 'menuid': 001, 'Menuame': 'student management'} * {ID: .. , text: ...} * @param map * @param treeNode * @throws InstantiationException * @throwsIllegalAccessException * @throws SQLException * / Private void MapToTreeNode (the Map <String, Object> Map, the TreeNode the treeNode) throws an InstantiationException is, IllegalAccessException, SQLException { treeNode.setId (as map.get ( "menuid") + "" ); treeNode.setText ( as map.get ( "MENUNAME") + "" ); treeNode.setAttributes (Map); // add a child node to a parent node among the established parent-child relationships between data // treeNode.setChildren (Children); the Map <String , String []> = childrenMap new new the 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,'Menuame':'学生管理'},{'Menuid':002,'Menuame':'后勤管理'}] * @param listMap * tree_data1_json * @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); } } }

 

New web 2.5 approach

 

package com.yuan.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.yuan.dao.UserDao;
import com.***.framework.ActionSupport;

public class UserAction extends ActionSupport {
    
    private UserDao userDao = new UserDao();
    /**
     * 登录成功后跳转index.jsp
     * @param request
     * @param response
     *@return 
     * / 
    public String Login (the HttpServletRequest Request, the HttpServletResponse Response) {
 //         if there is currently logged on user system 
        the Map <String, Object> Map = null ;
         the try { 
            Map = the this .userDao.list (request.getParameterMap (), null ) .get (0 ); 
        } the catch (Exception E) { 
            request.setAttribute ( "MSG", "user absent" );
             return "Login" ; 
        } 
        the try {
             // have to query the user menu on the intermediate table, to acquire a corresponding menuid set of 
            IF (the Map! =null&&map.size()>0) {
                //得到[{Menuid:002,...},{Menuid:003,...}]
                //截成002,003
                StringBuilder sb= new StringBuilder();
                List<Map<String, Object>> menuByUid = this.userDao.getMenuByUid(request.getParameterMap(), null);
                for (Map<String, Object> m : menuByUid) {
                    sb.append(","+m.get("menuId"));
                }
                request.setAttribute("menuIds", sb.substring(1));
                 return"index" ; 
            } the else {
                 // did not jump back on the re-login screen and prompts the user does not exist 
                request.setAttribute ( "msg", "user does not exist" );
                  return "the Login" ; 
            } 
        } the catch (InstantiationException | IllegalAccessException | E SQLException) {
             // the TODO Auto-Generated Block the catch 
            e.printStackTrace ();
              return "Login" ; 
        } 
        
    }      
    
}

 

2.6 Add the login screen, the front end jump menu tree

 

<%@ 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?methodName=login" method="post">
   uid:<input type="text" name="uid">
   upwd:<input type="text" name="upwd">
   <input type="submit">
   <span style="color:red;">${msg }</span>
</form>
</body>
</html>

 

Page is relatively simple, self-optimization.

 

2.7 modify js code index.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 = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>';
            if($('#menuTab').tabs('exists',node.text)) {
                
            }, Node.text);
                $ ( '# menuTab') tabs ( 'SELECT'.the presence of the selected tab is performed prior operation tab//
                the else { 
                $ ( '#menuTab') tabs ( 'the Add'. , {    
                     // operations performed in the absence of additional 
                    title: Node.text,     
                    Content: Content,     
                    the closable: to true ,     
                });   
            } 

        } 
    });   

})

We need to add one line of code at the time of acceptance of menuid web layer pass over a user logs in the original index.jsp page when modifying url

<input type="hidden" id="menuIds" value="${menuIds }" >

 

2.8 configuration file mvc.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <!-- <action path="/regAction" type="test.RegAction">
        <forward name="failed" path="/reg.jsp" redirect="false" />
        <forward name="success" path="/login.jsp" redirect="true" />
    </action> -->
    
    <action path="/menuAction" type="com.yuan.web.MenuAction">
    </action>
    <action path="/userAction" type="com.yuan.web.UserAction">
      <forward name="index" path="/index.jsp" redirect="false"></forward>
      <forward name="login" path="/login.jsp" redirect="false"></forward>
    </action>
    
</config>

 

3, operating results

3.1 login screen

3.2 001 User Login

 

3.3 002 User Login

 

3.3 003 User Login

 

3.4 000 User Login

 

Thank you for watching ^ - ^! ! !

 

Guess you like

Origin www.cnblogs.com/ly-0919/p/11110954.html