在页面上只显示最多5个页码:

主要思路其实就是分情况:

1. 总页数小于5页时

2. 总页数大于5页时,且当前页距离首页距离小于2

3. 总页数大于5页时,且当前页距离末页距离小于2
4. 总页数大于5页时,且当前页距离首页距离和末页距离都大于2

 

        <ul class="pagination">
            <li th:class="${pageinfo.isHasPreviousPage()}?'' : 'disabled'">
                <a th:href="@{'/all?pageNum=' + ${pageinfo.getPageNum()-1}}">上一页
                </a>
            </li>
            <li th:if="${pageinfo.getPages()<= 5}" th:each="i :${pageinfo.getNavigatepageNums()}"
                th:class="${pageinfo.getPageNum() == i}? 'page-item active' :' ' ">
                <a th:href="@{'/all?pageNum=' + ${i}}" th:text="  ${i}"></a>
            </li>

            <li th:if="${pageinfo.getPages()> 5 && (pageinfo.getPages() - pageinfo.getPageNum()) <= 2}"
                th:each="i :${#numbers.sequence(pageinfo.getPages() - 4, pageinfo.getPages())}"
                th:class="${pageinfo.getPageNum() == i}? 'page-item active' :' ' ">
                <a th:href="@{'/all?pageNum=' + ${i}}" th:text="  ${i}"></a>
            </li>

            <li th:if="${pageinfo.getPages()> 5 && (pageinfo.getPageNum() - 1) <= 2}"
                th:each="i :${#numbers.sequence(1, 5)}"
                th:class="${pageinfo.getPageNum() == i}? 'page-item active' :' ' ">
                <a  th:href="@{'/all?pageNum=' + ${i}}" th:text="  ${i}"></a>
            </li>

            <li th:if="${pageinfo.getPages()> 5 && ((pageinfo.getPageNum() - 1) > 2)&& (pageinfo.getPages() - pageinfo.getPageNum()) > 2}"
                th:each="i :${#numbers.sequence(pageinfo.getPageNum() - 2, pageinfo.getPageNum() + 2)}"
                th:class="${pageinfo.getPageNum() == i}? 'page-item active' :' ' ">
                <a th:href="@{'/all?pageNum=' + ${i}}" th:text="  ${i}"></a>
            </li>

            <li th:class="${pageinfo.isHasNextPage()}?'' : 'disabled'">
                <a th:href="@{'/all?pageNum=' + ${pageinfo.getPageNum()+1}}">下一页</a>
            </li>
        </ul>

 

 

 

从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件.  https://www.cnblogs.com/duanxz/p/7493276.html