开发者

jqgrid json data type with spring mvc

开发者 https://www.devze.com 2023-02-18 02:23 出处:网络
I am new to jqgrid and I tried to simulate above code, but it is not working. In server side i have created class

I am new to jqgrid and I tried to simulate above code, but it is not working. In server side i have created class

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;
}

Spring Controller Code

@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; 
}

Client side JQGrid JS code

jQuery(document).ready(function(){ 
jQuery("#list3").jqGrid({
autowidth: true,
datatype : "json",
url: "http://localhost:8080/SpringMVC/page4.htm",
mtype: 'get',
colNames : ["Title","O开发者_如何学编程rder 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"
      }       
}
}); });

Can any one help to resolve this? When I see jqgrid sample code, looks simple but i dont see any output. The output page is empty. Pls help to resolve.

Thanks


I suggest you look at the following tutorial first:

jqGrid and Spring 3 MVC Integration Tutorial at http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration.html

You might find your answers from that tutorial or at least it will refine what you're looking for.


@user669789 first of all go through the krams tutorial that chris has mentioned.i am also new to jqgrid but that tutorial and source helped me a lot.another alternative is use hard coded json values according to your jsonreader in the jqgrid and see if itz get populated. have a look on this example by Oleg

try to write it to the printwriter and see weather you can get the json output by invoking it through the URL


In Controller you can try to convert list to Json.That Json value will directly bind to JQGrid if all column names matches

Controller Class

@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; 
}

Download gson.jar and then try this

Its worked for me.You can also refer to the sample Project from here

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号