很多時(shí)候不同的人擁有不同的權(quán)限,不同的人擁有不同的菜單。
那怎么樣做一個(gè)可以靈活變動(dòng)的菜單呢。
大致思路:
建2張數(shù)據(jù)表:Module(保存著所有的菜單)和User(里面有一個(gè)“權(quán)限”字段保存著自己需要的菜單);
先獲得所有菜單和用戶菜單,所有的主菜單和子菜單
代碼片段如下:
顯示部分jsp:
注意window.location.href="user.portal?action=viewUserPermissionSubmit"+"&permissions="+values+"&id="+${requestScope.id};
這里values就是修改后的菜單id,形如:[2][3][5]...把它傳到后臺(tái)修改User表的權(quán)限字段并刷新頁(yè)面就可以顯示修改后的菜單了。
那怎么樣做一個(gè)可以靈活變動(dòng)的菜單呢。
大致思路:
建2張數(shù)據(jù)表:Module(保存著所有的菜單)和User(里面有一個(gè)“權(quán)限”字段保存著自己需要的菜單);
先獲得所有菜單和用戶菜單,所有的主菜單和子菜單
代碼片段如下:
else if("viewUserPermission".equals(action)){
System.out.println("--------------------viewUserPermission-------------------");
String id=request.getParameter("id");
User instance=(User)this.getBaseService().get("User",id);
// 初始化菜單,根據(jù)MenuLevel=0代表根目錄從0開始
List list = this.getModuleService().getModuleTree(Integer.parseInt(instance.getMenuLevel()));//getModuleTree里的參數(shù)一般是0
Iterator menu = list.iterator();
Vector parentV = new Vector();
Vector childV = new Vector();
Module module;
String allMenu="";
while (menu.hasNext()) {
module = (Module) menu.next();
if (module.getParentId().intValue() == 0) {//0表示父菜單
parentV.add(module);
} else {
childV.add(module);
}
allMenu+="["+module.getId()+"]";
}
request.setAttribute("parentVector", parentV);//所以的父菜單Vector
request.setAttribute("childVector", childV);//所以的子菜單Vector
request.setAttribute("allMenu", allMenu);//所有菜單(String型)形如:[1][2][3][4][5]...
request.setAttribute("id", instance.getId());//當(dāng)前用戶id
request.setAttribute("userPermission", instance.getPermission());//當(dāng)前用戶菜單(String型)形如:[2][4][5]...
return mapping.findForward("viewUserPermission");
}
顯示部分jsp:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>修改權(quán)限</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" type="text/javascript">
<!--
function selectKind(target){
var obj = target;
var container = document.getElementById("sub_" + obj.name);
for(var i=0;i<container.getElementsByTagName("input").length;i++){
container.getElementsByTagName("input")[i].checked = obj.checked;;
}
}
function getCheckedBox(){
if(confirm("警告:此操作可能會(huì)影響到輸出。\n如果您確定要修改,請(qǐng)先聯(lián)系管理員!")){
var checkNum=document.getElementsByTagName("input").length;
var values="";
for(var i=0;i<checkNum;i++){
if(document.getElementsByTagName("input")[i].checked){
values+="["+document.getElementsByTagName("input")[i].value+"]";
}
}
window.location.href="user.portal?action=viewUserPermissionSubmit"+"&permissions="+values+"&id="+${requestScope.id};
}
}
-->
</script>
</head>
<body>
<c:forEach var='parent' items='${requestScope.parentVector}' varStatus='i'>
<c:if test="${fn:indexOf(requestScope.allMenu,parent.id)!=-1}">
<table border="0" width="400">
<tr>
<td>
<div align="left" >
<c:choose>
<c:when test="${fn:indexOf(requestScope.userPermission,parent.id)!=-1}">
<input type="checkbox" value="${ parent.id }" name='${ i.index }' onclick="selectKind(this)" checked>${ parent.moduleName }
</c:when>
<c:otherwise>
<input type="checkbox" value="${ parent.id }" name='${ i.index }' onclick="selectKind(this)" >${ parent.moduleName }
</c:otherwise>
</c:choose>
</div>
</td>
</tr>
</table>
</c:if>
<div id='sub_${i.index}' style="padding-left:20px">
<table border="0" width="300">
<c:forEach var='child' items='${requestScope.childVector}' varStatus='j'>
<c:if test="${fn:indexOf(requestScope.allMenu,parent.id)!=-1&&child.parentId==parent.id}">
<tr>
<td>
<c:choose>
<c:when test="${fn:indexOf(requestScope.userPermission,child.id)!=-1}">
<input type="checkbox" value="${child.id }" name='${ j.index }' checked>${child.moduleName}
</c:when>
<c:otherwise>
<input type="checkbox" value="${child.id }" name='${ j.index }' >${child.moduleName}
</c:otherwise>
</c:choose>
</td>
</tr>
</c:if>
</c:forEach>
</table>
</div>
</c:forEach>
<input title="修改權(quán)限" class="button" onclick="getCheckedBox();" type="button" value="提 交">
<input title="返回" class="button" onclick="window.location.href='user.portal?action=list';" type="button" value="返 回">
</body>
</html>
注意window.location.href="user.portal?action=viewUserPermissionSubmit"+"&permissions="+values+"&id="+${requestScope.id};
這里values就是修改后的菜單id,形如:[2][3][5]...把它傳到后臺(tái)修改User表的權(quán)限字段并刷新頁(yè)面就可以顯示修改后的菜單了。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

