动态Ztree-根据角色拥有的不同菜单拼Json生成Ztree

根据用户对应的角色所拥有的菜单在后台动态拼成Json,传递给js,生成Ztree。

公司之前的ztree实现都是在jsp里获取用户权限之后拼json。但添加父子级菜单,更改权限内容,维护起来都比较麻烦,所以花了两三个小时重构了一下,轻拍···

SpringMVC+Hibernate+Spring
Ztree+EasyUI

用户User 和 角色Role      多对一
角色Role 和 菜单Function  多对多
po里使用关联对象注解方式配置的关联关系。

具体代码:
1.Control层,easyui-layout的west访问下面的方法


@RequestMapping(value = "/showTreeMenu.do", method = RequestMethod.GET)
public String zTreeShow(Model model, HttpServletRequest request)
throws Exception {
String userId = ((UserVo) request.getSession()
.getAttribute("loginUser")).getUSERID();
List<FunctionVo> parentList = userService.selectParentZtree(userId); // 根据用户Id,查询该用户拥有的一级菜单
List<String> childList = userService.selectChildZtree(parentList,
request, userId); // 根据用户ID,一级菜单获取该用户拥有的子菜单
model.addAttribute("pList", parentList);
model.addAttribute("cList", childList);
return "/jsp/main/west";  //返回生成ztree的jsp页面
}

2.Service层

// 查询用户拥有的一级菜单
@Override
public List<FunctionVo> selectParentZtree(String userId) throws Exception {
List<Function> functionlist = this.userDao.selectParentZtree(userId);
List<FunctionVo> retFunctionVoList = new ArrayList<FunctionVo>();
for (Function function : functionlist) {
FunctionVo vo = new FunctionVo();
PropertyUtils.copyProperties(vo, function);
retFunctionVoList.add(vo);
}
return retFunctionVoList;
}

// 根据父类菜单的ID,查询出对应的子类菜单
@Override
public List<String> selectChildZtree(List<FunctionVo> parentList,
HttpServletRequest request, String userId) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("userId", userId);
List<String> childList = new ArrayList<String>();
for (FunctionVo vo : parentList) {
map.put("functionVo", vo);
String temp = recursionChildZtree(map, request, 0);//递归查询子菜单,拼json
if (temp.length() != 0) {
childList.add("[" + temp.substring(0, temp.length() - 1) + "]");//父菜单List和子菜单List一一对应,保证父子菜单的List下标相同
} else {
childList.add(null);
}
}
return childList;
}

// 根据参数递归查询子菜单
public String recursionChildZtree(Map<String, Object> map,
HttpServletRequest request, int status) throws Exception {
Map<String, Object> m;
FunctionVo vo;
List<Function> listResult = userDao.selectChildZtree(map); // 根据父类菜单和用户ID获取该菜单旗下用户拥有的所有子级菜单
String temp = "";
for (Function function : listResult) { 
if (function != null) {
if (((FunctionVo) map.get("functionVo")).getFUNCTIONID()
.equals(function.getPARENTFUNCTION())) 
{//如果当前菜单有父类菜单,则继续查询该菜单的子菜单
m = new HashMap<String, Object>();
vo = new FunctionVo();
PropertyUtils.copyProperties(vo, function);
m.put("functionVo", vo);
m.put("userId", map.get("userId"));
temp += recursionChildZtree(m, request, 1);
}
String icon = "";
String url = "";

icon = (status == 0) ? "1_open.png" : "1_close.png";
url = (status == 0) ? "" : ",attributes:{\"url\" :\""
+ request.getContextPath() + function.getFUNCTIONURL()
+ "\"}";
temp += "{id : \"" + function.getFUNCTIONID() + "\",pId:\""
+ function.getPARENTFUNCTION() + "\",icon:\""
+ request.getContextPath() + "/css/zTreeStyle/img/diy/"
+ icon + "\",name: \"" + function.getFUNCTIONNAME()
+ "\",open:true " + url + "},";
}
}
return temp;
}

3.Dao层

@Override
public List<Function> selectChildZtree(Map map) {
StringBuffer sql = new StringBuffer();
List<Function> list = null;
sql
.append("from Function a inner join fetch a.role b inner join fetch b.user c where c.USERID='"
+ map.get("userId")
+ "' and a.PARENTFUNCTION='"
+ ((FunctionVo) map.get("functionVo")).getFUNCTIONID()
+ "'order by a.FUNCTIONORDER, a.FUNCTIONID");
list = (List<Function>) this.findByHQL(sql.toString());
return list;
}

@Override
public List<Function> selectParentZtree(String userId) {
StringBuffer sql = new StringBuffer();
List<Function> list = null;
sql.append("from Function a inner join fetch a.role b inner join fetch b.user c where a.PARENTFUNCTION='0' and c.USERID='"
+ userId + "' order by a.FUNCTIONORDER, a.FUNCTIONID");
list = (List<Function>) this.findByHQL(sql.toString());
return list;
}

4.west.jsp

<script type="text/javascript" charset="utf-8">
var json=${cList };  //接收子菜单

$(function() {
$('#menuAccordion').accordion({ //定义手风琴
animate:false,
fit : true,
border : false
});
});
var setting = {
data: {
simpleData: {
enable: true
}
},
callback: {
onClick:zTreeOnClick
}
};

$(document).ready(function(){
for(var i=0;i<json.length;i++) {
$.fn.zTree.init($("#"+i+""), setting, json[i]); //遍历Json,生成zTree
}

});
function zTreeOnClick(event, treeId, treeNode) {

    addTab(treeNode);
};

</script>
<div id="menuAccordion">
<c:set var="index" value="0" />
<c:forEach items="${pList}" var="li"  >
<div title=" ${li.FUNCTIONNAME }" style="overflow:auto;" id="${li.FUNCTIONID }" >
<ul id="${index }" class="ztree"></ul>  <!-- 根据查出的父类菜单的数量生成对等的手风琴 -->
</div>
<c:set var="index" value="${index+1}" />
</c:forEach>
</div>

猜你喜欢

转载自csc526754427.iteye.com/blog/1900149
今日推荐