因为使用easyui的tree菜单时不能够直接使用后台数据转的json对象,要先转换成tree要求的对象格式再转换成json.
首先创建一个TreeNode的类
private String id;
private String text;
private Map<String, Object> attributes = new HashMap<>();
private List<TreeNode> children = new ArrayList<>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Map<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
public TreeNode(String id, String text, Map<String, Object> attributes, List<TreeNode> children) {
super();
this.id = id;
this.text = text;
this.attributes = attributes;
this.children = children;
}
public TreeNode() {
super();
}
@Override
public String toString() {
return "TreeNode [id=" + id + ", text=" + text + ", attributes=" + attributes + ", children=" + children + "]";
}
}
先从数据库取到数据,由于没有使用实体类所以使用map集合因为map集合转换的json对象和java对象转换的是一样的
/**
*
* @Title: findmenu
* @Description: (查询数据库里的菜单项)
* @param map 前台页面上的参数集合
* @return 因为是树形菜单所以不用分页
* @return List<Map<String,Object>> 返回数据库的菜单集合 不能直接用于展示
* @throws Exception
*/
public List<Map<String, Object>> findmenu(Map<String, String[]> map) throws Exception{
StringBuilder sb=new StringBuilder();
sb.append("SELECT * from t_easyui_menu where true");
/**
* 如果获取到菜单id
* 就将这个id作为父id去查询
*/
String paramvalue = JsonUtils.getParamvalue(map, "Menuid");
if(StringUtils.isBlank(paramvalue)) {
sb.append(" and parentid=-1");//最大的一级菜单
}else {
sb.append(" and parentid in ("+paramvalue+")");
}
/**
* 这里使用的是通用方法
*/
return super.executeQuery(null, sb.toString());
}
通用方法的关键代码为
List<Map<String, Object>> list = new ArrayList<>();
/**
* 获取到源数据
*/
ResultSetMetaData metaData = rs.getMetaData();
/**
*获取到有多少个列
*/
int columnCount = metaData.getColumnCount();
Map<String, Object> map=null;//多次引用
while(rs.next()) {
map=new HashMap<String, Object>();
for (int i = 1; i <=columnCount; i++) {
/**
* 获取到列的名字作为键的值
*/
String key = metaData.getColumnName(i);
map.put(key, rs.getObject(key));//用列名做键取值保存到map集合
}
list.add(map);
}
return list;
然后就是两个方法的递归调用注意用当前id做为父id取查询,如果为空的话可能会造成无限递归栈内存溢出
/**
*
* @Title: mapTreeNode
* @Description: (传入单个数据库菜单map集合)
* @param map 数据库菜单对象
* @param treeNode 树形菜单节点
* @throws Exception
* @return void
*/
public void mapTreeNode(Map<String, Object> map,TreeNode treeNode) throws Exception {
treeNode.setId(map.get("Menuid").toString());
treeNode.setText(map.get("Menuname").toString());
treeNode.setAttributes(map);
//还要设置子节点
/**
* 菜单的id作为父id去查询
*/
Map<String, String[]> parammap=new HashMap<>();
parammap.put("Menuid",new String[] {map.get("Menuid").toString()});
List<Map<String, Object>>lsmap = findmenu(parammap);
/**
* 如果有子节点
* 就调用MaplisttoTreeNodeList方法
* 将子节点复制
*/
if(lsmap.size()>0) {
List<TreeNode> maplisttoTreeNodeList = MaplisttoTreeNodeList(lsmap);
treeNode.setChildren(maplisttoTreeNodeList);
}
}
/**
*
* @Title: MaplisttoTreeNodeList
* @Description: (将传入的list<map>集合转换为list<TreeNode>集合)
* @param lsmap 传入的数据库对象集合
* @return
* @throws Exception
* @return List<TreeNode>
*/
public List<TreeNode> MaplisttoTreeNodeList(List<Map<String, Object>> lsmap) throws Exception{
TreeNode treeNode=null;
List<TreeNode> ls=new ArrayList<>();
for (Map<String, Object> map : lsmap) {
treeNode=new TreeNode();
mapTreeNode(map, treeNode);
ls.add(treeNode);
}
return ls;
}
将这些方法组合一下
/**
*
* @Title: getTreeNode
* @Description: (返回一个TreeNode的集合)
* @param map
* @return
* @throws Exception
* @return List<TreeNode>
*/
public List<TreeNode> getTreeNode(Map<String, String[]> map) throws Exception {
/**
* 第一次手动查到数据
*/
List<Map<String, Object>> findmenu = this.findmenu(map);
List<TreeNode> maplisttoTreeNodeList = this.MaplisttoTreeNodeList(findmenu);
return maplisttoTreeNodeList;
}
子控制器,配置一下子控制器
private MenuDao md=new MenuDao();
public ActionForward MenuTreeList(HttpServletRequest request,HttpServletResponse response) {
Map<String, String[]> parameterMap = request.getParameterMap();
try {
List<TreeNode> treeNode = md.getTreeNode(parameterMap);
String str = JSON.toJSONString(treeNode);
super.print(str, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
然后在前台请求子控制器
$(function(){
$('#MenuTree').tree({
url:'menuaction.action?methodName=MenuTreeList&Menuid='+$("#Menuid").val()
,onClick: function(node){
if($('#Menutab').tabs('exists',node.text)){
$('#Menutab').tabs('select',node.text)
}else{
$('#Menutab').tabs('add',{
title:node.text,
content:'<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuUrl+'" width="99%" height="99%"></iframe>',
closable:true,
}); }
}
});
})
效果为: