背景
在用thymeleaf末班获取后台的page对象时,我们需要进行分页页码的显示,通过page对象的totalPages可以获取到所有页面,那么获取到的这个整数,怎么循环打印出来呢?
方案
Thymeleaf提供了一种函数
#numbers.sequence(start,end)
这个函数的作用是循环遍历start-end之间的整数
例子
<div class="page"th:each="i:${#numbers.sequence(0,page.totalPages)}">
<div class="pageItem" th:text="${i+1}"></div>
</div>
注意我们需要用一个变量接收所有的整数,再依次打印出来