【问题标题】:smartGWT ListGrid JSON issuesmartGWT ListGrid JSON 问题
【发布时间】:2015-03-04 01:33:32
【问题描述】:

我在 URL:http://localhost:8888/rest/contacts 上创建了一个 REST API,并带有此 JSON 输出:

{
    "contact": {
        "address": [
            {
                "city":"Shanghai",
                "street":"Long Hua Street"
            },
            {
                "city":"Shanghai",
                "street":"Dong Quan Street"
            }
        ],
        "id": "huangyim",
        "name": "Huang Yi Ming"
   }
}

我只想打印 smartGWT ListGrid 中的 id 值。

public class ExampleEntry implements EntryPoint {
    #Override
    public void onModuleLoad() {
        DataSource dataSource = new DataSource();  
        dataSource.setDataFormat(DSDataFormat.JSON);  
        dataSource.setDataURL("http://localhost:8888/rest/contacts");
        dataSource.setRecordXPath("/contact");

        DataSourceTextField field = new DataSourceTextField("id", "id");
        dataSource.addField(field);        

        final ListGrid grid = new ListGrid();  
        grid.setDataSource(dataSource);
        grid.setAutoFetchData(true);  
        grid.draw();
    }
}

但它抛出了以下异常:

15:33:12.766 [ERROR] [jerseyexample] 15:33:12.747:XRP2:WARN:RPCManager:xmlHttpRequest.getAllResponseHeaders() returned null
com.smartgwt.client.core.JsObject$SGWT_WARN: 15:33:12.747:XRP2:WARN:RPCManager:xmlHttpRequest.getAllResponseHeaders() returned null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:105)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
    at java.lang.Thread.run(Unknown Source)

我尝试在谷歌上搜索以找到解决方法,但没有帮助。如果有人知道此问题的解决方案,请告诉我。

【问题讨论】:

    标签: json datasource smartgwt listgrid


    【解决方案1】:

    您只需要更改数据 url,而不是使用绝对路径使用相对路径。

    改变

    dataSource.setDataURL("http://localhost:8888/rest/contacts");
    

    dataSource.setDataURL("rest/contacts");
    

    当您必须将其部署到远程服务器时,这也是必要的。

    我建议您使用 RestDataSource 而不是普通的 DataSource,它专为 REST 服务而设计,正如您所说的那样。

    使用 RestDataSource 就像使用 DataSource 一样简单。

    @Override
    public void onModuleLoad() {
        RestDataSource dataSource = new RestDataSource();  
        dataSource.setDataFormat(DSDataFormat.JSON);  
        dataSource.setDataURL("rest/contacts");
        dataSource.setRecordXPath("/contact");
    
        DataSourceTextField field = new DataSourceTextField("id", "id");
        dataSource.addField(field);        
    
        OperationBinding get=new OperationBinding();
        get.setOperationType(DSOperationType.FETCH);
        dataSource.setOperationBindings(get);
    
        final ListGrid grid = new ListGrid();  
        grid.setDataSource(dataSource);
        grid.setAutoFetchData(true);  
        grid.draw();
    }
    

    您可以将每个 OperationBinding 映射到每个 http 方法(GET、POST PUT、DELETE)或任何适合您的 REST API 的方法。

    干杯!

    【讨论】:

      【解决方案2】:

      你可以直接把Xpath放在你要映射的字段中

      RecordXPath 可以直接在 DataSource 上指定一个简单的只读 DataSource 只能“获取”操作。

      field.setRecordXPath("contact")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-07
        • 1970-01-01
        • 2012-05-11
        • 1970-01-01
        相关资源
        最近更新 更多