一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot引入Thymeleaf的實現方法

SpringBoot引入Thymeleaf的實現方法

2021-07-28 12:12Bobby Java教程

這篇文章主要介紹了SpringBoot引入Thymeleaf的實現方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1.thymeleaf簡介

thymeleaf是個xml/xhtml/html5模板引擎,可以用于web與非web應用 

thymeleaf的主要目標在于提供一種可被瀏覽器正確顯示的、格式良好的模板創建方式,因此也可以用作靜態建模,thymeleaf的可擴展性也非常棒。你可以使用它定義自己的模板屬性集合,這樣就可以計算自定義表達式并使用自定義邏輯,thymeleaf還可以作為模板引擎框架。

2.引入thymeleaf

引入依賴

在maven(pom.xml)中直接引入:

?
1
2
3
4
5
6
7
8
<dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-thymeleaf</artifactid>
</dependency>
<dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-web</artifactid>
</dependency>

配置thymeleaf

在application.yml配置thymeleaf

?
1
2
3
4
5
6
7
8
9
10
11
12
server:
 port: 8000
spring:
 thymeleaf:
 cache: false # 關閉頁面緩存
 encoding: utf-8 # 模板編碼
 prefix: classpath:/templates/ # 頁面映射路徑
 suffix: .html # 試圖后的后綴
 mode: html5 # 模板模式
 
# 其他具體配置可參考org.springframework.boot.autoconfigure.thymeleaf.thymeleafproperties
# 上面的配置實際上就是注入該類的屬性值

demo示例

創建indexcontroller

?
1
2
3
4
5
6
7
8
9
@controller
public class indexcontroller {
 // 返回視圖頁面
 @requestmapping("index")
 public string index(){
  return "index";
 }
 
}

創建index.html

?
1
2
3
4
5
6
7
8
9
10
<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>title</title>
</head>
<body>
 hello thymeleaf!
</body>
</html>

創建testcontroller

?
1
2
3
4
5
6
7
8
9
@restcontroller
public class testcontroller {
 
 // 返回整個頁面
 @requestmapping("/test")
 public modelandview test(){
  return new modelandview("test");
 }
}

創建test.html

?
1
2
3
4
5
6
7
8
9
10
11
<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>title</title>
</head>
<body>
hello thymeleaf! </br>
by: modelandview
</body>
</html>

3.測試結果

SpringBoot引入Thymeleaf的實現方法
SpringBoot引入Thymeleaf的實現方法

4.thymeleaf基礎語法及使用

1.引入標簽 

html標簽里引入xmlns:th="http://www.thymeleaf.org"才能使用th:*這樣的語法

2.引入url 

@{...} 

例如:

<a th:href="@{http://www.baidu.com}" rel="external nofollow" >絕對路徑</a> 是訪問絕對路徑下的url, <a th:href="@{/}" rel="external nofollow" >相對路徑</a> 是訪問相對路徑下的url。

<a th:href="@{css/bootstrap.min.css}" rel="external nofollow" >是引入默認的static下的css文件夾下的bootstrap文件,類似的標簽有: th:href 和 th:src

3.獲取變量 

通過${}取值,對于javabean的話,使用變量名.屬性名獲取

4.字符串替換

<span th:text="'welcome to our application, ' + ${user.name} + '!'"></span>
或者
<span th:text="|welcome to our application, ${user.name}!|"></span>

注意:|…|中只能包含變量表達式${…},不能包含其他常量、條件表達式等

5.運算符 

   在表達式中可以使用各類算術運算符
   例如 (+, -, *, /, %)
   例如:th:with="iseven=(${stat.number} % 1 == 0)"
   邏輯運算符 (>, <, <=,>=,==,!=)
   需要注意的是使用<,>的時候需要轉義

?
1
2
th:if="${stat.number} > 1"
th:text="'execution mode is ' + ( (${execmode} == 'dev')? 'development' : 'production')"

6.條件 

if/unless th:if是該標簽在滿足條件的時候才會顯示,unless是不成立時候才顯示

?
1
<a th:href="@{/login}" rel="external nofollow" th:unless=${user.number != null}>login</a>

switch  thymeleaf支持switch結構,默認屬性(default)用*表示

?
1
2
3
4
5
<div th:switch="${user.role}">
  <p th:case="'admin'">user is an administrator</p>
  <p th:case="#{roles.manager}">user is a manager</p>
  <p th:case="*">user is some other thing</p>
</div>

7.循環

?
1
2
3
4
5
<tr th:each="prod : ${prods}">
 <td th:text="${prod.name}">onions</td>
 <td th:text="${prod.price}">2.41</td>
 <td th:text="${prod.instock}? #{true} : #{false}">yes</td>
</tr>

8.utilities

內置在context中,可以直接通過#訪問
#dates
#calendars
#numbers
#strings
arrays
lists
sets
maps

5.小結

本文講述了如何在spring boot中引入模板引擎thymeleaf以及thymeleaf基礎語法和實際使用

本文github地址:https://github.com/ishuibo/springall

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人福利色视频 | 国产精品一区二区不卡的视频 | 亚洲 欧美 国产 综合首页 | 亚洲精品视频观看 | 精品在线91 | 色综合中文字幕天天在线 | 撕开老师的丝袜白丝扒开粉嫩的小 | 亚洲 国产精品 日韩 | 日本私人影院 | 国产成人久久精品一区二区三区 | 美女脱一光二净的视频 | 窝窝影院午夜色在线视频 | 欧美日韩一区二区三区在线视频 | 国产精品毛片无码 | 日本漫画无翼乌 | 无人影院在线播放 | 3d蒂法受辱在线播放 | 波多野结衣黑人系列在线观看 | 日本福利视频一区 | 操儿媳小说| 天堂在线中文无弹窗全文阅读 | 小舞同人18av黄漫网站 | 欧美综合精品一区二区三区 | 俄罗斯图书馆无打码久久 | 98pao强力打造高清免费 | 欧美国产日韩1区俺去了 | 99超级碰碰成人香蕉网 | 日韩视频在线免费观看 | 九九国产在线观看 | 国产精品福利短视在线播放频 | 国产日韩免费视频 | 香蕉97超级碰碰碰免费公 | 欧美久久一区二区三区 | 日本亚洲欧洲高清有码在线播放 | 天美传媒影视在线免费观看 | 91插插插插| 夫妻性生活影院 | 欧美视频一区二区三区四区 | 亚洲成熟人网站 | 亚洲一区二区三区免费视频 | 欧美成人精品第一区二区三区 |