今天利用SSH+json结合jqGrid做了个小例子,能从数据库查出数据并显示到JSP页面,可是查出来的数据重复了:比如数据库只有2条数据,查出来之后变成了4条。经后台打印查看发现,是jqGrid连续执行了两次查询操作。找了好久都搞不懂为什么!?�0�2先上张图:�0�21、JSP页面代码�0�2jQuery(document).ready(function(){ $("#list").jqGrid({ url:'<%=path%>/jqGrid/jqGrid!getData.action', datatype: "json", height:230, colNames:['书号','书名','作者'], colModel:[ {name:'bookid',index:'bookid', width:100,editable:true,editoptions:{size:20},editrules:{required:true}}, {name:'bookname',index:'bookname', width:200,editable:true,editoptions:{size:20},editrules:{required:true}}, {name:'author',index:'author', width:150,editable:true,editoptions:{size:20}} ], sortname:'bookid', sortorder:'asc', viewrecords:true, rowNum:10, rowList:[10,20,30], loadonce : true, editurl:'', jsonReader: { root:"dataRows", // 数据行(默认为:rows) page: "curPage", // 当前页 total: "totalPages", // 总页数 records: "totalRecords", // 总记录数 repeatitems : false // 设置成false,在后台设置值的时候,可以乱序。且并非每个值都得设 }, prmNames:{rows:"page.pageSize",page:"page.curPageNo",sort:"page.orderBy",order:"page.order"}, pager:"#pager", caption: "jqGrid+SSH" }).navGrid('#pager',{view:true,edit:true,add:true,del:true,refresh:true}); }); �0�2
�0�2�0�2�0�22、action代码�0�2public class ListAction extends JsonBaseAction { private IBookServices bookServices; public String getData() throws Exception{ System.out.print("获取数据..."); List<Books> list=this.bookServices.ListAll(); // 给另外三个返回参数设值 int totleSize=list.size(); System.out.print("共"+totleSize+"条数据
"); int p = totleSize / this.page.getPageSize(); if (p % this.page.getPageSize() > 0) p += 1; setTotalPages(p); // 总页数 if (this.page.getCurPageNo() > 0) this.setCurPage(this.page.getCurPageNo()); // 当前页 else this.setCurPage(1); setTotalRecords(totleSize); // 总记录数 for (int i = 0; i < totleSize; i++) { // 定义一个Map<String,Object>存放一行行数据。 Map<String, Object> row = new HashMap<String, Object>(); if (i >= totleSize) break; Books b = list.get(i); row.put("bookid",b.getBookid()); row.put("bookname",b.getBookname()); row.put("author",b.getAuthor()); dataRows.add(row); } return SUCCESS; } @Override public int getTotalPages() { return this.totalPages; } @Override public int getCurPage() { return this.curPage; } @Override public int getTotalRecords() { return this.totalRecords; } @Override public List<Map<String, Object>> getDataRows() { return this.dataRows; } public void setBookServices(IBookServices bookServices) { this.bookServices = bookServices; } }�0�2�0�2说明下的就是spring和json冲突的问题,我把bookServices的getter方法去掉了。�0�2另外我试过getData() 方法return的是null,但是这样数据显示不出来。�0�23、配置文件�0�2<package name="jqGrid" namespace="/jqGrid" extends="json-default"> <action name="jqGrid" class="jqGridBean"> <result type="json"/> </action> </package>�0�2jqGridBean在spring配置文件里配置了,这里就不贴代码了。�0�2
温馨提示:内容为网友见解,仅供参考