用户权限管理动态显示该用户的有什么权限菜单的.

此时需要 四张表 :

1 用户表(user)

2 菜单表(menu)

3 职位表(posts)

4 职位表和菜单表中间表(post_menu)

用户表不用多说里面都是个人信息,就是多一个他是什么职位的职位表的ID


职位表:



菜单表:

扫描二维码关注公众号,回复: 2287070 查看本文章



职位表和菜单表中间表



以上就是这几个表设计

其中该职位有什么权限需要在前端去设置一下,然后添加到中间表中.

添加页面


后台代码Controller:

public void update(Posts p,Integer[] mids,HttpServletResponse response)throws Exception{
		response.setContentType("text/html;charset=utf-8");	
		if(p.getPname().equals("系统管理员")){
			response.getWriter().write("<script>alert('小婊砸,超管岂是你说改就改的?!');location.href='posts_list.action';</script>");
		}else{
			int count=postsService.update(p);
			if(count>0){
				Integer pId = p.getPid();//得到post的id
				//先删除原来菜单再执行新增
				postMenuService.delPostMenu(pId);
				for (Integer mId : mids) {
					postMenuService.addPostMenu(pId, mId);
				}
				response.getWriter().write("<script>alert('添加成功!');location.href='posts_list.action';</script>");
			}else{
				response.getWriter().write("<script>alert('添加失败!');location.href='posts_list.action';</script>");
			}
		}
		
	}

其中把前台选择的职位和数组菜单添加到中间表中.

此时就是用这个用户去登录.

进入到登录页面,点击登陆后.

后台代码controller:

前面处理代码...
// ----------查询登录用户所对应的菜单集合-------------------
				Integer postId = users.getUpost();// 获得登录角色ID
				List<Menu> menus = postsService.getMenusByPostId(postId);
				session.setAttribute("menus", menus);// 用户菜单存Session

getMengByPostId()方法;

public List<Menu> getMenusByPostId(Integer postId) {
		List<Menu> menus = new ArrayList<Menu>();
		PostMenuExample example = new PostMenuExample();
		example.createCriteria().andPIdEqualTo(postId);
		//根据职位id查询出PostMenu集合
		List<PostMenu> pm = postMenuDao.selectByExample(example);
		for (PostMenu postMenu : pm) {
			Menu m = menuDao.selectByPrimaryKey(postMenu.getmId());
			if(m.getmDel()==0){
				menus.add(m);
			}
		}
		return menus;
	}

这就后台结束了.返回到前台一个menu信息.

前台处理:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
    <base href="<%=basePath%>">
        <meta http-equiv=content-type content="text/html; charset=utf-8" />
        <link href="css/admin.css" type="text/css" rel="stylesheet" />
         <script language=javascript>
         
            function expand(el)
            {
                childobj = document.getElementById("child" + el);

                if (childobj.style.display == 'none')
                {
                    childobj.style.display = 'block';
                }
                else
                {
                    childobj.style.display = 'none';
                }
                return;
            }
        </script>
    </head>
    <body background=img/menu_bg.jpg >
        <table height="100%" cellspacing=0 cellpadding=0 width=170   background=./img/menu_bg.jpg border=0>
            <tr>
                <td valign=top align=middle>
                    <table cellspacing=0 cellpadding=0 width="100%" border=0>
						<tr>
                            <td height=10></td>
						</tr>
					</table>
		<c:forEach items="${sessionScope.menus}" var="m" varStatus="sta">
				<c:if test="${m.mPareantid==0}">
                    <table cellspacing=0 cellpadding=0 width=150 border=0>
                        <tr height=22>
                            <td style="padding-left: 30px" background=./img/menu_bt.jpg>
							   <a class=menuparent onclick="expand(${sta.index})" href="javascript:void(0);">${m.mName}</a>
							 </td>
						</tr>
                        <tr height=4>
                            <td></td>
						</tr>
					</table>
				</c:if>
                    <table id="child${sta.index}" cellspacing=0 cellpadding=0  width=150 border=0>
                   
				<c:forEach  items="${sessionScope.menus}" var="c">
						    <c:if test="${m.mId==c.mPareantid}">
							<tr height=20>
	                            <td align=middle width=30>
									<img height=9 src="./img/menu_icon.gif" width=9>
								</td>
	                            <td>
									<a class=menuchild  href="${pageContext.request.contextPath}/${c.mUrl}"  target="right">
									   ${c.mName}
									</a>
								</td>
							</tr>
							</c:if>
				</c:forEach>
                        <tr height=4>
                            <td colspan=2></td>
						</tr>
					</table>
      </c:forEach>
				</td>
                <td width=1 bgcolor=#d1e6f7></td>
            </tr>
            
   </table>
    </body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_32230309/article/details/81019042