开发者

How to provide JSON data to Dojo tree using Struts2 framework

I'm currently developing a web application using Struts2 framework. This application requires to dynamically update the objects on the screen based on data received from another application.

At the moment, I would like to implement a dynamic tree view which nodes are updated periodically with data provided by an Action class. I’m trying to do so using the dojo.dijit.tree object from the dojo toolkit. I’m aware that I can do so using the dojo tags which are part of the struts framework, however, it lacks much of the functionality that I need (persistence, open and close branches dynamically, etc) therefore, I have opted for using the dojo toolkit instead.

My problem with the dojo.dijit.tree is that I don’t know how to provide its data using a JSON result type. I have already created a class which returns a JSON result type with the same structure needed by the dojo tree component. I have tested the generation of a dojo tree using a file “test.txt” which was generated by the class and it works as expected. However, I would like to pass the JSON data directly to the dojo.dijit.tree component without saving a file on disk. When I execute the application I get a “save as” window to save the returned JSON result.

This is my struts.xml file:

<struts>
<constant name="struts.devMode" v开发者_开发技巧alue="true" />

<package name="default" namespace="/" extends="struts-default">                     

    <action name="devResult" class="gui.JsonAction">                        
        <result name="success">/start1.jsp</result>                         
    </action>

</package>


<package name="example" namespace="/" extends="json-default">              

    <result-types>
        <result-type name="json" class="org.apache.struts2.json.JSONResult"></result-type>
    </result-types>  

    <action name="getJSONResult" class="gui.JsonAction">
        <result type="json"/>           
    </action>

</package>

This is the jsp file which displays the tree:

<head>
<title>Testing Tree</title>    

<style type="text/css">
  @import "js/dojo/dojo/resources/dojo.css";
  @import "js/dojo/dijit/themes/nihilo/nihilo.css";
</style>

<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js"
        djConfig="isDebug: true,parseOnLoad: true">
</script>

<script type="text/javascript">
    dojo.require("dojo.data.ItemFileReadStore");
    dojo.require("dijit.Tree");
    dojo.require("dojo.parser");        
</script>

<body class="nihilo">
    The Tree:<br><br>

    <s:url id="devResult" action="jsonAction.action"></s:url>
    <div dojoType="dojo.data.ItemFileReadStore" href="%{devResult}" jsid="popStore" />
    <div dojoType="dijit.Tree" store="popStore" labelAttr="sname" label="Tree" />
</body>

This is the Action class which produces the JSON result:

public class JsonAction extends ActionSupport {

private static final long serialVersionUID = 7392602552908646926L;
private String label = "name";
private String identifier = "name";
private List<ChildrenClass> items = new ArrayList<ChildrenClass>();

public JsonAction() {
    ChildrenClass item1 = new ChildrenClass("name1", "cat");
    ChildrenClass item2 = new ChildrenClass("name2", "cat");
    ChildrenClass item3 = new ChildrenClass("name3", "cat");
    ChildrenClass item4 = new ChildrenClass("name4", "cat");

    items.add(item1);
    items.add(item2);
    items.add(item3);
    items.add(item4);
}

public String execute() {       
    return SUCCESS;
}

public void setLabel(String label) {
    this.label = label;
}

public String getLabel() {
    return label;
}

public void setIdentifier(String identifier) {
    this.identifier = identifier;
}

public String getIdentifier() {
    return identifier;
}   

public void setItems(List<ChildrenClass> lists) {
    this.items = lists;
}

public List<ChildrenClass> getItems() {
    return items;
}

}

This is the ChildrenClass which is used in the class above:

public class ChildrenClass {

private String name;
private String type;
private ChildrenClass[] children; 

public ChildrenClass() {
    name = "DefaultName";
    type = "DefaultType";
}

public ChildrenClass(String aName, String aType) {
    name = aName;
    type = aType;
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setType(String type) {
    this.type = type;
}

public String getType() {
    return type;
}

public void setChildren(ChildrenClass[] children) {
    this.children = children;
}

public ChildrenClass[] getChildren() {
    return children;
}

}

I would like to ask to the stackoverflow reader to please indicate me how to do to read the JSON data in the jsp file in order to populate the dojo tree. In addition, I would like to ask how can I refresh the content of it periodically.

PS: If somebody has a better method to implement something similar to this, I would be glad to receive your comments.

Thanks in advance.


I have found out a way to pass data directly from a JSON result to a dojo.dijit.tree component. Setting the "url" parameter to the action name which returns the json result type.

This is my new body of the .jsp file:

Simple Tree:<br><br>
<div dojoType="dojo.data.ItemFileReadStore" url=getJSONResult handleAs="json" jsid="popStore" />
<div dojoType="dijit.Tree" store="popStore" labelAttr="sname" label="PID 512" />                        
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜