我正在尝试使用j2ee构建一个网络应用程序。我正在尝试在表格中显示项目列表,如果返回的项目超过25个,则将列表分成多个页面。
以下是一个例子:
我该怎么办?提前谢谢。
答案 0 :(得分:0)
您可以使用spring-data来执行此操作。 你的jsp代码显示记录:
<c:forEach var="user" items="${page.content}">
<p>${user.name}</p>
</c:forEach>
用于显示分页控件的jsp代码(如附图中所示):
<c:if test="${page.totalPages > 1}">
<c:set var="THRESHOLD" value="3" />
<div class="pagination">
<ul>
<c:choose>
<c:when test="${page.number != 0}">
<li><a href="${home}/${targetUrl}?page=${page.number-1}${queryParams}">←</a></li>
<li><a href="${home}/${targetUrl}?page=0${queryParams}">1</a></li>
<c:choose>
<c:when test="${page.number < (THRESHOLD + 3)}">
<c:forEach var="i" begin="1" end="${page.number}">
<c:choose>
<c:when test="${i == page.number}">
<li class="current"><span>${i+1}</span></li>
</c:when>
<c:otherwise>
<li><a href="${home}/${targetUrl}?page=${i}${queryParams}">${i+1}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
</c:when>
<c:otherwise>
<li><span>...</span></li>
<c:forEach var="i" begin="${page.number - THRESHOLD}" end="${page.number}">
<c:choose>
<c:when test="${i == page.number}">
<li class="current"><span>${i+1}</span></li>
</c:when>
<c:otherwise>
<li><a href="${home}/${targetUrl}?page=${i}${queryParams}">${i+1}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
</c:otherwise>
</c:choose>
</c:when>
<c:otherwise>
<li class="current"><span>1</span></li>
</c:otherwise>
</c:choose>
<c:if test="${page.number != page.totalPages-1}">
<c:choose>
<c:when test="${(page.totalPages - page.number) < (THRESHOLD + 3)}">
<c:forEach var="i" begin="${page.number+1}" end="${page.totalPages-1}">
<li><a href="${home}/${targetUrl}?page=${i}${queryParams}">${i+1}</a></li>
</c:forEach>
</c:when>
<c:otherwise>
<c:forEach var="i" begin="${page.number+1}" end="${page.number + THRESHOLD}">
<li><a href="${home}/${targetUrl}?page=${i}${queryParams}">${i+1}</a></li>
</c:forEach>
<li><span>...</span></li>
<li><a href="${home}/${targetUrl}?page=${page.totalPages-1}${queryParams}">${page.totalPages}</a></li>
</c:otherwise>
</c:choose>
<li><a href="${home}/${targetUrl}?page=${page.number+1}${queryParams}">→</a></li>
</c:if>
</ul>
</div>
</c:if>
只需将$ {targetUrl}和$ {queryParams}替换为您的控制器网址和所需的参数
你的控制器:
@Autowired
private UserDAO userDao;
@RequestMapping(value = "/your-controller-url", method = RequestMethod.GET)
public String adminUsersList(@RequestParam(required = false) Integer page, Model model) {
if(page == null) {
page = 0;
}
PageRequest pageReq = new PageRequest(page, "15", new Sort(new Order(Direction.ASC, "id"))); // 15 - is number of items on page
Page<User> usersPage = userDao.findAll(pageReq);
model.addAttribute("page", usersPage);
return "your-view-name";
}
和UserDAO:
public interface UserDAO extends JpaRepository<User, Long> {
}
为了更好地了解最新情况,请阅读这篇精彩的教程:http://www.petrikainulainen.net/spring-data-jpa-tutorial/