2014-04-22 124 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>