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 的三元操作符相似。