Spring mvc pagination
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.v开发者_C百科alueOf(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:
http://pastebin.com/ybKSXzyq
Can anybody suggest better solution for this?
I would suggest you to look at Display Tag Library
, for pagination.
I'm doing full implementation of it I did JQGridPage.java class to contain data for jqgrid as
public class JQGridPage<T> {
private Integer page;
private Integer total;
private Long records;
private List<T> rows;
....
}
and in controller method I'm filling it and send it as return of method
@RequestMapping(value = "", method = RequestMethod.GET, params = "page")
@ResponseBody
public JQGridPage<T> listPages(
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "rows", defaultValue = "10") int rows,
@RequestParam(value = "sidx", defaultValue = "id") String sortField,
@RequestParam(value = "sord", defaultValue = "ASC") String sortDirection,
@RequestParam(value = "searchField", defaultValue = "id") String searchField,
@RequestParam(value = "searchOper", defaultValue = "eq") String searchOper,
@RequestParam(value = "searchString", defaultValue = "") String searchString) {
.....
JQGridPage<T> jqGrid = new JQGridPage<T>();
jqGrid.setPage(page);
jqGrid.setTotal(pageOfCustomer.getTotalPages());
jqGrid.setRecords(pageOfCustomer.getTotalElements());
jqGrid.setRows(pageOfCustomer.getContent());
return jqGrid;
}
This is my pangination result in Spring MVC
精彩评论