【发布时间】:2021-07-18 02:00:13
【问题描述】:
我的控制器中有一个对象列表
public String postResults(Model model, @RequestParam String shopName) throws IOException {
DataFilter filter = new DataFilter();
List<Row> rows = filter.filterRows(shopName);
model.addAttribute("results", rows);
return "results";
}
以及下面的部分模板:
<tr th:each="obj : ${results}">
<td><img th:src="${obj.image}" th:class="images"></td>
<td th:utext="${obj.description}"></td>
<td th:utext="${obj.price}"></td>
</tr>
目前我正在尝试弄清楚如何在这里实现分页。
附:我已经在基于存储库的同一个项目中进行了分页,现在我需要在没有它的情况下实现相同的分页,只需使用对象列表
【问题讨论】:
-
仅使用
List of objects分页是不可能的。您需要使用一些唯一 ID 将列表临时保存在内存(或存储)中,并且这些唯一 ID 应与每个请求一起传递以获取数据(以及页码)。但这不是可扩展的解决方案。数据库分页是您应该使用的。 -
你的意思是这样?
List<Row> rows = filter.filterRows(shopName); Pageable pageable = PageRequest.of(0, 25); Page<Row> page = new PageImpl<>(rows, pageable, rows.size());
标签: java spring pagination