以下代码在分页时工作正常。但是在尝试在会话属性上加载另一个列表时,#page; pagedProductList"没有重新加载页面,我收到错误......
产品控制器代码:
String page = request.getParameter("page");
PagedListHolder<Product> products = new PagedListHolder<Product>();
if ("previous".equals(page)) {
products = (PagedListHolder<Product>) request.getSession().getAttribute("pagedProductList");
products.previousPage();
} else if ("next".equals(page)) {
products = (PagedListHolder<Product>) request.getSession().getAttribute("pagedProductList");
products.nextPage();
} else {
products = new PagedListHolder<Product>(allValidProducts);
products.setPageSize(10);
request.getSession().setAttribute("pagedProductList", products);
}
Html:
<tbody id="productList" >
<tr th:each="product : ${products.pageList}">
<td style="min-width:300px;"> <a th:href="@{editProduct/(productId=${product.id})}" th:text="${product.name}">name</a> </td>
<td style="width:400px;" th:text="${product.url}">URL</td>
<td style="width:200px;" th:text="${product.id}">ID</td>
</tr>
<tr>
<td colspan="3" style="text-align:center;"> <hr/> </td>
</tr>
<tr>
<td colspan="3" style="text-align:center;">
<a th:if="${!products.firstPage}" th:href="@{/virtualadmin/listProducts(keyword=${param.keyword}, page='previous')}">Previous</a>
<span th:text="${products.page + 1}">1</span>
<a th:if="${!products.lastPage}" th:href="@{/virtualadmin/listProducts(keyword=${param.keyword}, page='next')}">Next</a>
</td>
</tr>
</tbody>
答案 0 :(得分:1)
您可以使用Spring的PagedListHolder实用程序来帮助控制器中的分页:
String page = request.getParameter("page");
PagedListHolder<Product> products;
if ("previous".equals(page)) {
products = (PagedListHolder<Product>) request.getSession().getAttribute("pagedProductList");
products.previousPage();
} else if ("next".equals(page)) {
products = (PagedListHolder<Product>) request.getSession().getAttribute("pagedProductList");
products.nextPage();
} else {
products = new PagedListHolder<Product>(productDAO.searchByKeyword(keyword));
products.setPageSize(10);
request.getSession().setAttribute("pagedProductList", products);
}
model.addAttribute("products", products);
在模板中,您可以浏览以下结果:
<div th:each="product : ${products.pageList}">
...
</div>
并添加页面导航:
< a th:if="${!products.firstPage}"
th:href="@{/search.html(keyword=${param.keyword}, page='previous')}">Previous< /a>
< span th:text="${products.page + 1}">1 / 10< /span>
< a th:if="${!products.lastPage}"
th:href="@{/search.html(keyword=${param.keyword}, page='next')}">Next< /a>