一、Thymeleaf分頁序號
Thymeleaf分頁的序號是使用當前頁數和每頁顯示數量計算出來的。可以通過SpringBoot的Pageable實現,使用Thymeleaf的th:each循環來進行展示,具體代碼如下:
其中,page為使用PageRequest類構造出來的分頁對象。
二、Thymeleaf模板引擎
Thymeleaf是一個Java模板引擎,用來替代JSP。它可以在HTML、XML、JavaScript、CSS甚至CSV文件中使用,同時支持Spring框架,功能強大。使用Thymeleaf分頁,需要在pom.xml中添加以下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
同時,在application.properties中添加thymeleaf的相關配置:
# 開啟thymeleaf緩存 spring.thymeleaf.cache=false # thymeleaf模板的前綴、後綴 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.servlet.content-type=text/html
三、Thymeleaf分頁查詢
在使用Thymeleaf分頁時,需要進行查詢操作。查詢可以使用SpringBoot的JpaRepository介面的findAll方法,也可以自定義方法來進行查詢。代碼如下:
@GetMapping("/")
public String index(@RequestParam(name = "page", defaultValue = "0") Integer page,
@RequestParam(name = "size", defaultValue = "10") Integer size,
Model model) {
Pageable pageable = PageRequest.of(page, size);
Page<Article> articles = articleRepository.findAll(pageable);
model.addAttribute("articles", articles);
model.addAttribute("title", "首頁");
model.addAttribute("isActive", "home");
return "index";
}
其中,articleRepository是一個繼承了JpaRepository的自定義介面,Article是實體類。
四、Thymeleaf分頁插件
使用Thymeleaf分頁,還可以使用一些Thymeleaf分頁插件,如bootstrap分頁插件、laypage分頁插件等等。這些插件提供了更多的分頁樣式和功能,並且可以易於地集成到Spring Boot項目中。使用方法如下:
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="text-center">
<ul class="pagination">
<li th:class="${articles.first ? 'disabled' : ''}">
<a th:href="@{/(page=0, size=${size})}">«</a>
</li>
<li th:class="${articles.first ? 'disabled' : ''}">
<a th:href="@{/(page=${articles.previousPageable().pageNumber}, size=${size})}"><<</a>
</li>
<li th:each="i : ${#numbers.sequence(page.getNumber()+1, page.getTotalPages())}"
th:class="${i-1 == page.getNumber() ? 'active' : ''}">
<a th:href="@{/(page=${i-1}, size=${size})}" th:text="${i}"></a>
</li>
<li th:class="${articles.last ? 'disabled' : ''}">
<a th:href="@{/(page=${articles.nextPageable().pageNumber}, size=${size})}">>></a>
</li>
<li th:class="${articles.last ? 'disabled' : ''}">
<a th:href="@{/(page=${articles.getTotalPages()-1}, size=${size})}">»</a>
</li>
</ul>
</div>
五、Thymeleaf分頁模板
在使用Thymeleaf分頁時,需要編寫一份模板,用於展示分頁數據。模板代碼如下:
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>標題</th>
<th>作者</th>
<th>發布時間</th>
</tr>
</thead>
<tbody>
<tr th:each="article : ${articles.content}">
<td th:text="${#ids.seq('seq', article)}"></td>
<td><a th:href="@{/article/}+${article.id}" th:text="${article.title}"></a></td>
<td th:text="${article.author.name}"></td>
<td th:text="${#dates.format(article.createdTime, 'yyyy/MM/dd')}"></td>
</tr>
</tbody>
</table>
六、Thymeleaf分頁功能
使用Thymeleaf分頁,可以利用SpringBoot的Pageable類進行分頁操作,更加方便快捷。同時,可以添加一些分頁插件,提供更多的分頁樣式和功能。最後,編寫模板來展示分頁數據。
七、Thymeleaf分頁Gitee
在Gitee上可以找到一些使用Thymeleaf分頁的示例項目。這些示例項目可以幫助開發者更加快速地了解Thymeleaf分頁的使用方法和技巧。可以訪問以下鏈接查看:
- https://gitee.com/Kayle/sharing/tree/master/spring-boot-thymeleaf-pageable-demo
- https://gitee.com/dyc87112/thymeleaf-bootstrap4-pagination/blob/master/src/main/resources/templates/pagination.html
八、使用Thymeleaf進行分頁
在Spring Boot項目中,使用Thymeleaf進行分頁,需要進行以下步驟:
- 在pom.xml中添加thymeleaf的依賴
- 在application.properties中進行thymeleaf的配置
- 使用JpaRepository介面進行查詢
- 編寫Thymeleaf模板來展示分頁數據
- 添加一些分頁插件,提供更多的分頁樣式和功能
九、Spring Boot Thymeleaf分頁
Spring Boot提供了Pageable類來幫助開發者進行分頁操作。同時,Thymeleaf也為開發者提供了一些模板語法來快速輸出分頁數據。在Spring Boot Thymeleaf分頁中,需要注意以下幾個要點:
- 需要進行依賴和配置的添加
- 需要查詢操作,可以使用JpaRepository等Spring Data JPA相關介面
- 模板中需要使用th:each等Thymeleaf模板語法來進行展示
- 可以使用一些分頁插件,提供更多的樣式和功能
十、用Thymeleaf進行分頁
使用Thymeleaf進行分頁可以大大簡化開發者的工作量,同時也為頁面展示提供了更多的靈活性和美觀度。在使用Thymeleaf進行分頁時,需要了解一些Thymeleaf的模板語法和SpringBoot的Pageable類的使用方法。
最後,開發者需要不斷嘗試,不斷調試,才能更好地應用Thymeleaf分頁,在實際項目中提高效率和質量。
原創文章,作者:POMB,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/132134.html
微信掃一掃
支付寶掃一掃