jstree+struts2已经卡住好几天了,网上资料都是那几个帖子,实际帮助并不大。
自己慢慢摸索着写,结果出现了很奇怪的问题。
测试控制台输出为以下数据:
[{attributes:{id":0"},state:"open",data:"root0_0" ,children:[{attributes:{id":1"},state:"open",data:"root1_1" ,children:[{attributes:{id":2"},state:"open",data:"root2_1" ,children:[{attributes:{id:5"},state:"open",data:"root3_1"},{attributes:{id:6"},state:"open",data:"root3_2"}]},{attributes:{id":3"},state:"open",data:"root2_2" ,children:[{attributes:{id:7"},state:"open",data:"root3_3"}]},{attributes:{id:4"},state:"open",data:"root2_3"}]},{attributes:{id":8"},state:"open",data:"root1_2" ,children:[{attributes:{id":9"},state:"open",data:"root2_4" ,children:[{attributes:{id:11"},state:"open",data:"root3_4"}]},{attributes:{id:10"},state:"open",data:"root2_5"}]}]}]
但是页面得到的json却是[][][]
如果直接用流输出也无法正确显示树,loading后就什么都没有了。
以下是action源码:
protected String treejson;
public String getTreejson() {
return this.treejson;
}
public void setTreejson(String treejson) {
this.treejson = treejson;
}
public String getTree() throws Exception{
treejson += "[";
treejson += getJson(-1);
treejson += "]";
treejson = treejson.replace("null", "");
treejson = treejson.replace("[[", "[");

return SUCCESS;
}
private String getJson(int pid){
TreeDao td = new TreeDao();
//同一父节点的所有子节点集合
List<Tree> list = new ArrayList<Tree>();
list = td.getTreesByPid(pid);
//所有拥有子节点的节点集合
List<Tree> listC = new ArrayList<Tree>();
listC = td.getTreesHasChildren();
for (int i = 0;i<list.size();i++){
Tree tree = list.get(i);
Boolean b = false;
for (Tree treeC : listC){
if (tree.getId() == treeC.getId()){
b = true;
break;
}
}
if (b){
treejson += "{attributes:{id\":" + tree.getId()   
                + "\"},state:\"open\",data:\"" + tree.getName()   
                + "\" ,";   
treejson += "children:["; 
getJson(tree.getId());
treejson += "]";
treejson += "}";
}
else {
treejson += "{attributes:{id:" + tree.getId()
                + "\"},state:\"open\",data:\"" + tree.getName() + "\""
                + "}";
}
if (i < list.size() - 1){
treejson += ",";
}
}
return treejson;
}
求高手执教。

解决方案 »

  1.   

    测试控制台输出为:
    [{attributes:{id:"0"},state:"open",data:"root0_0" ,children:[{attributes:{id:"1"},state:"open",data:"root1_1" ,children:[{attributes:{id:"2"},state:"open",data:"root2_1" ,children:[{attributes:{id:"5"},state:"open",data:"root3_1"},{attributes:{id:"6"},state:"open",data:"root3_2"}]},{attributes:{id:"3"},state:"open",data:"root2_2" ,children:[{attributes:{id:"7"},state:"open",data:"root3_3"}]},{attributes:{id:"4"},state:"open",data:"root2_3"}]},{attributes:{id:"8"},state:"open",data:"root1_2" ,children:[{attributes:{id:"9"},state:"open",data:"root2_4" ,children:[{attributes:{id:"11"},state:"open",data:"root3_4"}]},{attributes:{id:"10"},state:"open",data:"root2_5"}]}]}]
    刚才有些错误。