8、SpringBoot:thymeleaf的使用技巧

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

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

	<a class="btn" th:href="@{'/teacher/tea/' + ${teacher.getTeacherId()}}">个人简介</a>

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

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

2、thymeleaf处理时间格式

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

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>

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"来决定要异步刷新的位置

在这里插入图片描述

ajax:

在这里插入图片描述
6、thymeleaf的命名空间约束

	xmlns:th="http://www.thymeleaf.org"

7、thymeleaf 模板引擎

        <!-- spring-boot-starter-thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.3.4.RELEASE</version>
        </dependency>

8、工作中遇到的语法记录
Thymeleaf 中,[[$0]]语法用于插值,即把后端的数据插入到模板中。

  • 具体来说[[...]]:用于输出文本到模板,且当这个表达式求值为false时不会输出。这与 Java 的 if 语句相似。
  • ${....}:用于输出文本到模板,且当这个表达式求值为false 时,仍然会输出,只是输出的内容会变成空字符串。这与 Java 的三元操作符相似。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值