11

This is my controller for Users view page:

@RequestMapping(value="/list")
    public ModelAndView listOfUsers() {
        ModelAndView modelAndView = new ModelAndView("list-of-users");

        List<User> users = userService.getUsers();
        PagedListHolder<User> pagedListHolder = new PagedListHolder<>(users);
        //pagedListHolder.setPageSize(1);
        modelAndView.addObject("users", pagedListHolder);

        return modelAndView;
    }

And this is my JSP page:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<?xml version="1.0" encoding="ISO-8859-1" ?>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>List of users</title>
</head>
<body>
<h1>List of users</h1>
<p>Here you can see the list of the users, edit them, remove or update.</p>
<table border="1px" cellpadding="0" cellspacing="0" >
<thead>
<tr>
<th width="10%">id</th><th width="15%">name</th><th width="10%">age</th><th width="10%">is Admin?</th><th width="10%">create date</th><th width="10%">actions</th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${users.pageList}">
<tr>
    <td>${user.id}</td>
    <td>${user.name}</td>
    <td>${user.age}</td>
    <td>${user.isAdmin}</td>
    <td>${user.createdDate}</td>
    <td>
    <a href="${pageContext.request.contextPath}/user/edit/${user.id}.html">Edit</a><br/>
    <a href="${pageContext.request.contextPath}/user/delete/${user.id}.html">Delete</a><br/>
    </td>

</tr>
</c:forEach>
<p><a href="${pageContext.request.contextPath}/user/search-users.html">Search</a></p>
</tbody>
</table>

<p><a href="${pageContext.request.contextPath}/index.html">Home page</a></p>



</body>
</html>

How can I add simple pagination to this?

Madness
  • 2,730
  • 3
  • 20
  • 29
Simon
  • 997
  • 1
  • 15
  • 29
  • can you use Spring Data JPA repositories? has built in paging and sorting – Neil McGuigan Aug 07 '15 at 21:50
  • 1
    This is a great example - http://springinpractice.com/2012/05/11/pagination-and-sorting-with-spring-data-jpa. If you do not use Spring Data, you can still implement your own repositories that support pagination with, for example, [Hibernate](http://stackoverflow.com/questions/19649194/hibernate-pagination-mechanism). – Constantine Aug 07 '15 at 22:18
  • Thanks. No i dont use Spring Data JPA.. I need paginataion like this (prev 1 2 .. 4 5 next ) – Simon Aug 08 '15 at 05:45

2 Answers2

13

I did it. If anybody else need it, this is my code:

JSP:

<div id="pagination">

    <c:url value="/user/list" 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="/user/list" 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="/user/list" 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>
</div>

Controller:

@RequestMapping(value="/list")
    public ModelAndView listOfUsers(@RequestParam(required = false) Integer page) {
        ModelAndView modelAndView = new ModelAndView("list-of-users");

        List<User> users = userService.getUsers();
        PagedListHolder<User> pagedListHolder = new PagedListHolder<>(users);
        pagedListHolder.setPageSize(5);
        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("users", pagedListHolder.getPageList());
        }
        else if(page <= pagedListHolder.getPageCount()) {
            pagedListHolder.setPage(page-1);
            modelAndView.addObject("users", pagedListHolder.getPageList());
        }

        return modelAndView;
    }
Simon
  • 997
  • 1
  • 15
  • 29
  • 2
    You have check `page` for `null`, `<1` or `>pageListHolder.getPageCount()` twice, but after first check you anyway get `page == 1` – Sild Jun 15 '16 at 19:51
  • @Simon Hey sorry but do you have any ideas on how to add the Go to First Page and Go to Last Page? – QWERTY Sep 04 '20 at 06:14
0

I have used your code it's working fine pagination But i want page link in limit. I have 5000 row just want to display 50 row on the page and page link should be display only 1 to 10 when click on next then should be display 10 to 20 page link so on but problem is the display 100 page link at time. how will resolve this problem.

Deepak Singh
  • 460
  • 2
  • 7
  • 19