在jqGrid中显示Json数组数据
我正在使用 Spring MVC、hibernate 和 java 开发基于 Web 的项目。 jquery 与 jetty 服务器.. 我想显示有关 json 响应的数据。 这是我在控制器类中的 Json 方法。(我需要在我的网格中显示港口的详细信息)
@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","Hambantota","Colombo 07","Southern"]]
那么朋友们我的错误在哪里? ? ?数据未显示在我的网格中
I'm developing web based project with Spring MVC, hibernate & jquery with jetty server..
i want to display data regarding to json response.
here is my Json method in Controller Class.(i need show Harbors' details in my grid)
@Entity
@Table(name="HARBOUR")
public class 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
& this is my Controller Class Method that is used to generate json array as response
@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;
}
And finally this is my Jquery for generate 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>
&
Firebug shows my response like this..
[[5,"CLM","Colombo","Colombo","Western"],[6,"HMB","Hambanthota","Colombo 07","Southern"]]
Then friends where is my error.? ? ?data not shown in my grid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的控制器需要一些修改;我假设客户端在这个例子中发布一些东西:
如果你想获取一些东西,它应该类似于这样:
你将需要为 Page 创建 getter 和 setter。希望这会有所帮助
Your controller needs some modifications; I am assuming the client is POSTing something in this example:
If you want to GET something it should be similar to this:
You will need to create getters and setters for Page. Hope this helps a bit