日期:2014-05-20  浏览次数:20610 次

Spring返回jason数据到jqgrid不显示
弄了两天了,知道的大佬帮忙看一下哪里的错误!!
调试的时候jqGrid能通过test.do进入后台的test方法,test方法也成功执行了,可是表格显示一直是空的

jsp code:
.....
jQuery("#list").jqGrid({
   
  url : "..test.do",  
   
  mtype : "post",  
  datatype : 'json', 
   
  colNames:['编号','姓名', '电话'],
  colModel :[
  {name:'id', index:'id', width:55, sorttype:'int'},
  {name:'name', index:'name', width:90},
  {name:'phone', index:'phone', width:100}
  ],
  pager: '#pager',
  rowNum:10,
  viewrecords: true,
  multiselect: true,
  caption: 'My first grid'
.........

java code:
public class AttachActionController extends MultiActionController{
......
public ModelAndView test(HttpServletRequest request,
HttpServletResponse response) throws Exception {

response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
request.setCharacterEncoding("UTF-8");


PrintWriter out = response.getWriter();

String page = request.getParameter("page"); // 取得当前页数
String rows = request.getParameter("rows"); // 取得每页显示行数
int totalRecord = 5; // 总记录数(应根据数据库取得,在此只是模拟)
int totalPage = totalRecord % Integer.parseInt(rows) == 0 ? totalRecord
/ Integer.parseInt(rows) : totalRecord / Integer.parseInt(rows)
+ 1; // 计算总页数

int index = (Integer.parseInt(page) - 1) * Integer.parseInt(rows); // 开始记录数
int pageSize = Integer.parseInt(rows);
// 以下模拟构造JSON数据对象
String json = "{total: " + totalPage + ", page: " + page
+ ", records: " + totalRecord + ", rows: [";
for (int i = index; i < pageSize + index && i < totalRecord; i++) {
json += "{cell:['ID " + i + "','NAME " + i + "','PHONE " + i
+ "']}";
if (i != pageSize + index - 1 && i != totalRecord - 1) {
json += ",";
}
}
json += "]}";
// System.out.println(json);
out.write(json); // 将JSON数据返回页面

return null;
}


......
}

------解决方案--------------------
用个httpwatch查看下请求返回的JSON内容,如果返回的json内容正确,那就应该是jQuery("#list").jqGrid这个控件的使用哪里没有写正确了。