package isearch.swust.action;import java.util.ArrayList;
import java.util.List;import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;@ParentPackage("isearch3")
@Controller()
@Scope("prototype")public class ThesaurusAction extends ActionSupport {
private String id;
private String text;
private boolean hasChildren;
private boolean isexpand;
private boolean complete;
private boolean value;
private List<ThesaurusAction> ChildrenNodes;
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 boolean isHasChildren() {
return hasChildren;
}
public void setHasChildren(boolean hasChildren) {
this.hasChildren = hasChildren;
}
public boolean isIsexpand() {
return isexpand;
}
public void setIsexpand(boolean isexpand) {
this.isexpand = isexpand;
}
public boolean isComplete() {
return complete;
}
public void setComplete(boolean complete) {
this.complete = complete;
}
public boolean isValue() {
return value;
}
public void setValue(boolean value) {
this.value = value;
}
public List<ThesaurusAction> getChildrenNodes() {
return ChildrenNodes;
}
public void setChildrenNodes(List<ThesaurusAction> childrenNodes) {
ChildrenNodes = childrenNodes;
} @Action(value="sendNode",results={
@Result(name="success",type="json")
})
public String sendNode(){
List<ThesaurusAction> list=new ArrayList<ThesaurusAction>();
ThesaurusAction tree1 = new ThesaurusAction();//root
tree1.setId("1");
tree1.setText("中国");
tree1.setHasChildren(true);
tree1.setIsexpand(false);
tree1.setComplete(false);//root
List<ThesaurusAction> tree = new ArrayList<ThesaurusAction>();//children

tree1.setChildrenNodes(tree);

ThesaurusAction tree2 = new ThesaurusAction();//firstchild
ThesaurusAction tree3 = new ThesaurusAction();//secondchild
ThesaurusAction tree4 = new ThesaurusAction();//thirdchild

tree2.setId("0.1");
tree2.setText("北京");
tree3.setId("0.2");
tree3.setText("上海");
tree4.setId("0.3");
tree4.setText("天津");

tree.add(tree2);
tree.add(tree3);
tree.add(tree4);

List<ThesaurusAction> tree21 = new  ArrayList<ThesaurusAction>();
tree2.setChildrenNodes(tree21);
ThesaurusAction tr1= new ThesaurusAction();
ThesaurusAction tr2 = new ThesaurusAction();
tr1.setId("1.1");
tr1.setText("东城区");
tr2.setId("1.2");
tr2.setText("海淀区");
tree21.add(tr1);
tree21.add(tr2);


list.add(tree1);//putRoot
Gson gson=new Gson();
String json=gson.toJson(list);
System.out.println(tree1);
System.out.println(json);

SendJson.send(ServletActionContext.getResponse(), list);
return SUCCESS;
}
}
结果显示出来的结果仅有:北京,
点击下级目录还是北京,一直点下去都是北京,请问这是怎么回事?
哪里有问题?
   

解决方案 »

  1.   

    后台把你的json打印下 看格式和内容对不?
      

  2.   

    格式正确:[{"id":"0","text":"中国","hasChildren":true,"isexpand":false,"complete":false,"ChildrenNodes":[{"id":"0.1","text":"北京","hasChildren":false,"isexpand":false,"complete":false,"ChildrenNodes":[{"id":"1.1","text":"东城区","hasChildren":false,"isexpand":false,"complete":false,"validationAware":{}},{"id":"1.2","text":"海淀区","hasChildren":false,"isexpand":false,"complete":false,"validationAware":{}}],"validationAware":{}},{"id":"0.2","text":"上海","hasChildren":false,"isexpand":false,"complete":false,"validationAware":{}},{"id":"0.3","text":"天津","hasChildren":false,"isexpand":false,"complete":false,"validationAware":{}}],"validationAware":{}}]
    我估计是调用的问题。
      

  3.   

    function load() {        
    var o={
    url:"sendNode",
    };
    $.ajax({
    url: o.url,
    type: "POST",
      async: false,
    data: {
    id: "0"
    },
    success: function(treedata){
    o.data = treedata;
    $("#tree").treeview(o);
    }
    });
            }   
           
            $(document).ready(load);
      

  4.   


    试下吧
    $.ajax({
    url : o.url,
    type : "POST",
    async : false,
    contentType : "application/json; charset=utf-8",
    dataType : "json",
    data : {
    id : "0"
    },
    success : function(treedata) {
    o.data = eval(treedata);
    $("#tree").treeview(o);
    }
    });