Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add simple pagination for Spring MVC

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?

like image 744
Simon Avatar asked Aug 07 '15 17:08

Simon


1 Answers

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;
    }
like image 91
Simon Avatar answered Nov 04 '22 03:11

Simon