数据库结构:
munuId parentId menuName
1 系统设置
2 1 修改密码
@RequestMapping(value="/loadMenus",produces="text/html;charset=UTF-8")
@ResponseBody
public String loadMenu(String roleId) throws BaseException{
String data="";
try {
List<AppMenu> menus = frameService.getAllMenus();//所有菜单
List<AppResource> resources=resourceService.findResource(roleId);//角色拥有的所有权限
AppResourceMenu appResourceMenu = new AppResourceMenu();
StringBuffer json=new StringBuffer("[");
for (AppMenu menu : menus) {
appResourceMenu= appResourceMenuService.findResourceIdByMenuId(menu.getMenuId());//menuId查询权限resourceId
json.append("{\"id\":"+menu.getMenuId()+",");
if(appResourceMenu !=null){//菜单对应的权限id resources角色拥有的权限
int i=0;
for( i=0;i<resources.size();i++ ){
if(resources.get(i).getResourceId()==appResourceMenu.getResourceId()){
json.append("\"checked\":\"true"+"\",");
json.append("\"resourceId\":"+appResourceMenu.getResourceId()+",");
break;
}else{
continue;
}
}
if(i>=resources.size()){
json.append("\"checked\":\"false"+"\",");
json.append("\"resourceId\":"+appResourceMenu.getResourceId()+",");
}
}else{
json.append("\"checked\":\"false"+"\",");
json.append("\"resourceId\":"+null+",");
json.append("\"isParent\":"+menu.getParentId()+",");
}
json.append("\"pId\":"+menu.getParentId()+",");
json.append("\"iconOpen\":\"../resources-1.0.1/Ztree/img/diy/1_open.png"+"\",");
json.append("\"iconClose\":\"../resources-1.0.1/Ztree/img/diy/1_close.png"+"\",");
json.append("\"icon\":\"../resources-1.0.1/treeImages/"+menu.getPic()+"\",");
json.append("\"name\":\""+menu.getMenuName()+"\",");
data=json.substring(0,json.lastIndexOf(","))+"},";
json=new StringBuffer(data);
}
data=json.substring(0, json.length()-1)+"]";
}catch(Exception e){
logger.error(e);
}
return data;
}
jsp页面+赋权:
//获取节点
var zNodes;
$(function(){
$.ajax({
type: 'POST',
/* url: "${basePath}/frame/queryMenu", */ //请求的action路径
url: "${basePath}/frame/loadMenus",
data:{roleId:"${role.roleId}"},
dataType:"text",
beforeSend:function(XMLHttpRequest){
},
error: function () {//请求失败处理函数
alertMsg.confirm("数据加载失败,请重试!", {
});
},
success:function(data){ //请求成功后处理函数。
zNodes = eval(data); //把后台封装好的简单Json格式赋给zNodes
$("#aa").css('display','block');
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
}
});
});
var setting={
check: {
enable: true
},
view:{
showTitle: true,
showIcon: true,
showLine: true,
},
callback: {
onCheck:onCheck,
},
data:{
simpleData: {
enable: true,
}
}
};
var resourceId="";
function onCheck(event, treeId, treeNode){
resourceId="";
var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
treeNode=treeObj.getCheckedNodes(true);//所有选中的节点
for(var i=0;i<treeNode.length;i++){
if(treeNode[i].resourceId!=null){
resourceId+="&resourceId="+treeNode[i].resourceId;//获取选中节点的值
}
}
}
function add(){
var ps = "";
var roleId="${role.roleId}";
ps=ps+"?roleId="+roleId;
ps += resourceId;
if(resourceId==""){
alertMsg.confirm("对不起,您还未进行任何操作!", {
});
return;
}
alertMsg.confirm("确定要给该角色赋予权限吗?", {
okCall: function(){
$.post('${basePath}/roleResource/assignResource'+ps,function(data){
success:DWZ.ajaxDone(data);
$.pdialog.closeCurrent();
});
}
});
}
<div>
<table>
<ul id="treeDemo" class="ztree"></ul>
</table>
</div>