问题描述
自己学习easy ui+spring3 web mvc+spring3 mvc+mybatis,做了一些尝试,现在有一个功能实现出了点问题,特来请教大家:首先,功能说明:其实很简单,就是要根据后台返回的json数据在页面创建一个combotree,这个对象需要的是一个树状的json格式。我的做法:整个spring环境配置都没问题,这儿就不说了,关于spring mvc的配置,加入了如下内容,支持自动的json转换:<bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />实现的controller的主要代码如下:/** * 根据查询结果生成树,自动转换成json树。 */@RequestMapping(value = "/subtree", method = RequestMethod.POST)@ResponseBodypublic ArrayList<BeanTree> subtree(String swjgdm) throws Exception {Tree tree = new Tree();ArrayList<BeanTree> treelist = null;ArrayList<BeanTree> nodelist = new ArrayList<BeanTree>();for (int i = 0; i <= 1; i++) {BeanTree node = new BeanTree();node.setId("1640100000" + i);node.setText("第一季" + i);node.setFldtag("备注" + i);nodelist.add(node);for (int j = 1; j <= 3; j++) {node = new BeanTree();node.setId("164010000" + i + j);node.setText("第二季" + i+j);node.setFldtag("备注" + i+j);node.setParentid("1640100000" + i);nodelist.add(node);for (int k = 1; k <= 3; k++) {node = new BeanTree();node.setId("16401000" + i + j + k);node.setText("第三季" + i + j+ k);node.setFldtag("备注" + i + j+ k);node.setParentid("164010000" + i + j);nodelist.add(node);}}}treelist = tree.createTree(nodelist);logger.info("创建树", treelist);return treelist;}这儿使用了两个类来创建树状的结构:BeaTree.javapackage com.person.camelt.common.utils;import java.util.ArrayList;public class BeanTree {private String id;private String text;private String parentid;private boolean leaf;private String fldtag;private ArrayList<BeanTree> child;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 String getParentid() {return parentid;}public void setParentid(String parentid) {this.parentid = parentid;}public boolean isLeaf() {return leaf;}public void setLeaf(boolean leaf) {this.leaf = leaf;}public String getFldtag() {return fldtag;}public void setFldtag(String fldtag) {this.fldtag = fldtag;}public ArrayList<BeanTree> getChild() {return child;}public void setChild(ArrayList<BeanTree> child) {this.child = child;}}Tree.java用来创建树状结构package com.person.camelt.common.utils;import java.util.ArrayList;public class Tree {// 将节点数组转换为树结构public ArrayList<BeanTree> createTree(ArrayList<BeanTree> lListBean) {ArrayList<BeanTree> aListBean = new ArrayList<BeanTree>();int num = lListBean.size();for (int i = 0; i < num; i++) {// 当所有的节点都不是当前节点的父节点时,说明该节点为“跟节点”,根节点可以有多个。boolean flag = true;for (int j = 0; j < num; j++) {if (lListBean.get(j).getId().equals(lListBean.get(i).getParentid())) {flag = false;break;}}if (flag) {lListBean.get(i).setParentid("");aListBean.add(lListBean.get(i));}}if (lListBean != null) {settleNode(aListBean, lListBean);}return aListBean;}// 递归整理树节点public static void settleNode(ArrayList<BeanTree> aListBean,ArrayList<BeanTree> lListBean) {if (aListBean.size() < lListBean.size()) {int num = lListBean.size();for (BeanTree bean : aListBean) {ArrayList<BeanTree> aListBeanC = new ArrayList<BeanTree>();for (int i = 0; i < num; i++) {if (bean.getId().equals(lListBean.get(i).getParentid())) {aListBeanC.add(lListBean.get(i));}}bean.setChild(aListBeanC);settleNode(aListBeanC, lListBean);}}}}然后,客户端的调试信息所获得的json数据如下:[{"id":"16401000000","text":"第一季0","parentid":"","leaf":false,"fldtag":"备注0","child":[{"id":"16401000001","text":"第二季01","parentid":"16401000000","leaf":false,"fldtag":"备注01","child":[{"id":"16401000011","text":"第三季011","parentid":"16401000001","leaf":false,"fldtag":"备注011","child":[{"id":"16401000111","text":"第三季111","parentid":"16401000011","leaf":false,"fldtag":"备注111","child":[]},{"id":"16401000112","text":"第三季112","parentid":"16401000011","leaf":false,"fldtag":"备注112","child":[]},{"id":"16401000113","text":"第三季113","parentid":"16401000011","leaf":false,"fldtag":"备注113","child":[]}]},{"id":"16401000012","text":"第三季012","parentid":"16401000001","leaf":false,"fldtag":"备注012","child":[{"id":"16401000121","text":"第三季121","parentid":"16401000012","leaf":false,"fldtag":"备注121","child":[]},{"id":"16401000122","text":"第三季122","parentid":"16401000012","leaf":false,"fldtag":"备注122","child":[]},{"id":"16401000123","text":"第三季123","parentid":"16401000012","leaf":false,"fldtag":"备注123","child":[]}]},{"id":"16401000013","text":"第三季013","parentid":"16401000001","leaf":false,"fldtag":"备注013","child":[{"id":"16401000131","text":"第三季131","parentid":"16401000013","leaf":false,"fldtag":"备注131","child":[]},{"id":"16401000132","text":"第三季132","parentid":"16401000013","leaf":false,"fldtag":"备注132","child":[]},{"id":"16401000133","text":"第三季133","parentid":"16401000013","leaf":false,"fldtag":"备注133","child":[]}]},{"id":"16401000011","text"这儿就不把所有的数据列出来了。现在的问题是:根据easy ui的文档,combotree需要的json数据格式应和上面列出来的差不多,但是,有两点差别。 首先,文档要求的json数据中,子节点使用的是“children”,而生成的文档却是“child”。 其次,生成的combotree只能显示出来树的第一级内容,后面的都显示不出来。请问大家有没有什么好的办法,谢谢了。
解决方案
首先,文档要求的json数据中,子节点使用的是“children”,而生成的文档却是“child”。 那就把child字段 改成 chidren 其次,生成的combotree只能显示出来树的第一级内容,后面的都显示不出来。 第一个问题造成的
解决方案二:
1L正解