Java基礎總結之Thymeleaf詳解
標簽
在HTML頁面上使用Thymeleaf標簽,Thymeleaf 標簽能夠動態地替換掉靜態內容,使頁面動態展示。為了大家更直觀的認識Thymeleaf,下面展示一個在HTML文件中嵌入了Thymeleaf的頁面文件,示例代碼如下:
<!DOCTYPE html><html lang='en' xmlns:th='http://www.thymeleaf.org'><head> <meta charset='UTF-8'><link rel='stylesheet' type='text/css' media='all'href='http://www.cgvv.com.cn/css/gtvg.css' rel='external nofollow' th:href='http://www.cgvv.com.cn/bcjs/@{/css/gtvg.css}' rel='external nofollow' /> <title>Title</title></head><body><p th:text='${hello}'>歡迎進入Thymeleaf的學習</p></body></html>
thymelef常用標簽
標簽 說明 th:insert 布局標簽,替換內容到引入的文件 th:replace 頁面片段包含(類似JSP中的include標簽) th:each 元素遍歷(類似JSP中的c:forEach標簽) th:if 條件判斷,如果為真 th:unless 條件判斷,如果為假 th:switch 條件判斷,進行選擇性匹配 th:case 條件判斷,進行選擇性匹配 th:value 屬性值修改,指定標簽屬性值 th:href 用于設定鏈接地址 th:src 用于設定鏈接地址 th:text 用于指定標簽顯示的文本內容標準表達式
說明 表達式語法 變量表達式 ${…} 選擇變量表達式 *{…} 消息表達式 #{…} 鏈接URL表達式 @{…} 片段表達式 ~{…} 1.1 變量表達式${…}主要用于獲取上下文中的變量值,示例代碼如下:
<p th:text='${title}'>這是標題</p>
Thymeleaf為變量所在域提供了一些內置對象,具體如下所示
# ctx:上下文對象# vars:上下文變量# locale:上下文區域設置# request:(僅限Web Context)HttpServletRequest對象# response:(僅限Web Context)HttpServletResponse對象# session:(僅限Web Context)HttpSession對象# servletContext:(僅限Web Context)ServletContext對象
假設要在Thymeleaf模板引擎頁面中動態獲取當前國家信息,可以使用#locale內置對象,示例代碼如下
The locale country is: <span th:text='${#locale.country}'>US</span>1.2 選擇變量表達式*{…}
和變量表達式用法類似,一般用于從被選定對象而不是上下文中獲取屬性值,如果沒有選定對象,則和變量表達式一樣,示例代碼如下
<div th:object='${book}'><p>titile: <span th:text='*{title}'>標題</span>.</p></div>
*{title} 選擇變量表達式獲取當前指定對象book的title屬性值。
1.3 消息表達式 #{…}消息表達式#{…}主要用于Thymeleaf模板頁面國際化內容的動態替換和展示,使用消息表達式#{…}進行國際化設置時,還需要提供一些國際化配置文件。
1.4 鏈接表達式 @{…}鏈接表達式@{…}一般用于頁面跳轉或者資源的引入,在Web開發中占據著非常重要的地位,并且使用也非常頻繁
<a th:href='http://www.cgvv.com.cn/bcjs/@{http://localhost:8080/order/details(orderId=${o.id})}' rel='external nofollow' >view</a><a th:href='http://www.cgvv.com.cn/bcjs/@{/order/details(orderId=${o.id},pid=${p.id})}' rel='external nofollow' >view</a>
鏈接表達式@{…}分別編寫了絕對鏈接地址和相對鏈接地址。
在有參表達式中,需要按照@{路徑(參數名稱=參數值,參數名稱=參數值…)}的形式編寫,同時該參數的值可以使用變量表達式來傳遞動態參數值
1.5 片段表達式 ~{…}片段表達式~{…}用來標記一個片段模板,并根據需要移動或傳遞給其他模板。其中,最常見的用法是使用th:insert或th:replace屬性插入片段
<div th:insert='~{thymeleafDemo::title}'></div>
thymeleafDemo為模板名稱,Thymeleaf會自動查找“/resources/templates/”目錄下的thymeleafDemo模板,title為片段名稱
二、基本使用2.1 Thymeleaf模板基本配置首先 在Spring Boot項目中使用Thymeleaf模板,首先必須保證引入Thymeleaf依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
其次,在全局配置文件中配置Thymeleaf模板的一些參數。一般Web項目都會使用下列配置
spring.thymeleaf.cache = true #啟用模板緩存spring.thymeleaf.encoding = UTF_8 #模板編碼spring.thymeleaf.mode = HTML5 #應用于模板的模板模式spring.thymeleaf.prefix = classpath:/templates/ #指定模板頁面存放路徑spring.thymeleaf.suffix = .html #指定模板頁面名稱的后綴
上述配置中:
spring.thymeleaf.cache表示是否開啟Thymeleaf模板緩存,默認為true,在開發過程中通常會關閉緩存,保證項目調試過程中數據能夠及時響應;
spring.thymeleaf.prefix指定了Thymeleaf模板頁面的存放路徑,默認為classpath:/templates/;
spring.thymeleaf.suffix指定了Thymeleaf模板頁面的名稱后綴,默認為.html
到此這篇關于Java基礎總結之Thymeleaf模板的文章就介紹到這了,更多相關Java Thymeleaf模板內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: