jsp - How do I add simple pagination for Spring MVC -
this controller 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 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 can see list of 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 add simple pagination this?
i did it. if else need it, 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; }
Comments
Post a Comment