<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'jgGird.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" type="text/css" media="screen" href="<%=path %>/css/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" media="screen" href="<%=path %>/css/jquery-ui-1.8.2.custom.css" />
<script type="text/javascript" src="<%=path %>/js/jquery-1.7.2.js"></script>
<script type="text/javascript" src="<%=path %>/js/jquery-ui-1.8.2.custom.min.js"></script>
<script type="text/javascript" src="<%=path %>/js/i18n/grid.locale-en.js"></script>
<script type="text/javascript" src="<%=path %>/js/i18n/grid.locale-cn.js"></script>
<script type="text/javascript" src="<%=path %>/js/jquery.jqGrid.min.js"></script>
<script type="text/javascript">
$(function()
    {
        $("#gridTable").jqGrid({
                url:'listProduct',
                dataType:'json',
                colNames:['商品编号','商品名称','商品价格'],
                colModel:[
                    {name:'id',index:'id',width:100},   
                    {name:'pname',index:'pname',width:100},
                    {name:'price',index:'price',width:100,}
                    
                ],
                pager:'#pager2',
                rowNum:-1,
                rowList:[10,20,30],
                sortname:'id',
                sortorder:'desc',
                viewrecords:true,
                caption:'商品总览'
            }).navGrid("#pager2",{edit:false,add:false,del:false,search:false});
        });
  
</script>
  </head>
  
  <body>
     <table id="gridTable"></table>
     <div id="pager2"></div>
  </body>
</html>

解决方案 »

  1.   

    @RequestMapping("/listProduct")
    public void listProductJson(HttpServletRequest request,
             HttpServletResponse response) throws IOException{
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json");
    request.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();
    System.out.println("OK");
    List<Product> products = productService.find("from Product");
    JSONArray json = new JSONArray();
    for(Product product:products){
    JSONObject temp = new JSONObject();
    temp.put("id", product.getId());
    temp.put("pname", product.getPname());
    temp.put("price", product.getPrice());
    json.add(temp);
    }
     out.print(json);
    }
      

  2.   

    用FireBug调试一下,先从控制台看看后台返回的Json格式是否正确