spring boot 中thymeleaf 分页(上一页,下一页,首页,尾页)

controller

@GetMapping(value = "/page")
public String findGoodsPage(Model model, @RequestParam(value="pageNum") String pageNum){
   Page<Goods> pages=goodsService.findGoodsNoCriteria(Integer.parseInt(pageNum),20,"commentNum"); 

    if(pageNum==null){
        pageNum="1";
    }
    int pagenum=Integer.parseInt(pageNum); 
    model.addAttribute("page",pages);
    model.addAttribute("pageNum",pagenum);
    model.addAttribute("totalPages",pages.getTotalPages());
    model.addAttribute("totalElements",pages.getTotalElements());
    return "list";
}

thymeleaf中的html页面:

<table>
  <tr>
  <td><a th:href="@{/goods/page?pageNum=0}">首页</a></td>
    <td th:switch="${pageNum}">
      <p th:case="1"> <a th:href="@{/goods/page?pageNum=1}">上一页</a></p>
      <p th:case="*"><a th:href="@{/goods/page(pageNum=${pageNum-1})}">上一页</a></p>
    </td>
     <td th:switch="${pageNum}">
       <p th:case="${totalPages}"><a th:href="@{/goods/page(pageNum=${totalPages})}">下一页</a></p>
       <p th:case="*"><a th:href="@{/goods/page(pageNum=${pageNum+1})}">下一页</a></p>
     </td>
     <td><a th:href="@{/goods/page(pageNum=${totalPages})}">尾页</a></td>
  </tr>
</table>

猜你喜欢

转载自blog.csdn.net/fengcai0123/article/details/79525109