gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:46:37 25 4
gpt4 key购买 nike

这是我的用户 View 页面 Controller :

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

如何添加简单的分页?

最佳答案

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

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

关于spring - 如何为 Spring MVC 添加简单的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31883643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com