权限菜单动态管理-自用备份

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了权限菜单动态管理-自用备份相关的知识,希望对你有一定的参考价值。

 

菜单分级管理

技术分享图片
<div id="menu">
    <ul>
        <li id="end"><em></em><a href=‘javascript:void(0);‘>信息管理</a>
            <ul>
                <c:forEach items="${menu1}" var="menu1">
                    <li id="end"><em></em><a href="${menu1.action}">${menu1.menuName}</a>
                        <ul>
                            <c:forEach items="${menu2}" var="menu2">
                                <c:if test="${menu1.age1 == menu2.age1 }">
                                    <li id="end"><em></em><a href="${menu2.action}">${menu2.menuName}</a>
                                        <ul>
                                            <c:forEach items="${menu3}" var="menu3">
                                                <c:if test="${menu2.age1 == menu3.age1 && menu2.age2==menu3.age2}">
                                                    <li id="end"><a href="${menu3.action}">${menu3.menuName}</a></li>
                                                </c:if>
                                            </c:forEach>
                                        </ul></li>
                                </c:if>
                            </c:forEach>
                        </ul></li>
                </c:forEach>
            </ul></li>
    </ul>
</div>
View Code
技术分享图片
<!-- 折叠菜单js -->
<script type="text/javascript">
    (function(e) {
        for (var _obj = document.getElementById(e.id).getElementsByTagName(
                e.tag), i = -1, em; em = _obj[++i];) {
            em.onclick = function() { //onmouseover 
                var ul = this.nextSibling;
                if (!ul) {
                    return false;
                }
                ul = ul.nextSibling;
                if (!ul) {
                    return false;
                }
                if (e.tag != ‘a‘) {
                    ul = ul.nextSibling;
                    if (!ul) {
                        return false;
                    }
                } //a 标签控制 隐藏或删除该行 
                for (var _li = this.parentNode.parentNode.childNodes, n = -1, li; li = _li[++n];) {
                    if (li.tagName == "LI") {
                        for (var _ul = li.childNodes, t = -1, $ul; $ul = _ul[++t];) {
                            switch ($ul.tagName) {
                            case "UL":
                                $ul.className = $ul != ul ? ""
                                        : ul.className ? "" : "off";
                                break;
                            case "EM":
                                $ul.className = $ul != this ? ""
                                        : this.className ? "" : "off";
                                break;
                            }
                        }
                    }
                }
            }
        }
    })({
        id : ‘menu‘,
        tag : ‘em‘
    });
</script>
View Code

sql

技术分享图片
DROP TABLE IF EXISTS `menu_info`;
CREATE TABLE `menu_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `menuname` varchar(20) DEFAULT NULL COMMENT 菜单名,
  `trcode` varchar(6) DEFAULT NULL COMMENT 菜单编号,
  `submenu` varchar(2) DEFAULT NULL COMMENT 是否有子菜单,
  `level` int(1) DEFAULT NULL COMMENT 等级,
  `action` varchar(50) DEFAULT NULL,
  `age1` varchar(2) DEFAULT NULL,
  `age2` varchar(2) DEFAULT NULL,
  `age3` varchar(2) DEFAULT NULL,
  `other` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of menu_info
-- ----------------------------
INSERT INTO `menu_info` VALUES (1, 书籍信息列表, 010000, Y, 1, ShowServlet?method=ulist, 01, 00, 00, 12);
INSERT INTO `menu_info` VALUES (2, 书籍信息管理, 010100, Y, 2, ShowServlet?method=ulist, 01, 01, 00, ‘‘);
INSERT INTO `menu_info` VALUES (3, 书籍信息添加, 010101, N, 3, ShowServlet?method=ulist, 01, 01, 01, null);
INSERT INTO `menu_info` VALUES (4, 书籍信息删除, 010102, N, 3, ShowServlet?method=ulist, 01, 01, 02, null);
INSERT INTO `menu_info` VALUES (5, 书籍信息编辑, 010103, N, 3, ShowServlet?method=ulist, 01, 01, 03, null);
INSERT INTO `menu_info` VALUES (6, 用户信息列表, 020000, Y, 1, LoginServlet?method=admin_manage, 02, 00, 00, null);
INSERT INTO `menu_info` VALUES (7, 用户信息管理, 020100, Y, 2, LoginServlet?method=admin_manage, 02, 01, 00, null);
INSERT INTO `menu_info` VALUES (8, 用户信息添加, 020101, N, 3, LoginServlet?method=admin_manage, 02, 01, 01, ‘‘);
INSERT INTO `menu_info` VALUES (9, 用户信息删除, 020102, N, 3, LoginServlet?method=admin_manage, 02, 01, 02, null);
INSERT INTO `menu_info` VALUES (10, 用户信息编辑, 020103, N, 3, LoginServlet?method=admin_manage, 02, 01, 03, null);
INSERT INTO `menu_info` VALUES (11, 备注1, 020200, Y, 2, LoginServlet?method=admin_manage, 02, 02, 00, 12);
INSERT INTO `menu_info` VALUES (12, 菜单信息列表, 030000, Y, 1, LoginServlet?method=munu_manage, 03, 00, 00, null);
INSERT INTO `menu_info` VALUES (17, 菜单信息管理, 030100, Y, 2, LoginServlet?method=munu_manage, 03, 01, 00, null);
INSERT INTO `menu_info` VALUES (18, 菜单信息添加, 030101, N, 3, LoginServlet?method=munu_manage, 03, 01, 01, null);
INSERT INTO `menu_info` VALUES (19, 菜单信息删除, 030102, N, 3, LoginServlet?method=munu_manage, 03, 01, 02, null);
INSERT INTO `menu_info` VALUES (20, 菜单信息编辑, 030103, N, 3, LoginServlet?method=munu_manage, 03, 01, 03, null);
View Code
技术分享图片
DROP TABLE IF EXISTS `person_info`;
CREATE TABLE `person_info` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `login_name` varchar(20) NOT NULL COMMENT 登录名,
  `password` varchar(50) NOT NULL,
  `post_id` int(20) NOT NULL,
  `other` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `login_name` (`login_name`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of person_info
-- ----------------------------
INSERT INTO `person_info` VALUES (1, user, 511B0D5F341BDDBD9A5348923B48D14C, 2, 普通管理员1);
INSERT INTO `person_info` VALUES (2, admin, 21232F297A57A5A743894A0E4A801FC3, 1, 超级管理员);
INSERT INTO `person_info` VALUES (3, admin01, 18C6D818AE35A3E8279B5330EDA01498, 1, 1);
View Code
技术分享图片
DROP TABLE IF EXISTS `work_menu`;
CREATE TABLE `work_menu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `work_id` varchar(255) DEFAULT NULL,
  `trcode` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of work_menu
-- ----------------------------
INSERT INTO `work_menu` VALUES (1, 1, 010000);
INSERT INTO `work_menu` VALUES (2, 1, 010100);
INSERT INTO `work_menu` VALUES (3, 1, 010101);
INSERT INTO `work_menu` VALUES (4, 1, 010102);
INSERT INTO `work_menu` VALUES (5, 1, 010103);
INSERT INTO `work_menu` VALUES (6, 1, 020000);
INSERT INTO `work_menu` VALUES (7, 1, 020100);
INSERT INTO `work_menu` VALUES (8, 1, 020101);
INSERT INTO `work_menu` VALUES (9, 1, 020102);
INSERT INTO `work_menu` VALUES (10, 1, 020103);
INSERT INTO `work_menu` VALUES (11, 1, 020200);
INSERT INTO `work_menu` VALUES (12, 1, 030000);
INSERT INTO `work_menu` VALUES (13, 1, 030100);
INSERT INTO `work_menu` VALUES (14, 1, 030101);
INSERT INTO `work_menu` VALUES (15, 1, 030102);
INSERT INTO `work_menu` VALUES (16, 1, 030103);
INSERT INTO `work_menu` VALUES (17, 2, 010000);
INSERT INTO `work_menu` VALUES (18, 2, 010100);
INSERT INTO `work_menu` VALUES (19, 2, 010101);
INSERT INTO `work_menu` VALUES (20, 2, 010102);
INSERT INTO `work_menu` VALUES (21, 2, 010103);
INSERT INTO `work_menu` VALUES (22, null, null);
View Code

menu方法

技术分享图片
package com.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dao.MenuDao;
import com.db.DBConn;
import com.db.impl.DBConnJdbcProp;
import com.entity.Menu_info;

public class MenuDaoImpl extends BaseDaoImpl<Menu_info> implements MenuDao{

    @Override
    public List<Menu_info> getTop(String login_name) {
        DBConn db = new DBConnJdbcProp();
        Connection con=db.getConn();
        String sql = "SELECT mi.* FROM work_menu wm, menu_info mi, person_info pi "
                    + "WHERE wm.trcode = mi.trcode "
                    + "AND wm.work_id = pi.post_id "
                    + "AND mi. LEVEL = 1 "
                    + "AND pi.login_name= ‘"+login_name+"‘";
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        List<Menu_info> top =null;
        try {
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            top = new ArrayList<Menu_info>();
            while(rs.next()){
                Menu_info menu = new Menu_info();
                menu.setId(rs.getInt("id"));
                menu.setMenuName(rs.getString("menuName"));
                menu.setTrcode(rs.getString("trcode"));
                menu.setSubmenu(rs.getString("submenu"));
                menu.setLevel(rs.getInt("level"));
                menu.setAction(rs.getString("action"));
                menu.setAge1(rs.getString("age1"));
                menu.setAge2(rs.getString("age2"));
                menu.setAge2(rs.getString("age2"));
                menu.setOther(rs.getString("other"));
                top.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return top;
    }
    
    
    public List<Menu_info> getSec(String login_name) {
        DBConn db = new DBConnJdbcProp();
        Connection con=db.getConn();
        String sql = "SELECT mi.* FROM work_menu wm, menu_info mi, person_info pi "
                + "WHERE wm.trcode = mi.trcode "
                + "AND wm.work_id = pi.post_id "
                + "AND mi. LEVEL = 2 "
                + "AND pi.login_name= ‘"+login_name+"‘";
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        List<Menu_info> sec =null;
        try {
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            sec = new ArrayList<Menu_info>();
            while(rs.next()){
                Menu_info menu = new Menu_info();
                menu.setId(rs.getInt("id"));
                menu.setMenuName(rs.getString("menuName"));
                menu.setTrcode(rs.getString("trcode"));
                menu.setSubmenu(rs.getString("submenu"));
                menu.setLevel(rs.getInt("level"));
                menu.setAction(rs.getString("action"));
                menu.setAge1(rs.getString("age1"));
                menu.setAge2(rs.getString("age2"));
                menu.setAge2(rs.getString("age2"));
                menu.setOther(rs.getString("other"));
                sec.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return sec;
    }    
    
    
    public List<Menu_info> getThird(String login_name) {
        DBConn db = new DBConnJdbcProp();
        Connection con=db.getConn();
        String sql = "SELECT mi.* FROM work_menu wm, menu_info mi, person_info pi "
                + "WHERE wm.trcode = mi.trcode "
                + "AND wm.work_id = pi.post_id "
                + "AND mi. LEVEL =3 "
                + "AND pi.login_name= ‘"+login_name+"‘";
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        List<Menu_info> third =null;
        try {
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            third = new ArrayList<Menu_info>();
            while(rs.next()){
                Menu_info menu = new Menu_info();
                menu.setId(rs.getInt("id"));
                menu.setMenuName(rs.getString("menuName"));
                menu.setTrcode(rs.getString("trcode"));
                menu.setSubmenu(rs.getString("submenu"));
                menu.setLevel(rs.getInt("level"));
                menu.setAction(rs.getString("action"));
                menu.setAge1(rs.getString("age1"));
                menu.setAge2(rs.getString("age2"));
                menu.setAge2(rs.getString("age2"));
                menu.setOther(rs.getString("other"));
                third.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return third;
    }        
    
}
View Code
技术分享图片
            MenuDao dao = new MenuDaoImpl();
            List<Menu_info> menu1 = dao.getTop(login_name);
            session.setAttribute("menu1", menu1);

            List<Menu_info> menu2 = dao.getSec(login_name);
            session.setAttribute("menu2", menu2);

            List<Menu_info> menu3 = dao.getThird(login_name);
            session.setAttribute("menu3", menu3);
View Code

修改

技术分享图片
<body>
<a href="ShowServlet?method=page_list">返回</a>
    <table align="center">
        <tr>
            <th>ID</th>
            <th>菜单名</th>
            <th>trcode</th>
            <th>submenu</th>
            <th>菜单等级</th>
            <th>链接</th>
            <th>分类1</th>
            <th>分类2</th>
            <th>分类3</th>
            <th>备注</th>
            <th>操作</th>
        </tr>    
    <c:forEach items="${menulist}" var="menulist">
        <tr>
            <td>${menulist.id}</td>
            <td>${menulist.menuName}</td>
            <td>${menulist.trcode}</td>
            <td>${menulist.submenu}</td>
            <td>${menulist.level}</td>
            <td>${menulist.action}</td>
            <td>${menulist.age1}</td>
            <td>${menulist.age2}</td>
            <td>${menulist.age3}</td>
            <td>${menulist.other}</td>
            <td>
                <a href="ShowServlet?method=menu_form&id=${menulist.id}">修改</a>
                <a href="ShowServlet?method=menu_delete&id=${menulist.id}">删除</a>
            </td>
        </tr>
    </c:forEach>
    </table>  
</body>
View Code
技术分享图片
<form action="ShowServlet?method=menu_update" method="post">
<input type="hidden" name="id" value="${menu_update.id}">
    <table align="center">        
        <tr>
            <td>menuName</td>
            <td><input type="text" name="menuName" value="${menu_update.menuName}"></td>
        </tr>        
        <tr>
            <td>trcode</td>
            <td><input type="text" name="trcode" value="${menu_update.trcode}"></td>
        </tr>
        <tr>
            <td>submenu</td>
            <td><input type="text" name="submenu" value="${menu_update.submenu}"></td>
        </tr>
        <tr>
            <td>level</td>
            <td><input type="text" name="level" value="${menu_update.level}"></td>
        </tr>        
        <tr>
            <td>action</td>
            <td><input type="text" name="action" value="${menu_update.action}"></td>
        </tr>        
        <tr>
            <td>age1</td>
            <td><input type="text" name="age1" value="${menu_update.age1}"></td>
        </tr>        
        <tr>
            <td>age2</td>
            <td><input type="text" name="age2" value="${menu_update.age2}"></td>
        </tr>    
        <tr>
            <td>age3</td>
            <td><input type="text" name="age3" value="${menu_update.age3}"></td>
        </tr>
        <tr>
            <td>other</td>
            <td><input type="text" name="other" value="${menu_update.other}"></td>
        </tr>                            
        <tr>
            <td><button type="reset">重置</button></td>
            <td><button type="submit">提交</button></td>
        </tr>                
    </table>

</form>
View Code
技术分享图片
else if("menu_form".equals(method)){
            
            menu = mbiz.get(Integer.parseInt(id));
            session.setAttribute("menu_update", menu);
            response.sendRedirect("menu_update.jsp");
            
        }else if("menu_update".equals(method)){
            
            String menuname = request.getParameter("menuName");
            String trcode = request.getParameter("trcode");
            String submenu = request.getParameter("submenu");
            String level = request.getParameter("level");
            String action = request.getParameter("action");
            String age1 = request.getParameter("age1");
            String age2 = request.getParameter("age2");
            String age3 = request.getParameter("age3");
            String other = request.getParameter("other");
            
            menu.setId(Integer.parseInt(id));
            menu.setMenuName(menuname);
            menu.setTrcode(trcode);
            menu.setSubmenu(submenu);
            menu.setLevel(Integer.parseInt(level));
            menu.setAction(action);
            menu.setAge1(age1);
            menu.setAge2(age2);
            menu.setAge3(age3);
            menu.setOther(other);
            int rs = mbiz.update(menu);
            if(rs>0){
                request.getRequestDispatcher("LoginServlet?method=munu_manage").forward(request, response);
                return;
            }else{
                request.getRequestDispatcher("LoginServlet?method=munu_manage").forward(request, response);
                return;
            }
        }
View Code

 

以上是关于权限菜单动态管理-自用备份的主要内容,如果未能解决你的问题,请参考以下文章

Django 权限管理-后台根据用户权限动态生成菜单

项目一:第十三天 1菜单数据管理 2权限数据管理 3角色数据管理 4用户数据管理 5在realm中动态查询用户权限,角色 6Shiro中整合ehcache缓存权限数据

GitHub掘金:系统权限管理SpringSecurity实现动态权限菜单控制

Vue权限控制——动态注册路由

Vue权限控制——动态注册路由

ABP开发框架前后端开发系列---(11)菜单的动态管理