thymeleaf

thymeleaf

官方文档

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.4.1</version>
</dependency>

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
</dependency>

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>

命名空间

<html xmlns:th="http://www.thymeleaf.org">

由thymeleaf接管静态资源

第一种方法:
<!-- Bootstrap core CSS -->
<link th:href="@{css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{css/signin.css}" rel="stylesheet">

如果出现页面加载问题,试试第二种方法:
<base th:href="@{/}">
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/signin.css" rel="stylesheet">

超链接

<a class="btn btn-sm" th:href="@{/index.html(language='zh_CN')}">中文</a>
拼接 url + brand= + 动态的brand
<a th:href="@{${url}(brand=${brand})}" th:text="${brand}"></a>

if

<span th:if="${pageInfo.hasNextPage}">…</span>
<a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">view</a>

switch

<div th:switch="${data}">
	<span th:case="0"></span>
	<span th:case="1"></span>
	<span th:case="3"></span>
</div>

for

<tr  th:each="infoStat : ${Infos}" >

判断map的key是否存在

表示判断searchMap有没有'keywords'这个key,如果有的话,就将其显示出来

th:value="${#maps.containsKey(searchMap,'keywords')}?${searchMap.keywords}" 

限制文本长度

表示限制sku.name的长度为20,多余的用...表示,注意...会占用三位

th:text="${#strings.abbreviate(sku.name,20)}" 

调用后端方法

<span th:text="${@timeUtils.tampToDate()}"></span>

点击事件

 th:onclick="|javascript:changeCommentStatus(${comment.getId()})|"