我正在尝试使用测试数据暂时创建一个dojo树。 Bean始终返回相同的值。当我将数据放在nsf资源中的文件中时,它可以工作。使用Bean失败。它仅显示单词Continent,而我无法打开和下降该节点。

有人可以帮我吗?我将不胜感激!

我的代码在store2起作用的地方,store1错误出了,而store0仅显示了大陆:

                <xe:restService id="restService1" pathInfo="treeData">
                    <xe:this.service>
                        <xe:customRestService serviceBean="com.sefip.TreeData" requestContentType="application/json" requestVar="tree"></xe:customRestService>
                    </xe:this.service>
                </xe:restService>
                <!--
                    xp:text disableTheme="true" value="#{javascript:getBoxValue(compositeData.boxName)}" styleClass="readonly" style="margin-top:2px">
                    <xp:this.rendered><![CDATA[#{javascript:getBoxValue(compositeData.boxName)}]]></xp:this.rendered> </xp:text> <xp:div id="boxTree"
                    dojoType="dijit.Tree"></xp:div
                -->

                <xp:scriptBlock id="scriptBlock2">
                    <xp:this.value><![CDATA[
    dojo.require("dojox.data.JsonRestStore");
    dojo.require("dojo.store.JsonRest");
    dojo.require("dojo.data.ItemFileReadStore");
    dojo.require("dijit.tree.ForestStoreModel");
    dojo.require("dijit.Tree");

    dojo.addOnLoad(function() {

// Create a data store to retrieve data from

        var store0 = new dojox.data.JsonRestStore({
            //url: "countries.json"
            target: "aCRM2.xsp/treeData/",
            labelAttribute: "name"
        });

        var store1 = new dojo.store.JsonRest({
            target: "aCRM2.xsp/treeData/",
            labelAttribute: "name"
        });

        var store2 = new dojo.data.ItemFileReadStore({
            url: "countries.json"
        });


// secondly we create a treeModel.
        var treeModel = new dijit.tree.ForestStoreModel({
            store: store0,
            query: {type: "continent"},
            rootId: "root",
            rootLabel: "Continents",
            childrenAttrs: ["children"]
        });

// Last but not least we create a new instance of our tree.
        var tree= new dijit.Tree({
            model: treeModel
        },
        "#{id:treeOne}");
    });
    ]]></xp:this.value>
                </xp:scriptBlock>

                <!-- The domnode we will use to render the tree -->
                <xp:div id="treeOne" />

            </xp:div>


country.json包含:

{
    "items" : [{
            "id" : "EU",
            "children" : [{
                    "_reference" : "NL"
                }
            ],
            "type" : "continent",
            "name" : "Europe"
        }, {
            "id" : "NL",
            "type" : "country",
            "name" : "Netherlands"
        }
    ],
    "label" : "name",
    "identifier" : "id"
}


Bean生成的代码完全相同,但是出于完整性考虑,我将在此处添加代码:

package com.sefip;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.commons.util.io.json.JsonException;
import com.ibm.commons.util.io.json.JsonGenerator;
import com.ibm.domino.services.ServiceException;
import com.ibm.domino.services.rest.RestServiceEngine;
import com.ibm.jscript.InterpretException;
import com.ibm.jscript.JSContext;
import com.ibm.jscript.json.JsonJavaScriptFactory;
import com.ibm.jscript.std.ArrayObject;
import com.ibm.jscript.std.ObjectObject;
import com.ibm.jscript.types.FBSUtility;
import com.ibm.xsp.extlib.component.rest.CustomService;
import com.ibm.xsp.extlib.component.rest.CustomServiceBean;
import com.ibm.xsp.util.JavaScriptUtil;

public class TreeData extends CustomServiceBean {

    @Override
    public void renderService(CustomService service, RestServiceEngine engine) throws ServiceException {

        HttpServletRequest request = engine.getHttpRequest();
        HttpServletResponse response = engine.getHttpResponse();
        String method = request.getMethod();

        response.setHeader("Content-Type", "application/json; charset=UTF-8");

        if (method.equals("GET")) {
            this.get(engine);
        } else if (method.equals("POST")) {
            this.post(engine, request);
        } else {
            this.other(engine);
        }
    }

    public void get(RestServiceEngine engine) {
        HttpServletResponse response = engine.getHttpResponse();
        try {
            JSContext jsContext = JavaScriptUtil.getJSContext();
            JsonJavaScriptFactory factory = new JsonJavaScriptFactory(jsContext);
            String json = null;
            ObjectObject returnJSON = new ObjectObject();
            returnJSON.put("identifier", FBSUtility.wrap("id"));
            returnJSON.put("label", FBSUtility.wrap("name"));
            ArrayObject countries = new ArrayObject();
            ObjectObject continent = new ObjectObject();
            continent.put("id", FBSUtility.wrap("EU"));
            continent.put("name", FBSUtility.wrap("Europe"));
            continent.put("type", FBSUtility.wrap("continent"));
            ArrayObject children = new ArrayObject();
            ObjectObject child = new ObjectObject();
            child.put("_reference", FBSUtility.wrap("NL"));
            children.addArrayValue(child);
            continent.put("children", children);
            countries.addArrayValue(continent);
            ObjectObject country = new ObjectObject();
            country.put("id", FBSUtility.wrap("NL"));
            country.put("name", FBSUtility.wrap("Netherlands"));
            country.put("type", FBSUtility.wrap("country"));
            countries.addArrayValue(country);
            returnJSON.put("items", countries);
            UserData.get().addLog("call generator");
            json = JsonGenerator.toJson(factory, returnJSON);
            UserData.get().addLog(json);
            response.getWriter().write(json);
            response.getWriter().close();
            return;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterpretException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void post(RestServiceEngine engine, HttpServletRequest request) {
        HttpServletResponse response = engine.getHttpResponse();
        Map parameters = request.getParameterMap();
        try {
            response.getWriter().write("post()");
            response.getWriter().write(request.getParameter("form"));
            String[] form = (String[]) parameters.get("form");
            String val = form[0];
            response.getWriter().write(val);
            response.getWriter().close();
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    public void other(RestServiceEngine engine) {
        HttpServletResponse response = engine.getHttpResponse();
        try {
            response.getWriter().write("other()");
            response.getWriter().close();
            return;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


所有这些主要基于Jeroen Somhorst和其他人所做的工作,请参见http://www.jeroensomhorst.eu/uncategorized/viewpanel-vs-dijit-tree-part-1/和第2部分。

PS Dank je,Jeroen,maar waar是第3部分? ;-)

最佳答案

终于找到了解决方案。

事实证明,JsonRestStore的结构应该是包含对象和子对象的嵌套数组,如下所示:

[{
    "id" : "EU",
    "children" : [{
        "id" : "NL",
        "type" : "country",
        "name" : "Netherlands"
        }],
    "type" : "continent",
    "name" : "Europe"
}]


干净利落...

感谢Jeroen提供的链接,其中的评论向我展示了这种方式。

10-05 19:11