【问题标题】:Display Json array data in jqGrid在 jqGrid 中显示 Json 数组数据
【发布时间】:2011-09-19 15:00:00
【问题描述】:

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

@Entity

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

最后这是我用于生成 jqGrid 的 Jquery。

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

&

Firebug 像这样显示我的响应..

[[5,"CLM","Colombo","Colombo","Western"],[6,"HMB","Hambanthota","Colombo 07","Southern"]]

那么朋友们我的错误在哪里。? ? ?数据未显示在我的网格中

【问题讨论】:

  • 是否需要jsonReader选项?

标签: json spring-mvc jqgrid


【解决方案1】:

您的控制器需要一些修改;我假设客户端在这个例子中发布了一些东西:

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

如果你想得到一些东西,它应该类似于:

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

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

您需要为 Page 创建 getter 和 setter。希望这能有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    相关资源
    最近更新 更多