`

权限控制

阅读更多
很多时候不同的人拥有不同的权限,不同的人拥有不同的菜单。
那怎么样做一个可以灵活变动的菜单呢。
大致思路:
建2张数据表:Module(保存着所有的菜单)和User(里面有一个“权限”字段保存着自己需要的菜单);
先获得所有菜单和用户菜单,所有的主菜单和子菜单
代码片段如下:
else if("viewUserPermission".equals(action)){
			System.out.println("--------------------viewUserPermission-------------------");
			String id=request.getParameter("id");
			User instance=(User)this.getBaseService().get("User",id);
//			初始化菜单,根据MenuLevel=0代表根目录从0开始
			List list = this.getModuleService().getModuleTree(Integer.parseInt(instance.getMenuLevel()));//getModuleTree里的参数一般是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());//当前用户id
			request.setAttribute("userPermission", instance.getPermission());//当前用户菜单(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>修改权限</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("警告:此操作可能会影响到输出。\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="修改权限" 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]...把它传到后台修改User表的权限字段并刷新页面就可以显示修改后的菜单了。



  • 大小: 38.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics