SpringBoot+Thymeleaf 实现分页

1.将分页模块提取出来

  1 package com.nowcoder.community.entity;
  2 
  3 /**
  4  * 封装分页相关的信息
  5  * @author ProgramCat
  6  * @date 2023/3/25 21:29
  7  */
  8 public class Page {
  9     //当前页数
 10     private int current=1;
 11     //显示上限
 12     private int limit=10;
 13     //数据总数(用于计算总数)
 14     private int rows;
 15     //查询路径(用于复用分页链接)
 16     private String path;
 17 
 18     public int getCurrent() {
 19         return current;
 20     }
 21 
 22     public void setCurrent(int current) {
 23         if(current>=1){
 24             this.current = current;
 25         }
 26     }
 27 
 28     public int getLimit() {
 29         return limit;
 30     }
 31 
 32     public void setLimit(int limit) {
 33         if (limit>=1&&limit<=100){
 34             this.limit = limit;
 35         }
 36     }
 37 
 38     public int getRows() {
 39         return rows;
 40     }
 41 
 42     public void setRows(int rows) {
 43         if (rows>=0){
 44             this.rows = rows;
 45         }
 46     }
 47 
 48     public String getPath() {
 49         return path;
 50     }
 51 
 52     public void setPath(String path) {
 53         this.path = path;
 54     }
 55     /**
 56      *
 57      * @author ProgramCat
 58      * @date 2023/3/25 22:10
 59      获取起始页
 60      * @return int 起始页
 61      */
 62     public int getOffset() {
 63         // current * limit - limit
 64         return (current - 1) * limit;
 65     }
 66     /**
 67      *获取总页数
 68      * 即rows/limit,有余数则加一
 69      * @author ProgramCat
 70      * @date 2023/3/25 21:34
 71      */
 72     public int getTotal(){
 73         if (rows%limit==0){
 74             return rows/limit;
 75         }else{
 76             return rows/limit +1;
 77         }
 78     }
 79     /**
 80      * @author ProgramCat
 81      * @date 2023/3/25 21:38
 82      *获取起始页码
 83      * @return int 始页码
 84      */
 85     public int getFrom(){
 86         int from=current-2;
 87         return from<1?1:from;
 88     }
 89     /**
 90      *
 91      * @author ProgramCat
 92      * @date 2023/3/25 21:38
 93      * 获取结束页
 94      * @return int 结束页
 95      */
 96     public int getTo() {
 97         int to = current + 2;
 98         int total = getTotal();
 99         return to > total ? total : to;
100     }
101 }
View Code

2.对应在Controller层设置分页模块中数据总量,和分页链接

3.在对应需要分页中填写相关数据

 1                 <!-- 分页 -->
 2                 <nav class="mt-5" th:if="${page.rows>0}">
 3                     <ul class="pagination justify-content-center">
 4                         <li class="page-item">
 5                             <a class="page-link" th:href="@{${page.path}(current=1)}">首页</a> <!-- 会自动转化为/链接地址/current=1 -->
 6                         </li>
 7                         <li th:class="|page-item ${page.current==1?'disabled':''}|"> <!--thymeleaf控制class时在两边使用‘|’-->
 8                             <a class="page-link" th:href="@{${page.path}(current=${page.current-1})}">上一页</a></li>
 9                         <li th:class="|page-item ${i==page.current?'active':''}|" th:each="i:${#numbers.sequence(page.from,page.to)}">
10                          <!--其中page.form,page.to 不需要${}是因为还在${}里面,其中numbers.sequence是产生page.from为首,page.to为尾为一个数组-->
11                             <a class="page-link" th:href="@{${page.path}(current=${i})}" th:text="${i}">1</a>
12                         </li>
13                         <li th:class="|page-item ${page.current==page.total?'disabled':''}|">
14                             <a class="page-link" th:href="@{${page.path}(current=${page.current+1})}">下一页</a>
15                         </li>
16                         <li class="page-item">
17                             <a class="page-link" th:href="@{${page.path}(current=${page.total})}">末页</a>
18                         </li>
19                     </ul>
20                 </nav>

 如果使用idea推荐使用插件 :

JBLHtmlToThymeleaf

posted @ 2023-03-25 22:44  喵工  阅读(290)  评论(0)    收藏  举报