在jqGrid中显示Json数组数据

在jqGrid中显示Json数组数据,json,spring-mvc,jqgrid,Json,Spring Mvc,Jqgrid,我正在使用SpringMVC、hibernate和jetty服务器开发基于web的项目。。 我想显示关于json响应的数据。 这是我在控制器类中的Json方法。(我需要在我的网格中显示Harbors的详细信息) @表(name=“HARBOUR”) 公共级港口{ @Id @Column(name="HARBOUR_ID") @GeneratedValue private Integer harbourId; @Column(name="HARBOURCODE") private String

我正在使用SpringMVC、hibernate和jetty服务器开发基于web的项目。。 我想显示关于json响应的数据。 这是我在控制器类中的Json方法。(我需要在我的网格中显示Harbors的详细信息)

@表(name=“HARBOUR”) 公共级港口{

@Id
@Column(name="HARBOUR_ID")
@GeneratedValue
private Integer harbourId;

@Column(name="HARBOURCODE")
private String harbourCode;

@Column(name="HARBOURNAME")
private String harbourName;

@Column(name="STREETNO")
private String streetNo;

@Column(name="STREETONE")
private String streetOne;

@Column(name="STREETTWO")
private String streetTwo;

@Column(name="CITYNAME")
private String cityName;

@Column(name="PROVINCE")
private String province;

@Column(name="ALL_ID")
private String allocationId; & Getter & Setters
&这是我的控制器类方法,用于生成json数组作为响应

@RequestMapping("/selectHarbour")
public ModelAndView selectHarbour(Map<String, Object> map,HttpServletRequest request,
        HttpServletResponse response) {
    try {

    List <Harbour> list= harbourService.listHarbour();
    JSONArray jsonArray=new JSONArray();
    for(Harbour harbour:list){
        JSONArray array=new JSONArray();
        array.put(harbour.getHarbourId());
        array.put(harbour.getHarbourCode());
        array.put(harbour.getHarbourName());
        array.put(harbour.getCityName());
        array.put(harbour.getProvince());
        jsonArray.put(array);
    }
    response.getWriter().write(jsonArray.toString());
    return null;
    }catch(Exception exception){
        System.out.println("error is "+exception);
    }
    return null;
}
@RequestMapping(“/selectharbor”)
公共模型和视图选择港口(地图地图、HttpServletRequest请求、,
HttpServletResponse(响应){
试一试{
List List=harbourService.listHarbour();
JSONArray JSONArray=新的JSONArray();
(海港:名单){
JSONArray数组=新的JSONArray();
array.put(harbour.getHarbourId());
array.put(harbour.getHarbourCode());
array.put(harbor.getHarborName());
array.put(harbor.getCityName());
array.put(harbor.getProvince());
jsonArray.put(数组);
}
response.getWriter().write(jsonArray.toString());
返回null;
}捕获(异常){
System.out.println(“错误为”+异常);
}
返回null;
}
最后,这是我的Jquery,用于生成jqGrid

<td colspan="2">
        <!-- Insert Data Tables -->
        <table id="list5"></table> 
        <div id="pager5"></div> 
        <br /> 
        <a href="#" id="a1">Get data from selected row</a> 
        <br /> 
    </td>

<script type="text/javascript">
jQuery("#list5").jqGrid({ 
    url:'selectHarbour.html', 
            datatype: "json", 
            colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'], 
            colModel:[ 
                       {name:'id',index:'id', width:55}, 
                       {name:'invdate',index:'invdate', width:90}, 
                       {name:'name',index:'name', width:100}, 
                       {name:'amount',index:'amount', width:80, align:"right"}, 
                       {name:'tax',index:'tax', width:80, align:"right"}, 
                       {name:'total',index:'total', width:80,align:"right"}, 
                       {name:'note',index:'note', width:150, sortable:false} 
                      ], 
                 rowNum:10, 
                 rowList:[10,20,30], 
                 pager: '#pager5', 
                 sortname: 'id', 
                 viewrecords: true, 
                 sortorder: "desc", 
                 caption:"Simple data manipulation", 
                 editurl:"" 
                }).navGrid("#pager5",
                        {edit:false,add:false,del:false}); 
                        jQuery("#a1").click( function(){ 
                            var id = jQuery("#list5").jqGrid('getGridParam','selrow'); 
                            if (id) { 
                                var ret = jQuery("#list5").jqGrid('getRowData',id); 
                                alert("id="+ret.id+" invdate="+ret.invdate+"..."); 
                                } else { alert("Please select row");} 
                            });
</script>



jQuery(“#list5”).jqGrid({ url:'selectharbor.html', 数据类型:“json”, colname:['Inv No','Date','Client','Amount','Tax','Total','Notes',], colModel:[ {名称:'id',索引:'id',宽度:55}, {名称:'invdate',索引:'invdate',宽度:90}, {名称:'name',索引:'name',宽度:100}, {名称:'amount',索引:'amount',宽度:80,对齐:“right”}, {名称:'tax',索引:'tax',宽度:80,对齐:“right”}, {名称:'total',索引:'total',宽度:80,对齐:“right”}, {名称:'note',索引:'note',宽度:150,可排序:false} ], rowNum:10, 行列表:[10,20,30], 传呼机:“#第5页”, sortname:'id', viewrecords:是的, 巫师:“描述”, 标题:“简单的数据操作”, 编辑URL:“ }).navGrid(“#第5页”), {编辑:假,添加:假,删除:假}); jQuery(“#a1”)。单击(函数(){ var id=jQuery(“#list5”).jqGrid('getGridParam','selrow'); 如果(id){ var ret=jQuery(“#list5”).jqGrid('getRowData',id); 警报(“id=“+ret.id+”invdate=“+ret.invdate+”); }else{alert(“请选择行”);} });
&

Firebug显示了我这样的反应

[5,“CLM”、“科伦坡”、“科伦坡”、“西部”]、[6,“HMB”、“Hambanthota”、“科伦坡07”、“南部”]]


那么,朋友们,我的错误在哪里呢?我的网格中没有显示数据。您的控制器需要进行一些修改;我假设客户机在本例中发布了一些信息:

@Entity
@SuppressWarnings("unchecked")
@RequestMapping(value = "/selectHarbour", method = POST)
public ResponseEntity<String> createEntity(HttpServletRequest request, @RequestBody Specialist specialist) {
    specialistBo.save(specialist);
    final int id = specialist.getId();
    URI uri = new UriTemplate("{requestUrl}/{username}").expand(request.getRequestURL().toString(), id);
    final HttpHeaders headers = new HttpHeaders();
    headers.put("Location", singletonList(uri.toASCIIString()));
    return new ResponseEntity<String>(headers, HttpStatus.CREATED);
}
@SuppressWarnings(“未选中”)
@请求映射(value=“/selectharbor”,method=POST)
公共响应实体createEntity(HttpServletRequest,@RequestBody Specialist){
专家保存(专家);
final int id=specialist.getId();
uriuri=newuritemplate(“{requestUrl}/{username}”).expand(request.getRequestURL().toString(),id);
最终HttpHeaders=新HttpHeaders();
headers.put(“Location”,singletonList(uri.toASCIIString());
返回新的ResponseEntity(标题,HttpStatus.CREATED);
}
如果你想得到一些东西,它应该类似于:

@RequestMapping(method = GET)
public
@ResponseBody
Page<Specialist> listEntities(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                              @RequestParam(value = "max", required = false, defaultValue = "5") int max,
                              @RequestParam(value = "sidx", required = false, defaultValue = "grid_name") String sidx,
                              @RequestParam(value = "sord", required = false, defaultValue = "desc") String sord,
                              @RequestParam(value = "filters", required = false, defaultValue = "") String filters) {
    return specialistBo.getSpecialist(page, max, stringStringMap.get(sidx), sord, filters);
} 
@RequestMapping(方法=GET)
平民的
@应答器
页面列表(@RequestParam(value=“Page”,required=false,defaultValue=“1”)int Page,
@RequestParam(value=“max”,required=false,defaultValue=“5”)int max,
@RequestParam(value=“sidx”,required=false,defaultValue=“grid\u name”)字符串sidx,
@RequestParam(value=“sord”,required=false,defaultValue=“desc”)字符串sord,
@RequestParam(value=“filters”,required=false,defaultValue=“”)字符串过滤器){
返回specialistBo.getSpecialist(页面、最大值、stringmap.get(sidx)、sord、过滤器);
} 

@Transactional(readOnly=true)
公共页面getSpecialist(int-Page、int-max、String-sidx、String-sord、String-filters){
int count=((长)getSession().createQuery(“从专家中选择计数(*)).iterate().next()).intValue();
最终整数开始=最大*页面-最大;
@抑制警告(“未选中”)
列表=
getSession().createCriteria(Specialist.class)
.add(Restrictions.sqlRestriction(“名称类似“%”按%s%s排序限制%s,%s”)).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
返回新页面(列表、页面、最大值、计数);
}

@XmlRootElement
@XMLSEEALLO({Specialist.class,Aptitude.class})
公共类页面{
私有列表行;
私人网页;
私人int max;
私人整数合计;

您需要为页面创建getter和setter。希望这有点帮助

是否需要jsonReader选项。?
   @Transactional(readOnly = true)
    public Page<Specialist> getSpecialist(int page, int max, String sidx, String sord, String filters) {
        int count = ((Long) getSession().createQuery("select count(*) from Specialist").iterate().next()).intValue();
        final int start = max * page - max;
        @SuppressWarnings("unchecked")
        List<Specialist> list =
                getSession().createCriteria(Specialist.class)
                        .add(Restrictions.sqlRestriction("name like '%%' order by %s %s limit %s,%s")).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
        return new Page<Specialist>(list, page, max, count);
    }
@XmlRootElement
@XmlSeeAlso({Specialist.class, Aptitude.class})
    public class Page<T> {
        private List<T> rows;
        private int page;
        private int max;
        private int total;