文章目录
SpringBoot导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
其他导入
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
然后需要在html页面的html标签中加入xmlns:th="http://www.thymeleaf.org"
命名空间。
然后使用th:元素名
替换html的元素,即可使用thymeleaf表达式。
th:任意html属性:替代原生属性的值
th:text :改变当前元素里面的文本内容。
例:
<div id="div01" class="myDiv" th:id="${hello}" th:class="${hello}" th:text="${hello}"></div>
表达式
Simple expressions(表达式语法)
1. Variable Expressions: ${…} //获取变量值,底层是OGNL表达式
- 获取对象属性、调用方法
例:- ${person.father.name}
- ${person.createCompleteName()}
- 使用内置的基本对象
例:#ctx : the context object. #vars: the context variables. #locale : the context locale. #request : (only in Web Contexts) the HttpServletRequest object. #response : (only in Web Contexts) the HttpServletResponse object. #session : (only in Web Contexts) the HttpSession object. #servletContext : (only in Web Contexts) the ServletContext object.
- ${session.size()}
- ${#request.getParameter(‘foo’)}
- 内置的一些工具对象
例:#execInfo : information about the template being processed. #messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax. #uris : methods for escaping parts of URLs/URIs #conversions : methods for executing the configured conversion service (if any). #dates : methods for java.util.Date objects: formatting, component extraction, etc. #calendars : analogous to #dates , but for java.util.Calendar objects. #numbers : methods for formatting numeric objects. #strings : methods for String objects: contains, startsWith, prepending/appending, etc. #objects : methods for objects in general. #bools : methods for boolean evaluation. #arrays : methods for arrays. #lists : methods for lists. #sets : methods for sets. #maps : methods for maps. #aggregates : methods for creating aggregates on arrays or collections. #ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).
- ${#numbers.sequence(from,to)}
- ${#strings.toString(obj)}
2. Selection Variable Expressions: *{…} //选择表达式
和${}在功能上是一样的,但是比 ${}多一个功能,需要配合th:object使用。
例:
<div th:object="${session.user}">
<!--此时的*{firstName}等同于${session.user.firstName}-->
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
等同于
<div>
<p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
</div>
3. Message Expressions: #{…} //获取国际化内容
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
结果:
home.welcome=¡Bienvenido a nuestra tienda de comestibles! //西班牙语
4. Link URL Expressions: @{…} //定义URL
<!-- Will produce 'http://localhost:8080/gtvg/order/details?orderId=3' (plus rewriting) -->
<!--使用@{}在url中传参时用()代替?,多个参数用,逗号分隔,并且在@{}中可以使用${}获取值-->
<a href="details.html" th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
<!--@{}中,最前面/代表当前项目下-->
<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<!--在@{}中可以直接用{变量名}获取值-->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
5. Fragment Expressions: ~{…} //片段引用表达式
<div th:insert="~{commons :: main}">...</div>
thymeleaf的三种引入功能片段属性:
- th:insert :将公共片段整个插入到声明引入的元素中
- th:replace :将声明引入的元素替换为公共片段
- th:include :将被引入的片段的内容包含进这个标签中
三种引入属性的区别
<!--抽取片段-->
<footer th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</footer>
<!--三种引入方式-->
<!--1-->
<div th:insert="footer :: copy"></div>
<!--2-->
<div th:replace="footer :: copy"></div>
<!--3-->
<div th:include="footer :: copy"></div>
<!--效果-->
<!--1-->
<div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
</div>
<!--2-->
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
<!--3-->
<div>
© 2011 The Good Thymes Virtual Grocery
</div>
Literals (字面量)
1. Text literals(字符串): ‘one text’ , ‘Another one!’ ,…
2. Number literals(数字): 0 , 34 , 3.0 , 12.3 ,…
3. Boolean literals(布尔值): true , false
4. Null literal(null值): null
5. Literal tokens(多数据 ,逗号分隔): one , sometext , main ,…
Text operations(文本操作)
1. String concatenation: +
2. Literal substitutions: |The name is ${name}|
Arithmetic operations(数学运算)
1. Binary operators: + , - , * , / , %
2. Minus sign (unary operator): -
Boolean operations(布尔运算)
1. Binary operators: and , or
2. Boolean negation (unary operator): ! , not
Comparisons and equality(比较运算)
1. Comparators: > , < , >= , <= ( gt , lt , ge , le )
2. Equality operators: == , != ( eq , ne )
Conditional operators(条件运算,三元运算符)
1. If-then: (if) ? (then)
2. If-then-else: (if) ? (then) : (else)
3. Default: (value) ?: (defaultvalue)
Special tokens(特殊操作,即_代表不操作)
1. No-Operation: _
标签内写法
如果想在标签内直接使用变量,需要遵循特定的语法。
<p>Hello, [[${session.user.name}]]!</p>
等同于
<p>Hello, <span th:text="${session.user.name}">Sebastian</span>!</p>
[[]]:如同th:text
例:
<p>The message is "[[${msg}]]"</p>
等同于
<p>The message is "This is <b>great!</b>"</p>
[()]:如同th:utext
例:
<p>The message is "[(${msg})]"</p>
等同于
<p>The message is "This is <b>great!</b>"</p>