springbootthymeleaf分页
时间: 2023-12-07 19:39:25 浏览: 25
以下是使用Spring Boot和Thymeleaf实现分页的示例代码:
1.在pom.xml文件中添加以下依赖项:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
```
2.在Controller中添加以下代码:
```java
@GetMapping("/list")
public String list(@RequestParam(defaultValue = "1") Integer pageNum, Model model) {
PageHelper.startPage(pageNum, 10);
List<Article> articles = articleService.findAll();
PageInfo<Article> pageInfo = new PageInfo<>(articles);
model.addAttribute("pageInfo", pageInfo);
return "article/list";
}
```
3.在Thymeleaf模板中添加以下代码:
```html
<div class="pagination">
<ul>
<li th:class="${pageInfo.isFirstPage() ? 'disabled' : ''}">
<a th:href="@{${#httpServletRequest.requestURI}(pageNum=1)}">«</a>
</li>
<li th:class="${pageInfo.isFirstPage() ? 'disabled' : ''}">
<a th:href="@{${#httpServletRequest.requestURI}(pageNum=${pageInfo.prePage})}"><</a>
</li>
<li th:each="page : ${pageInfo.navigatepageNums}"
th:class="${page == pageInfo.pageNum ? 'active' : ''}">
<a th:href="@{${#httpServletRequest.requestURI}(pageNum=${page})}" th:text="${page}"></a>
</li>
<li th:class="${pageInfo.isLastPage() ? 'disabled' : ''}">
<a th:href="@{${#httpServletRequest.requestURI}(pageNum=${pageInfo.nextPage})}">></a>
</li>
<li th:class="${pageInfo.isLastPage() ? 'disabled' : ''}">
<a th:href="@{${#httpServletRequest.requestURI}(pageNum=${pageInfo.pages})}">»</a>
</li>
</ul>
</div>
```
其中,`pageNum`表示当前页码,`10`表示每页显示的记录数,`Article`表示实体类。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)