2014-04-22 47 views
0

我正在使用sAjaxSource从我的弹簧控制器获取数据,从而给出json响应。 像这样GSON with DataTables弹簧mvc为空数据

[ 
    { 
     "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”:“”..我的数据属性为空..我如何处理这个(事情?我在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); 
    } 

这里是我的DataTable创建

$("#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. 
      } 
      }); 

它完美罚款时,有列表中的一些数据,但如果该列表是空的DataTable中只显示“加载”消息,我得到类型错误:无法处理浏览器控制台错误中的空例外。 我已经通过编写一些jquery代码来替换“Loading ...”消息来临时修复它。请指导。

回答

1

您的错误似乎与您如何表示空白列表有关。

当listobjs为null时,您的控制器似乎输出“null”。 datatables插件假设一个空的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>