參考說明:如下筆記參考來自尚硅谷springboot教學中的筆記!html
thymeleaf官網docs: https://www.thymeleaf.org/documentation.htmljava
模板引擎:spring
JSP、Velocity、Freemarker、Thymeleafexpress
SpringBoot推薦的Thymeleaf;api
語法更簡單,功能更強大;springboot
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> 2.1.6 </dependency>
切換thymeleaf版本 <properties> <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version> <!-- 佈局功能的支持程序 thymeleaf3主程序 layout2以上版本 --> <!-- thymeleaf2 layout1--> <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version> </properties>
@ConfigurationProperties(prefix = "spring.thymeleaf") public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8"); private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html"); public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; //
以上是package org.springframework.boot.autoconfigure.thymeleaf;包中的源碼,放在這裏主要是告訴你模塊該如何配置;session
只要咱們把HTML頁面放在classpath:/templates/,thymeleaf就能自動渲染;app
使用:ide
(1) 導入thymeleaf的名稱空間(導入後有語法提示)spring-boot
<html lang="en" xmlns:th="http://www.thymeleaf.org">
(2) 使用thymeleaf語法:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>成功!</h1> <!--th:text 將div裏面的文本內容設置爲 --> <div th:text="${hello}">這是顯示歡迎信息</div> </body> </html>
1)、th:text;改變當前元素裏面的文本內容;
th:任意html屬性;來替換原生屬性的值(eg: th:id=1改變id的值, th:class=「c1」改變class的值)
補充:
<div th:remove="tag">
<div>唐嫣</div> //包含標記的標籤將被刪除,即這裏的外層div將被刪除 參考:th:remove用法
</div>
2)、表達式?
一、Simple expressions:(表達式語法) (1)Variable Expressions: ${...}:獲取變量值;OGNL; 1)、獲取對象的屬性、調用方法 2)、使用內置的基本對象: #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.foo} 3)、內置的一些工具對象: #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). (2)Selection Variable Expressions: *{...}:選擇表達式:和${}在功能上是同樣; 補充:配合 th:object="${session.user}:#只有做爲全局變量使用的時候纔是不同的 eg: 用th:object從session裏面取出user對象(即將取出來的對象賦值給Object),之後要用user對象的值,就能夠在當前的div裏面,
"*"號就表明剛纔的th:object對象,而後直接獲取對象的屬性便可。
<div th:object="${session.user}"> <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 th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div> (3)Message Expressions: #{...}:獲取國際化內容 (4)Link URL Expressions: @{...}:定義URL; @{/order/process(execId=${execId},execType='FAST')} (5)Fragment Expressions: ~{...}:片斷引用表達式 <div th:insert="~{commons :: main}">...</div> 二、Literals(字面量) Text literals: 'one text' , 'Another one!' ,… Number literals: 0 , 34 , 3.0 , 12.3 ,… Boolean literals: true , false Null literal: null Literal tokens: one , sometext , main ,… 三、Text operations:(文本操做) String concatenation: + Literal substitutions: |The name is ${name}| 四、Arithmetic operations:(數學運算) Binary operators: + , - , * , / , % Minus sign (unary operator): - 五、Boolean operations:(布爾運算) Binary operators: and , or Boolean negation (unary operator): ! , not 六、Comparisons and equality:(比較運算) Comparators: > , < , >= , <= ( gt , lt , ge , le ) Equality operators: == , != ( eq , ne ) 七、Conditional operators:條件運算(三元運算符) If-then: (if) ? (then) If-then-else: (if) ? (then) : (else) Default: (value) ?: (defaultvalue) 八、Special tokens(特殊操做): No-Operation: _
九、內聯表達式
<p>Hello, [[${session.user.name}]]!</p>
任何在th:text或th:utext屬性中使⽤的表達式均可以出如今[[]]或[()]中.
[[...]]等價於th:text(即結果將被HTML轉義) , [(...)]等價於th:utext, 不會執⾏任何HTML轉義