thymeleaf的使用技巧

    科技2022-09-03  100

    1、thymeleaf处理时间

    <span th:text="${#dates.format(article.getArticleTime(), 'yyyy-MM-dd')}">2017-02-23</span>

    注:article是从后台model中传过来的对象,getArticleTime()是获取它的时间

    2、thymeleaf的@{}中引用${}

    我们可以用来动态的传递请求的参数

    <li class="text"><a th:href="@{'/article/byId/'+${article.getArticleId()}}"></a></li>

    我们可以用来动态的传递请求的参数(如:可以通过+1或-1来实现文章上一篇下一篇)

    <li class="text"><a th:href="@{'/article/byId/'+${article.getArticleId()+1}}"></a></li>

    3、通过后台传入的数字循环生成标签

    model.addAttribute("length",2)

    页面循环:

    <span th:each="i:${#numbers.sequence(1,length)}"> <span th:text="${i}"></span> </span>

    也可以

    <a class="num" th:id="page+${i}" th:each="i:${#numbers.sequence(1,length)}" th:text="${i}"></a>

    最终效果: 1,2 如果你要是生成固定的标签的话,可以不用管那个循环体i

    <span th:each="i:${#numbers.sequence(0,length)}"> <span>happy</span> </span>

    结果: happy happy

    4、thymeleaf调用js函数并且传参 函数

    function clickPage(i) { alert(i); }

    调用

    <a class="num" th:id="page+${i}" th:onclick="'clickPage('+${i}+')'" th:each="i:${#numbers.sequence(1,totalPage)}" th:text="${i}"></a>

    5、thymeleaf来完成异步刷新(分页)

    使用th:fragment="reflash"来决定要异步刷新的位置

    Processed: 0.010, SQL: 9