Thymeleaf
模板引擎
-
前端交給我們的頁(yè)面,是
html
頁(yè)面。如果是我們以前開(kāi)發(fā),我們需要把他們轉(zhuǎn)成jsp頁(yè)面,jsp好處就是當(dāng)我們查出一些數(shù)據(jù)轉(zhuǎn)發(fā)到JSP頁(yè)面以后,我們可以用jsp輕松實(shí)現(xiàn)數(shù)據(jù)的顯示,及交互等。 -
jsp支持非常強(qiáng)大的功能,包括能寫(xiě)Java代碼,但是呢,我們現(xiàn)在的這種情況,SpringBoot這個(gè)項(xiàng)目首先是以jar的方式,不是war,像第二,我們用的還是嵌入式的Tomcat,所以呢,他現(xiàn)在默認(rèn)是不支持jsp的。
-
那不支持jsp,如果我們直接用純靜態(tài)頁(yè)面的方式,那給我們開(kāi)發(fā)會(huì)帶來(lái)非常大的麻煩,那怎么辦呢?
SpringBoot推薦你可以來(lái)使用模板引擎:
模板引擎,我們其實(shí)大家聽(tīng)到很多,其實(shí)jsp就是一個(gè)模板引擎,還有用的比較多的freemarker,包括SpringBoot給我們推薦的Thymeleaf,模板引擎有非常多,但再多的模板引擎,他們的思想都是一樣的,什么樣一個(gè)思想呢我們來(lái)看一下這張圖:
模板引擎的作用就是我們來(lái)寫(xiě)一個(gè)頁(yè)面模板,比如有些值呢,是動(dòng)態(tài)的,我們寫(xiě)一些表達(dá)式。而這些值,從哪來(lái)呢,就是我們?cè)诤笈_(tái)封裝一些數(shù)據(jù)。然后把這個(gè)模板和這個(gè)數(shù)據(jù)交給我們模板引擎,模板引擎按照我們這個(gè)數(shù)據(jù)幫你把這表達(dá)式解析、填充到我們指定的位置,然后把這個(gè)數(shù)據(jù)最終生成一個(gè)我們想要的內(nèi)容給我們寫(xiě)出去,這就是我們這個(gè)模板引擎,不管是jsp還是其他模板引擎,都是這個(gè)思想。只不過(guò)呢,就是說(shuō)不同模板引擎之間,他們可能這個(gè)語(yǔ)法有點(diǎn)不一樣。其他的我就不介紹了,我主要來(lái)介紹一下SpringBoot給我們推薦的Thymeleaf模板引擎,這模板引擎呢,是一個(gè)高級(jí)語(yǔ)言的模板引擎,他的這個(gè)語(yǔ)法更簡(jiǎn)單。而且呢,功能更強(qiáng)大。
引入Thymeleaf
怎么引入呢,對(duì)于springboot來(lái)說(shuō),什么事情不都是一個(gè)start
的事情嘛,我們?nèi)ピ陧?xiàng)目中引入一下。給大家三個(gè)網(wǎng)址:
-
Thymeleaf 官網(wǎng):https://www.thymeleaf.org/
-
Thymeleaf 在Github 的主頁(yè):https://github.com/thymeleaf/thymeleaf
-
Spring官方文檔:找到我們對(duì)應(yīng)的版本https://docs.spring.io/spring-boot/docs/2.6.5/reference/htmlsingle/#using-boot-starter
找到對(duì)應(yīng)的pom依賴(lài):可以適當(dāng)點(diǎn)進(jìn)源碼看下本來(lái)的包!
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Thymeleaf分析
前面呢,我們已經(jīng)引入了Thymeleaf,那這個(gè)要怎么使用呢?
我們首先得按照SpringBoot的自動(dòng)配置原理看一下我們這個(gè)Thymeleaf的自動(dòng)配置規(guī)則,在按照那個(gè)規(guī)則,我們進(jìn)行使用。
我們?nèi)フ乙幌耇hymeleaf的自動(dòng)配置類(lèi):ThymeleafProperties
@ConfigurationProperties(
prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
private String mode = "HTML";
private Charset encoding;
}
我們可以在其中看到默認(rèn)的
前綴
和后綴
!
我們只需要把我們的html頁(yè)面放在類(lèi)路徑下的templates下,thymeleaf就可以幫我們自動(dòng)渲染了。
使用thymeleaf什么都不需要配置,只需要將他放在指定的文件夾下即可!
測(cè)試
-
編寫(xiě)一個(gè)TestController
@Controller public class TestController { @RequestMapping("/test") public String test1(){ //classpath:/templates/test.html return "test"; } }
-
編寫(xiě)一個(gè)測(cè)試頁(yè)面 test.html 放在 templates 目錄下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>Test頁(yè)面</h1> </body> </html>
-
啟動(dòng)項(xiàng)目請(qǐng)求測(cè)試
Thymeleaf 語(yǔ)法學(xué)習(xí)
要學(xué)習(xí)語(yǔ)法,還是參考官網(wǎng)文檔最為準(zhǔn)確,我們找到對(duì)應(yīng)的版本看一下;
Thymeleaf 官網(wǎng):https://www.thymeleaf.org/ , 簡(jiǎn)單看一下官網(wǎng)!我們?nèi)ハ螺dThymeleaf的官方文檔!在線(xiàn)文檔:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html
Thymeleaf入門(mén)
我們做個(gè)最簡(jiǎn)單的練習(xí) :我們需要查出一些數(shù)據(jù),在頁(yè)面中展示
-
修改測(cè)試請(qǐng)求,增加數(shù)據(jù)傳輸;
@RequestMapping("/t1") public String test1(Model model){ //存入數(shù)據(jù) model.addAttribute("msg","Hello,Thymeleaf"); //classpath:/templates/test.html return "test"; }
-
我們要使用thymeleaf,需要在html文件中導(dǎo)入命名空間的約束,方便提示。
我們可以去官方文檔中看一下命名空間拿來(lái)過(guò)來(lái):
xmlns:th="http://www.thymeleaf.org"
-
我們?nèi)ゾ帉?xiě)下前端頁(yè)面
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>測(cè)試</title> </head> <body> <h1>測(cè)試頁(yè)面</h1> <!--th:text就是將div中的內(nèi)容設(shè)置為它指定的值,和之前學(xué)習(xí)的Vue一樣--> <div th:text="${msg}"></div> </body> </html>
-
啟動(dòng)測(cè)試!
Thymeleaf語(yǔ)法
1、我們可以使用任意的 th:attr 來(lái)替換Html中原生屬性的值!
2、我們能寫(xiě)哪些表達(dá)式呢?
Simple expressions:(表達(dá)式語(yǔ)法)
Variable Expressions: ${...}:獲取變量值;OGNL;
1)、獲取對(duì)象的屬性、調(diào)用方法
2)、使用內(nèi)置的基本對(duì)象:#18
#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.
3)、內(nèi)置的一些工具對(duì)象:
#execInfo : information about the template being processed.
#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.
==================================================================================
Selection Variable Expressions: *{...}:選擇表達(dá)式:和${}在功能上是一樣;
Message Expressions: #{...}:獲取國(guó)際化內(nèi)容
Link URL Expressions: @{...}:定義URL;
Fragment Expressions: ~{...}:片段引用表達(dá)式
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: _
練習(xí)測(cè)試:
1、 我們編寫(xiě)一個(gè)Controller,放一些數(shù)據(jù)
@RequestMapping("/test2")
public String test2(Map<String,Object> map){
//存入數(shù)據(jù)
map.put("msg","<h1>Hello</h1>");
map.put("users", Arrays.asList("qinjiang","kuangshen"));
//classpath:/templates/test.html
return "test";
}
2、測(cè)試頁(yè)面取出數(shù)據(jù)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<h1>Test頁(yè)面</h1>
<!--不轉(zhuǎn)義-->
<div th:text="${msg}"></div>
<!--轉(zhuǎn)義-->
<div th:utext="${msg}"></div>
<hr>
<!--遍歷數(shù)據(jù)-->
<!--th:each每次遍歷都會(huì)生成當(dāng)前這個(gè)標(biāo)簽:官網(wǎng)#9-->
<h3 th:each="user:${users}" th:text="${user}"></h3>
<hr>
<!--行內(nèi)寫(xiě)法:官網(wǎng)#12-->
<h3 th:each="user:${users}">[[ ${user} ]]</h3>
</div>
</body>
</html>
3、啟動(dòng)項(xiàng)目測(cè)試!
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-647326.html
我們看完語(yǔ)法,很多樣式,我們即使現(xiàn)在學(xué)習(xí)了,也會(huì)忘記,所以我們?cè)趯W(xué)習(xí)過(guò)程中,需要使用什么,根據(jù)官方文檔來(lái)查詢(xún),才是最重要的,要熟練使用官方文檔!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-647326.html
到了這里,關(guān)于SpringBoot Thymeleaf模板引擎的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!