后端 分页组件实例

举报
兔老大 发表于 2021/04/26 00:02:25 2021/04/26
【摘要】 /** * 分页相关信息 */public class Page { //当前页码 private int current=1; //显示的上限 private int limit=10; //数据总数 //用于计算页数 private int rows; //路径 private String path; public int getCurrent() { return ...

  
  1. /**
  2. * 分页相关信息
  3. */
  4. public class Page {
  5. //当前页码
  6. private int current=1;
  7. //显示的上限
  8. private int limit=10;
  9. //数据总数
  10. //用于计算页数
  11. private int rows;
  12. //路径
  13. private String path;
  14. public int getCurrent() {
  15. return current;
  16. }
  17. public void setCurrent(int current) {
  18. if (current >= 1) {
  19. this.current = current;
  20. }
  21. }
  22. public int getLimit() {
  23. return limit;
  24. }
  25. public void setLimit(int limit) {
  26. if (limit >= 1 && limit<=100) {
  27. this.limit = limit;
  28. }
  29. }
  30. public int getRows() {
  31. return rows;
  32. }
  33. public void setRows(int rows) {
  34. if(rows>=0){
  35. this.rows = rows;
  36. }
  37. }
  38. public String getPath() {
  39. return path;
  40. }
  41. public void setPath(String path) {
  42. this.path = path;
  43. }
  44. /**
  45. * 获取起始行
  46. * @return
  47. */
  48. public int getOffset(){
  49. return (current-1)*limit;
  50. }
  51. /**
  52. * 获取起始页码
  53. *
  54. * @return
  55. */
  56. public int getFrom() {
  57. int from = current - 2;
  58. return from < 1 ? 1 : from;
  59. }
  60. /**
  61. * 获取结束页码
  62. *
  63. * @return
  64. */
  65. public int getTo() {
  66. int to = current + 2;
  67. int total = getTotal();
  68. return to > total ? total : to;
  69. }
  70. /**
  71. * 获取总页数
  72. *
  73. * @return
  74. */
  75. public int getTotal() {
  76. // rows / limit [+1]
  77. if (rows % limit == 0) {
  78. return rows / limit;
  79. } else {
  80. return rows / limit + 1;
  81. }
  82. }
  83. }

  
  1. @RequestMapping(path = "/index", method = RequestMethod.GET)
  2. public String getIndexPage(Model model, Page page,
  3. @RequestParam(name = "orderMode", defaultValue = "0") int orderMode) {
  4. // 方法调用前,SpringMVC会自动实例化Model和Page,并将Page注入Model.
  5. // 所以,在thymeleaf中可以直接访问Page对象中的数据.
  6. page.setRows(discussPostService.findDiscussPostRows(0));
  7. page.setPath("/index?orderMode=" + orderMode);
  8. List<DiscussPost> list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit(),orderMode);
  9. List<Map<String, Object>> discussPosts = new ArrayList<>();
  10. ......
  11. model.addAttribute("discussPosts", discussPosts);
  12. model.addAttribute("orderMode", orderMode);
  13. return "/index";
  14. }

 


  
  1. <!-- 分页 -->
  2. <nav class="mt-5" th:if="${page.rows>0}" th:fragment="pagination">
  3. <ul class="pagination justify-content-center">
  4. <li class="page-item">
  5. <a class="page-link" th:href="@{${page.path}(current=1)}">首页</a>
  6. </li>
  7. <li th:class="|page-item ${page.current==1?'disabled':''}|">
  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. <a class="page-link" th:href="@{${page.path}(current=${i})}" th:text="${i}">1</a>
  11. </li>
  12. <li th:class="|page-item ${page.current==page.total?'disabled':''}|">
  13. <a class="page-link" th:href="@{${page.path}(current=${page.current+1})}">下一页</a>
  14. </li>
  15. <li class="page-item">
  16. <a class="page-link" th:href="@{${page.path}(current=${page.total})}">末页</a>
  17. </li>
  18. </ul>
  19. </nav>

文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。

原文链接:fantianzuo.blog.csdn.net/article/details/102613854

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。