title: Spring mvc pagination
body: <p>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 :</p>
  
  <pre><code>@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 &gt; 0) {
              totalPages = (int) Math.ceil(totalCount/rownumber);
          }
  
          if (Integer.valueOf(page) &gt; 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&lt;Record&gt; 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);
      }
  </code></pre>
  
  <p>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:</p>
  
  <p><a href="http://pastebin.com/ybKSXzyq" rel="nofollow">http://pastebin.com/ybKSXzyq</a></p>
  
  <p>Can anybody suggest better solution for this?</p>