Hooked to a simple CRUD paging application as follows:

  • In users.jsp:

    <div id="pagination"> <c:url value="/users" var="prev"> <c:param name="page" value="${page-1}"/> </c:url> <c:if test="${page > 1}"> <a href="<c:out value="${prev}" />" class="pn prev">Prev</a> </c:if> <c:forEach begin="1" end="${maxPages}" step="1" varStatus="i"> <c:choose> <c:when test="${page == i.index}"> <span>${i.index}</span> </c:when> <c:otherwise> <c:url value="/users" var="url"> <c:param name="page" value="${i.index}"/> </c:url> <a href='<c:out value="${url}" />'>${i.index}</a> </c:otherwise> </c:choose> </c:forEach> <c:url value="/users" var="next"> <c:param name="page" value="${page + 1}"/> </c:url> <c:if test="${page + 1 <= maxPages}"> <a href='<c:out value="${next}" />' class="pn next">Next</a> </c:if> 

On the same page, in addition to the list of users, there is a form for adding / editing them, well, in the users table there are buttons for each position:

 <td><a href="<c:url value='/edit/${user.id}'/>">Edit</a></td> <td><a href="<c:url value='/remove/${user.id}'/>">Delete</a></td> 
  • In the controller:

     @RequestMapping(value = "users", method = RequestMethod.GET) public ModelAndView listUsers(@RequestParam(required = false) Integer page) { ModelAndView modelAndView = new ModelAndView("users"); List<User> users = userService.listUsers(); PagedListHolder<User> pagedListHolder = new PagedListHolder<User>(users); pagedListHolder.setPageSize(pageSize); modelAndView.addObject("maxPages", pagedListHolder.getPageCount()); if(page==null || page < 1 || page > pagedListHolder.getPageCount()) page=1; modelAndView.addObject("page", page); if(page == null || page < 1 || page > pagedListHolder.getPageCount()){ pagedListHolder.setPage(0); modelAndView.addObject("listUsers", pagedListHolder.getPageList()); } else if(page <= pagedListHolder.getPageCount()) { pagedListHolder.setPage(page-1); modelAndView.addObject("listUsers", pagedListHolder.getPageList()); } modelAndView.addObject("user", new User()); return modelAndView; } 

At this stage, everything works, the list is paginated, navigation works, BUT, for example, being on page 2 we delete the user

 @RequestMapping(value = "/remove/{id}") public String removeUser(@PathVariable("id") int id) { this.userService.removeUser(id); return "redirect:/users"; } 

removal is successful, but the display of the list of users is reset to the 1st page. The same with add / edit operations. It is clear, because in each case

return "redirect: / users";

without request parameter with page number.

I am not yet strong in either JSP or Spring. Tell me, how can this parameter be transmitted and operated on it in the controller?

Thank!

  • It seems to me that it is easier to make it with javascript! You will simply send to the server the command to delete the user remaining on the current page without reloading it - JVic
  • I could be wrong, but would JSP display the modified list without a reboot? Listener probably needed, right? - maxXx
  • If you use scripts then convert the whole page without reloading, again the list and most likely the entire pagination will be more logical on JS - JVic
  • Is logical. I'll try to redo the whole page on JS. - maxXx
  • So that you don’t have to strain hard at first about the front end (oh, they’re throwing me in slippers now) try JQuery with its DataTable there and a simple pagination is implemented and a whole lot more! - JVic

2 answers 2

In projects we use the title X-Range: 0-19/300 . Where 0-19 are the displayed records (from 0 to 19) and 300 are the total records. Total billed by the server when responding.

In the controller, such a header is easy to get from the query and limit the result set to it.

The client uses JS for asynchronous requests.

    The easiest, but not the most reliable way is to take the REFERER header pointing to the address from which the request came, and send it to it:

     @RequestMapping(value = "/remove/{id}") public String removeUser(@PathVariable("id") int id, HttpServletRequest request) { this.userService.removeUser(id); return Optional .ofNullable(request.getHeader("referer")) .map(url -> "redirect:" + url) .orElse("redirect:/users/"); // На случай, если заголовок не установлен } 

    A more reliable option is to explicitly pass a page to the removal controller to which you need to return.

     @RequestMapping(value = "/remove/{id}") public String removeUser(@PathVariable("id") int id, @RequestParam(required = false, default = "1") int page) { this.userService.removeUser(id); String url = MvcUriComponentsBuilder.fromMethodName(UserController.class, "listUsers", page) .build() .toUriString(); return "redirect:" + url; }