Spring mvc 分页
你好,我在我的项目中使用 jqgrid,我需要实现分页,所以我在 php 中找到了一个代码并将其复制到 java :
@RequestMapping(value = "getgriddata", method = RequestMethod.GET)
public @ResponseBody
String getGrid(@RequestParam("page") String page, @RequestParam("rows") int rownumber, @RequestParam("sidx") String sortBy, @RequestParam("sord") String sortOrder,@RequestParam(value = "_search") String search, @RequestParam(value="filters", required = false) String filters) {
int totalCount = dao.getTotalRecordCount();
CustomJsonResponse response = new CustomJsonResponse();
int totalPages = 0;
if (totalCount > 0) {
totalPages = (int) Math.ceil(totalCount/rownumber);
}
if (Integer.valueOf(page) > totalPages) {
page = String.valueOf(totalPages);
}
Gson jsonConverter = new Gson();
int start = (rownumber * Integer.valueOf(page)) - rownumber;
Filters searchFilter = null;
if(Boolean.valueOf(search) == true){
searchFilter = jsonConverter.fromJson(filters, Filters.class);
}
// Retrieve records from database
List<Record> recorBatch = dao.getRecords(start, rownumber, sortBy, sortOrder, search, searchFilter);
// Assign the result from the service to this response
response.setRows(recorBatch);
response.setTotal(String.valueOf(totalPages));
response.setRecords(String.valueOf(stagingLoadBatch.size()));
response.setPage(page);
return jsonConverter.toJson(response);
}
当我处理整个数据集时,这可以正常工作,但是当我缩小范围时搜索,即选择某个时间间隔内的记录,然后分页失败。我意识到这是不好的做法,我从这段代码中复制了它以进行分页:
有人可以为此提出更好的解决方案吗?
Hello I'm using jqgrid for my project, and I needed to implement the pagination so I found a code in php and copied it over to java :
@RequestMapping(value = "getgriddata", method = RequestMethod.GET)
public @ResponseBody
String getGrid(@RequestParam("page") String page, @RequestParam("rows") int rownumber, @RequestParam("sidx") String sortBy, @RequestParam("sord") String sortOrder,@RequestParam(value = "_search") String search, @RequestParam(value="filters", required = false) String filters) {
int totalCount = dao.getTotalRecordCount();
CustomJsonResponse response = new CustomJsonResponse();
int totalPages = 0;
if (totalCount > 0) {
totalPages = (int) Math.ceil(totalCount/rownumber);
}
if (Integer.valueOf(page) > totalPages) {
page = String.valueOf(totalPages);
}
Gson jsonConverter = new Gson();
int start = (rownumber * Integer.valueOf(page)) - rownumber;
Filters searchFilter = null;
if(Boolean.valueOf(search) == true){
searchFilter = jsonConverter.fromJson(filters, Filters.class);
}
// Retrieve records from database
List<Record> recorBatch = dao.getRecords(start, rownumber, sortBy, sortOrder, search, searchFilter);
// Assign the result from the service to this response
response.setRows(recorBatch);
response.setTotal(String.valueOf(totalPages));
response.setRecords(String.valueOf(stagingLoadBatch.size()));
response.setPage(page);
return jsonConverter.toJson(response);
}
This works ok when I'm working with entire set of data, but when I narrow the search, i.e. select records in some time interval, then the pagination breaks down. I realized this is bad practice, I've copied it from this code for pagination:
Can anybody suggest better solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您查看
显示标签库
进行分页。I would suggest you to look at
Display Tag Library
, for pagination.我正在完整实现它,我做了 JQGridPage.java 类来包含 jqgrid 的数据
,并在控制器方法中填充它并将其作为方法的返回发送
I'm doing full implementation of it I did JQGridPage.java class to contain data for jqgrid as
and in controller method I'm filling it and send it as return of method
这是我在 Spring MVC 中的分页结果
This is my pangination result in Spring MVC