- 常見的模板引擎有JSP印荔、Velocity雷恃、Freemarker疙剑、Thymeleaf
- SpringBoot推薦使用Thymeleaf宰闰;
一潭陪、引入Thymeleaf啟動(dòng)器依賴颠放、
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
如需切換thymeleaf版本:
<properties>
<thymeleaf.version>X.X.X.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
二伪朽、Thymeleaf使用
- 這里直接看源碼了
- 默認(rèn)只要我們把HTML頁(yè)面放在classpath:/templates/帮毁,thymeleaf就能自動(dòng)渲染溜宽;
@ConfigurationProperties(prefix = "spring.thymeleaf") //與配置文件相關(guān)聯(lián)
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8; //編碼格式
public static final String DEFAULT_PREFIX = "classpath:/templates/"; //默認(rèn)的讀取路徑
public static final String DEFAULT_SUFFIX = ".html"; //默認(rèn)的后綴名
1吉拳、創(chuàng)建模板文件t1.html,并導(dǎo)入thymeleaf的名稱空間
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
2适揉、在controller中準(zhǔn)備數(shù)據(jù)
@RequestMapping("thy")
public String thy(Map<String,Object> map){
map.put("til","thymelaf測(cè)試頁(yè)面");
map.put("ceshi","<h2 style='color: red'>utext會(huì)轉(zhuǎn)義特殊字符留攒,如:html標(biāo)簽</h2>");
map.put("list", Arrays.asList("zhangshan","lisi","wangwu"));
return "thymeleaf01";
}
3煤惩、使用模板
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>[[${til}]]</title>
</head>
<body>
<div th:text="${til}">這是一個(gè)頁(yè)面</div>
<div th:utext="${ceshi}"></div>
<hr/>
<h2 th:each="item : ${list}">[[${item}]]</h2>
</body>
</html>
三、語(yǔ)法規(guī)則
-
1炼邀、
th:text --> 改變當(dāng)前元素里面的文本內(nèi)容魄揉;
th:任意html屬性 --> 來替換原生屬性的值
image.png
- 2、語(yǔ)法表達(dá)式
Simple expressions:(表達(dá)式語(yǔ)法)
Variable Expressions: ${...}:獲取變量值拭宁;OGNL洛退;
1)、獲取對(duì)象的屬性杰标、調(diào)用方法
2)兵怯、使用內(nèi)置的基本對(duì)象:
#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)、內(nèi)置的一些工具對(duì)象:
#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).
Selection Variable Expressions: *{...}:選擇表達(dá)式:和${}在功能上是一樣腔剂;
補(bǔ)充:配合 th:object="${session.user}:
<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>
Message Expressions: #{...}:獲取國(guó)際化內(nèi)容
Link URL Expressions: @{...}:定義URL媒区;
@{/order/process(execId=${execId},execType='FAST')}
Fragment Expressions: ~{...}:片段引用表達(dá)式
<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:(數(shù)學(xué)運(yùn)算)
Binary operators: + , - , * , / , %
Minus sign (unary operator): -
Boolean operations:(布爾運(yùn)算)
Binary operators: and , or
Boolean negation (unary operator): ! , not
Comparisons and equality:(比較運(yùn)算)
Comparators: > , < , >= , <= ( gt , lt , ge , le )
Equality operators: == , != ( eq , ne )
Conditional operators:條件運(yùn)算(三元運(yùn)算符)
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
Special tokens:
No-Operation: _
更多配置參考官方文檔:https://www.thymeleaf.org/documentation.html