【问题标题】:GSON with DataTables spring mvc for empty dataGSON 与 DataTables spring mvc 用于空数据
【发布时间】:2014-04-22 15:05:58
【问题描述】:

我正在使用 sAjaxSource 从我的 spring 控制器中获取数据,该控制器给出 json 响应。 像这样的

   [
   {
      "id":20009,
      "title":"1914 tran by H. Rackham",
      "description":"\"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charmces ",
      "username":"nessudi",
      "datecreated":"Apr 22, 2014 10:39:24 AM"
   },
   {
      "id":20008,
      "title":"The standard Lorem Ipsum passage, used since the 1500s",
      "description":"\"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\"",
      "username":"naai",
      "datecreated":"Apr 22, 2014 10:38:46 AM"
   },
   {
      "id":20065,
      "title":"Where to get Lorem Ipsum",
      "description":"Therpetition, injected humour, or non-characteristic words etc.",
      "username":"nensi",
      "datecreated":"Apr 22, 2014 10:37:34 AM"
   },
   {
      "id":20056,
      "title":"What is Lorem Ipsum",
      "description":"Lorrinter took a galley of type and scrambled it to make a type specimen book. ",
      "username":"xyz",
      "datecreated":"Apr 22, 2014 10:28:39 AM"
   }
]

虽然没有数据时数据表会显示“正在加载......”消息。

我阅读了其他建议使用“{aaData:[ ]}”发送空数组的帖子..但我正在使用 "sAjaxDataProp": "" ..so 我的数据属性为空.. 我该如何处理?我在 java 中使用 gson 库。

这是我的控制器

@RequestMapping(value="/theurl", method =  RequestMethod.GET)
    public @ResponseBody String somemethod(){
        List<someobject> listobjs = this.someService.getobjsList();
        if(listobjs != null){
            Collections.sort(listobjs, Collections.reverseOrder());
        }
        return gson.toJson(listobjs);
    }

这是我创建的数据表

$("#myTable").dataTable({
            "bPaginate": true,
            "sAjaxSource": "/theurl",
            "sAjaxDataProp": "",
            "oLanguage": {
                "sEmptyTable":     "My Custom Message On Empty Table"
            },
            "iDisplayLength": 5,
            "bSort" : false,
            "aoColumnDefs" : [
                               {"aTargets": [0],
                                "mRender" : function (data, type, full){
                                    return '<input style="float:left; margin: 0 auto; width: 100%;" class="selected" type="checkbox" value="' + data + '">';
                                }   
                               }
                               ],
            "aoColumns" : [{"mData" : "id"},
                           {"mData" : "title"},
                           {"mData" : "description"},
                           {"mData" : "datecreated"}],

            "fnInitComplete": function(oSettings, json){
                //I do something here after init.
            }
             });

当列表中有一些数据时,它工作得非常好,但是当列表为空时,数据表只显示“正在加载”消息并且我收到类型错误:无法处理浏览器控制台错误中的空异常。 我已经通过编写一些 jquery 代码来替换“正在加载...”消息来临时修复它。请指导。

【问题讨论】:

    标签: jquery json spring datatables gson


    【解决方案1】:

    您的错误似乎与您表示空列表的方式有关。

    当 listobjs 为 null 时,您的控制器似乎输出“null”。 数据表插件假定一个空的 Javascript 数组,"[]" .. 因此类型错误。

    如果 listobjs 为空,您应该将控制器更改为只返回一个空列表,例如

    final ArrayList<SomeObject> empty = new ArrayList<SomeObject>();
    gson.toJson(empty);
    

    这也可以在您的服务控制器中处理,因此您无需更改任何内容。

    附带说明:如果您已经在使用 Spring 并且不依赖 Gson 的细节,您可以通过使用内置的 jackson marshaller 来大大简化您的代码 - 它可以让您直接返回 List 对象。

    @RequestMapping("/theurl")
    public
    @ResponseBody
    List<SomeObject> somemethod() {
        // call to service
        return listobjs;
    }
    

    如果您将必要的杰克逊罐子添加到类路径中,第二个示例就可以正常工作 - 如果您使用的是 maven

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 2019-12-03
      • 2015-01-04
      • 2015-03-27
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多