Pageable+Page实现分页无需配置,也不需要加入jar包(maven依赖)

Controller控制层

package com.gxuwz.late.controller;

import com.gxuwz.late.bean.Record;
import com.gxuwz.late.repository.RecordRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletResponse; @Controller
@RequestMapping("/manager")
public class ManagerController {
static Logger logger = LoggerFactory.getLogger(ManagerController.class); @Autowired
RecordRepository recordRepository; @RequestMapping("/list")
public String list(HttpServletResponse response, Model model, Integer pageNum){ if (pageNum == null){
pageNum = 1;
}
     // 排序方式,这里是以“recordNo”为标准进行降序
Sort sort = new Sort(Sort.Direction.DESC, "recordNo"); // 这里的"recordNo"是实体类的主键,记住一定要是实体类的属性,而不能是数据库的字段
Pageable pageable = new PageRequest(pageNum - 1, 6, sort); // (当前页, 每页记录数, 排序方式)
Page<Record> list = recordRepository.findAll(pageable); logger.info("pageNum==" + pageNum); model.addAttribute("pageInfo", list); response.addHeader("x-frame-options","SAMEORIGIN"); // 允许iframe
return "record_list";
}
}

html页面

      <table id = "table" class="table table-hover text-center">
<tr>
<th>晚归记录编号</th>
<th>宿舍楼编号</th>
<th>宿舍号</th>
<th>学号</th>
<th>班级</th>
<th>辅导员</th>
<th>晚归时间</th>
<th>晚归原因</th>
<th>操作</th>
</tr>
          
     <!-- pageInfo.getContent() 返回的是一个list -->
<tr th:each="record:${pageInfo.getContent()}">
<td th:text="${record.recordNo }"></td>
<td th:text="${record.buildingNo }"></td>
<td th:text="${record.dorId }"></td>
<td th:text="${record.studentNo }"></td>
<td th:text="${record.className }"></td>
<td th:text="${record.instName }"></td>
<td th:text="${record.time }"></td>
<td th:text="${record.reason }"></td>
</tr>
<tr>
<td colspan="8">
<div class="pagelist">
<p>当前<span th:text="${pageInfo.getNumber()} + 1"></span>页,总<span th:text="${pageInfo.totalPages}"></span>页
                 共<span th:text="${pageInfo.totalElements}"></span>条记录
<a th:href="@{/manager/list}">首页</a>
<a th:href="@{/manager/list(pageNum = ${pageInfo.hasPrevious()} ? ${pageInfo.getNumber() } : 1)}">上一页</a>
<a th:href="@{/manager/list(pageNum = ${pageInfo.hasNext()} ? ${pageInfo.getNumber()} + 2 : ${pageInfo.totalPages})}">下一页</a>
<a th:href="@{/manager/list(pageNum = ${pageInfo.totalPages})}">尾页</a></p>
</div> </td>
</tr>
</table>

实现效果:

最新文章

  1. 俄罗斯方块C#版
  2. jquery template模版引擎
  3. OleDb 内存泄露问题
  4. 数据字典生成工具之旅(8):SQL查询表的约束默认值等信息
  5. java项目的划分方式:模块优先还是层优先?
  6. ZMQ 在linux进程 和分布式之间的通信
  7. SQL Server select 将类型相同的行合并,并将对应金额相加
  8. bzoj 2251: [2010Beijing Wc]外星联络 后缀数组
  9. RBM 与 DBN 学习笔记
  10. 逆天的IE7中,诡异的横向滚动条
  11. 如何清空android ListView控件的内容
  12. Twenty Newsgroups Classification任务之二seq2sparse(2)
  13. SpringMVC 基于注解的Controller @RequestMapping @RequestParam..
  14. 一张图告诉你最流行的 7 个 JavaScript框架特点
  15. SSM-Spring-06:Spring的自动注入autowire的byName和byType
  16. Jmeter实例(一)_Beanshell脚本断言Mock接口
  17. 如何利用office绘制施工进度计划横道图?
  18. Sigar--系统性能监测
  19. [BZOJ 1032][JSOI 2007]祖玛 题解(区间DP)
  20. java 中文及特殊字符校验

热门文章

  1. PostgreSQL 给数组排序
  2. IntelliJ IDEA 添加项目后编译显示包不存在的解决方案
  3. 五.获得MYSQL数据库自动生成的主键
  4. Go语言开发教程
  5. MaxCompute Mars开发指南
  6. 【NS2】各种TCP版本 之 TCP Tahoe 和 TCP Reno(转载)
  7. jsp中几注释的区别
  8. oracle函数 localtimestamp
  9. @bzoj - 4709@ 柠檬
  10. python实现以立春为起点n为周期任意日期所在的日期区间