Thymeleaf分页详解

一、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进行分页,需要进行以下步骤:

  1. 在pom.xml中添加thymeleaf的依赖
  2. 在application.properties中进行thymeleaf的配置
  3. 使用JpaRepository接口进行查询
  4. 编写Thymeleaf模板来展示分页数据
  5. 添加一些分页插件,提供更多的分页样式和功能

九、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/n/132134.html

相关推荐

  • mysql分页实现代码(mysql 分页语句)

    本文目录一览: 1、MySql中查询语句实现分页功能 2、怎样用mysql语句实现分页 3、新手怎么以最简单的方式实现jsp+mysql分页 4、MySQL分页的sql语言怎么写?…

    编程 2024-12-12
  • Vue数据类型详解

    一、vue数据类型有哪些 在Vue中,有以下几种数据类型: Object:对象 Array:数组 String:字符串 Number:数字 Boolean:布尔值 Function…

    编程 2024-12-16
  • 微信小程序搜索功能详解

    微信小程序是一种轻便小程序,由于安装方便、操作简单、不占用手机内存等特点,深受用户喜爱。搜索功能作为微信小程序的重要组成部分之一,不仅能够给用户带来极佳的使用体验,还能帮助开发者更…

    编程 2024-12-22
  • w3school Python详解

    一、 w3school w3school是一家提供Web技术在线学习资源的网站,其Python教程部分得到广泛的用户认可。这个教程的不仅仅是介绍Python的语法和基础知识,它涵盖…

    编程 2024-10-03
  • 右连接查询sql语句详解

    在SQL中,连接操作是一种非常重要的操作,它可以将两个或多个表中的数据进行组合,产生新的结果集。右连接查询是其中一种连接方式,它可以帮助我们轻松地从右表中查询数据,本文将从以下几个…

    编程 2025-02-01
  • Python数组添加元素详解

    介绍 在Python编程中,数组是非常重要的一种数据结构,用来存储一系列有序的元素。Python中的数组可以存储各种类型的数据,例如整数,浮点数,字符串等等。在这篇文章中,我们将探…

    编程 2024-10-03
  • Unity地形的多方面技术详解

    一、创建和编辑地形 Unity提供了可视化界面方便我们快速创建和编辑地形。在创建地形时,首先需要添加Terrain组件,然后可以通过左侧Inspector面板中的工具来进行细节的调…

    编程 2025-04-23
  • PyTorch Permute详解

    一、Permute是什么? Permute是PyTorch中的一个函数,用于对Tensor进行维度重排,实现Tensor在维度上的特定排序,可以输入参数来指定新的维度顺序。 二、P…

    编程 2024-10-03
  • Jsthen的用法详解

    在Web开发领域中,JavaScript是必不可少的一种编程语言。Jsthen是一个JavaScript库,它提供了强大的控制流程功能,在异步编程中可以提高代码的可读性和可维护性。…

    编程 2025-02-01
  • Python中列表转字符串全方位详解

    一、基本概念 Python是一种高级编程语言,支持多种数据类型,其中一个重要的数据类型是列表。列表是一种有序的数据集合,其中每个元素都有一个对应的索引值。在Python中,通过将列…

    编程 2024-12-12