2011-03-22 71 views
2

我是jqgrid的新手,我試圖模擬上面的代碼,但它不工作。在服務器端我已經創建的類jqgrid json數據類型與彈簧mvc

public class TaskBean { 
String orderId; 
String realty; 
String building; 
String priority; 
String action; 
String assignee; // add getter/setter methods 
} 

TaskBeanList.java

public class TaskBeanList { 
private String page; 
private String total; 
private String records; 
private List<TaskBean> rows; 
} 

春季控制器代碼

@RequestMapping("/page4.htm") 
public @ResponseBody TaskBeanList getJQGridData(HttpServletRequest request, HttpServletResponse response) { 
System.out.println("xml data"); 
List<TaskBean> taskList = new ArrayList<TaskBean>(); 
    taskList.add(new TaskBean("Task1", "K-CY-329", "144", "G-3", "1", "Pending", "XYZ")); 
    taskList.add(new TaskBean("Task2", "K-CY-356", "165", "A-10", "4", "Closed", "ABC")); 
    taskList.add(new TaskBean("Task3", "K-CY-343", "768", "B-12", "3", "Pending", "IJK")); 
    taskList.add(new TaskBean("Task4", "K-CY-786", "918", "F-9", "2", "Open", "PQR")); 
    TaskBeanList tsklst = new TaskBeanList("1", "5", "20", taskList); 
    return tsklst; 
} 

客戶端jqGrid的JS代碼

jQuery(document).ready(function(){ 
jQuery("#list3").jqGrid({ 
autowidth: true, 
datatype : "json", 
url: "http://localhost:8080/SpringMVC/page4.htm", 
mtype: 'get', 
colNames : ["Title","Order ID","Realty","Building", 
     "Priority","Action","Assignee"], 
colModel : [ 
{label: "Title", name: "title", index: "Title", jsonmap: "orderId"}, 
{label: "OrderID", name: "orderId", index: "OrderID", jsonmap: "orderId"}, 
{label: "Realty", name: "realty", index: "Realty", jsonmap: "realty" }, 
{label: "Building",name: "building",index: "Building",jsonmap: "building"}, 
{label: "Priority",name: "priority",index: "Priority",jsonmap: "priority"}, 
{label: "Action", name: "action", index: "Action", jsonmap: "action" }, 
{label: "Assignee",name: "assignee",index: "Assignee",jsonmap: "assignee"} 
], 
sortname : "Title", 
sortorder : "desc", 
shrinkToFit: true, 
viewrecords: true, 
jsonReader : { 
    root: "rows", 
    page: "page", 
    total: "total", 
    records: "records", 
    repeatitems: false, 
    cell: "cell", 
    id: "id", 
    userdata: "userdata", 
    subgrid: {root:"rows", 
     repeatitems: true, 
     cell:"cell" 
     }  
} 
}); }); 

任何一個可以幫助解決t他?當我看到jqgrid示例代碼時,看起來很簡單,但我沒有看到任何輸出。輸出頁面爲空。請幫助解決。

感謝

回答

0

我建議你看看下面的教程第一:

的jqGrid和Spring 3 MVC集成教程在http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration.html

或許可以從該教程中找到答案,或者至少將細化你在找什麼。

+0

感謝克里斯參考樣本項目,我已經在使用傑克遜插件Java對象轉換成JSON結下不解之緣。如果我將\t 包含到我的dispatcher-servlet.xml中,我得到了saxparser異常。我已將Jackson jar保存在我的classpath中。誰能幫我? – Sabarish 2011-03-22 11:23:46

0

@ user669789首先通過克里斯提到的克拉姆斯教程。我也是jqgrid的新手,但是教程和源代碼給了我很多幫助。另一種方法是根據jqgrid中的jsonreader使用硬編碼的json值並看看它是否得到填充。對this example by Oleg

嘗試看看其寫入PrintWriter的,看看天氣,您可以通過此網址調用它獲得JSON輸出

0

在控制器,你可以嘗試將列表轉換爲Json.That的Json值會直接綁定到jqGrid的,如果所有的列名相匹配

控制器類

@RequestMapping("/page4.htm") 
public @ResponseBody String getJQGridData(HttpServletRequest request, HttpServletResponse response) { 
System.out.println("xml data"); 
List<TaskBean> taskList = new ArrayList<TaskBean>(); 
    taskList.add(new TaskBean("Task1", "K-CY-329", "144", "G-3", "1", "Pending", "XYZ")); 
    taskList.add(new TaskBean("Task2", "K-CY-356", "165", "A-10", "4", "Closed", "ABC")); 
    taskList.add(new TaskBean("Task3", "K-CY-343", "768", "B-12", "3", "Pending", "IJK")); 
    taskList.add(new TaskBean("Task4", "K-CY-786", "918", "F-9", "2", "Open", "PQR")); 
    TaskBeanList tsklst = new TaskBeanList("1", "5", "20", taskList); 
    Gson gson = new GsonBuilder().setPrettyPrinting().create(); 
    String result = gson.toJson(tsklst); 
    return result; 
} 

下載gson.jar然後試試這個

它的工作了me.You也可以從這裏

http://www.jriyazahamed.blogspot.com/