如何使用Struts2框架向Dojo树提供JSON数据
我目前正在使用Struts2框架开发一个web应用程序。此应用程序需要根据从另一个应用程序接收的数据动态更新屏幕上的对象 目前,我想实现一个动态树视图,其中节点使用Action类提供的数据定期更新。我正试图使用dojo工具箱中的dojo.dijit.tree对象来实现这一点。我知道我可以使用dojo标记来实现这一点,dojo标记是struts框架的一部分,但是,它缺少很多我需要的功能,比如持久性、动态打开和关闭分支等等,因此,我选择使用dojo工具包来代替 我对dojo.dijit.tree的问题是,我不知道如何使用JSON结果类型提供其数据。我已经创建了一个类,它返回的JSON结果类型与dojo树组件所需的结构相同。我已经使用类生成的文件“test.txt”测试了dojo树的生成,该文件按预期工作。但是,我希望将JSON数据直接传递到dojo.dijit.tree组件,而不在磁盘上保存文件。当我执行应用程序时,我会得到一个“另存为”窗口来保存返回的JSON结果 这是我的struts.xml文件:如何使用Struts2框架向Dojo树提供JSON数据,json,struts2,tree,dojo,Json,Struts2,Tree,Dojo,我目前正在使用Struts2框架开发一个web应用程序。此应用程序需要根据从另一个应用程序接收的数据动态更新屏幕上的对象 目前,我想实现一个动态树视图,其中节点使用Action类提供的数据定期更新。我正试图使用dojo工具箱中的dojo.dijit.tree对象来实现这一点。我知道我可以使用dojo标记来实现这一点,dojo标记是struts框架的一部分,但是,它缺少很多我需要的功能,比如持久性、动态打开和关闭分支等等,因此,我选择使用dojo工具包来代替 我对dojo.dijit.tree的问
<struts>
<constant name="struts.devMode" value="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>
}
我想请stackoverflow阅读器告诉我如何读取jsp文件中的JSON数据以填充dojo树。此外,我想问一下如何定期刷新它的内容
PS:如果有人有更好的方法来实现类似的东西,我会很高兴收到你的意见
提前感谢。我找到了一种将数据直接从JSON结果传递到dojo.dijit.tree组件的方法。将url参数设置为返回json结果类型的操作名称 这是我的.jsp文件的新主体:
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" />
如果您已经验证了Struts2 S2操作是否生成了格式正确的JSON结果,那么请忽略所有S2细节,只需简单地说某个URL提供了所需的JSON,使用您的文本文件显示一个工作示例,但希望替换URL中的数据,我相信DOJO专家会发现它更加清晰。顺便说一句,感谢您直接使用工具包。谢谢您的答案四元数。这是生成的JSON结果数据:{identifier:name,label:name,items:[{children:null,name:name1,type:cat},{children:null,type:cat},{children:null,name:cat},{children:null,name:name4,type:cat}}},如果我将其保存为文件test.txt,然后按照以下方式声明ItemFileReadStore。问题是我不知道如何在不先将JSON结果保存为文件的情况下读取它。
<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>
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;
}
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;
}
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" />