1、Thymeleaf:java模板引擎
标准方言:常用
无需引入命名空间
html中引入thymaleaf命名空间:
<html xmlns:th="http://www.thymeleaf.org">
2、变量表达式:
语法:${…}
<span th:text="${book.author.name}">
3、消息表达式:(文本外部化、国际化或i18n)
语法:#{…}
<th th:text="#{header.address.city}"><th>
4、选择表达式:
语法:*{…}
<div th:object="${book}">
<span th:text="*{title}"><span>
</div>
与变量表达式的区别:实在当前选择的对象而不是上下文变量映射上执行(即此时的title是book对象的title)
5、连接表达式(超链接)
语法:@{…}
6、分段表达式(引用片段)
语法:th:insert或者th:replace
<div th:fragment="copy">
...
</div>
<div th:insert="~{footer :: copy}"/>
7、字面量(文字)
文本:th:text=“文字”
数字:th:text=“2013+7”
布尔:th:if="${user.isAdmin()} == false"
null:th:if="${variable.something} == null"
算术操作:(±*/%等计算)th:with=“isEven={${product.count} % 2 == 0}”
8、比较和等价:
避免与标签中的<>冲突所以使用字母
比较
<、>=、<=(gt、lt、ge、le)
data-th-if="${page.totalPages le 7}"
等价
==、!=(eq、ne)
data-th-select="${i eq page.size}"
9、条件运算符
th:class="${row.even}? ‘even’ : ‘odd’"
10、无操作(保留之前的文本等)
<span th:text="${user.name} ? : _">no user authenticated<span/>
11、设置属性值
a:设置任意属性值:th:attr
th:attr=“action=@{/subscribe}”
b:设置值到指定的属性
c:设置固定值布尔属性
12、迭代器(数组、Map、List)
语法:th:each
<li th:each="book : ${books}">En las Orillas del Sar<li/>
状态变量:index、count、size、current、even/odd(奇偶)、first、last
13、条件语句
语法:th:if、th:unless
switch
<div th:switch="${user.role}">
<p th:case="'admin'">user is an administrator</p>
<p th:case="#{roles.manager}">user is a manager</p>
<p th:case="*">user is some other thing</p>
<div/>
14、模板布局
a、定义和引用片段
定义:th:fragement=“name”
引用:th:insert="~{footer :: name}" 这里的footer是页面名称
b、不使用th:fragment定义的引用
th:insert="~{footer :: #name}"
c、th:insert、th:replace、th:include的区别
th:insert插入片段作为正文的主标签
th:replace用指定片段来替换主标签
th:include插入片段的内容(3.x后不建议使用)
15、属性优先级
16、注释
a、标准HTML/XML注释<!-- -->
b、Thymeleaf解析器级注释块<!–/* /*–>
(thymeleaf解析的时候会被注释,静态打开时会显示 )
c、原型注释块<!–/*/ /*/–>
(thymeleaf解析的时候会显示,静态打开时会被注释 )
17、内联
a、[[…]] 会被转义
<p>Hello, [[${session.user.name}]]!</p>
等同于:
<p>Hello, <span th:text="${session.user.name}">Sebastian</span>!</p>
b、[(…)] 不会被转义
<p>Hello, [[${session.user.name}]]!</p>
等同于:
<p>Hello, [[${session.user.name}]]!</p>
c、禁用内联 th:inline=“none”
18、表达式基本对象
#ctx:上下文对象(context)
#locale:直接访问与java.util.Locale关联的请求
request/session等属性
a、param:用于检索请求参数
b、session:用于检索sessions属性
c、application:用于检索application/servlet上下文属性
web上下文对象
a、#request:与当前请求关联的request对象
b、#session:与当前请求关联的session对象
c、#servletContext:与当前对象关联的servletContext对象
pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>