如何为Spring MVC添加简单的分页

时间:2015-08-07 17:39:08

标签: spring jsp spring-mvc pagination

这是我的用户视图控制器页面:

@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;
    }

这是我的JSP页面:

<%@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>

如何为此添加简单的分页?

2 个答案:

答案 0 :(得分:13)

我做到了。 如果有其他人需要它,这是我的代码:

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>

控制器:

@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;
    }

答案 1 :(得分:0)

我已经使用了您的代码,它的分页效果很好,但是我想限制页面链接。我有5000行,只是想在页面上显示50行,页面链接在单击“下一步”时应该仅显示1到10,然后应该显示10到20页的链接,依此类推,但是问题是当时显示100页的链接。如何解决这个问题。