初识
https://www.thymeleaf.org/
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html
关闭缓存,不然每次都要重启
spring.thymeleaf.cache=false
使用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 热启动 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
html页面放入templates下
<html lang="en" xmlns:th="http://www.thymeleaf.org">
th:text
th:style
@GetMapping("/thymeleaf")
public String thymeleaf(Model model) {
model.addAttribute("msg", "hello thymeleaf");
model.addAttribute("list", Arrays.asList("myname", "mingyue", "fusu"));
return "thymeleaf";
}
<div th:text="${msg}"></div>
<div th:utext="${msg}"></div> 可以识别标签
<div th:each="item : ${list}" th:text="${item}"></div>
<div th:each="item : ${list}">[[ ${item} ]]</div>
语法
简单表达:
- 变量表达式:
${...} - 选择变量表达式:
*{...} - 消息表达式:
#{...} - 链接网址表达式:
@{...} - 片段表达式:
~{...}
字面
- 文本文字:
'one text','Another one!',… - 号码文字:
0,34,3.0,12.3,… - 布尔文字:
true,false - 空字面:
null - 文字标记:
one,sometext,main,…
文字操作:
- 字符串连接:
+ - 文字替换:
|The name is ${name}|
算术运算:
- 二元运算符:
+,-,*,/,% - 减号(一元运算符):
-
布尔运算:
- 二元运算符:
and,or - 布尔否定(一元运算符):
!,not
比较和平等:
- 比较:
>,<,>=,<=(gt,lt,ge,le) - 平等运营商:
==,!=(eq,ne)
有条件:
- IF-THEN:
(if) ? (then) - IF-THEN-ELSE:
(if) ? (then) : (else) - 默认:
(value) ?: (defaultvalue)
特殊代币:
- 无操作:
_
标签
```php
th:if=”{not #strings.isEmpty(msg)}”
th:each=”item : ${items}”
// text
th:text=”#{login.btn}”
th:utext=”#{person.html}”
// url
th:href=”@{/index.html(l=’zh_CN’)}”
th:href=”@{/admin/people/(${people.id})}” 使用restful风格传递参数
th:href=”|http://item.gulimall.com/${product.skuId}.html|"
th:href=”@{http://search.gulimall.com(catId=${catagory.catId})}"
th:placeholder=”#{login.username}”
th:attr=”myattr=${person.data}” # 自己的属性
// class
th:class=”${( !#strings.isEmpty(p)
&& #strings.startsWith(p,’hotScore’)
&& #strings.endsWith(p,’desc’))
? ‘sort_a desc’ : ‘sort_a’ }”
th:fragment=”topbar”
th:insert=”{dashboard::topbar}”{commons/commons::topbar}” ()传递参数
th:replace=”
行内表达式 [[${data}]]
将参数变为本地变量 this:with=”name = ${param.name}”
判断字符串是否为空 $