gpt4 book ai didi

java - 如何在阔叶开源中设置简单的分页?

转载 作者:太空宇宙 更新时间:2023-11-04 06:40:02 26 4
gpt4 key购买 nike

以下代码可以很好地处理分页。但是,当尝试在 session 属性“pagedProductList”上加载另一个列表而不重新加载页面时,我收到错误...

产品 Controller 代码:

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>

最佳答案

您可以使用 Spring 的 PagedListHolder 实用程序来帮助在 Controller 中进行分页,如下所示:

    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>

关于java - 如何在阔叶开源中设置简单的分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24799246/

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