激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一区二区三区手机在线 | av电影网站在线观看 | 粉嫩粉嫩一区二区三区在线播放 | 国产成人精品免高潮在线观看 | 国产精品hd免费观看 | 欧美日韩综合视频 | 91在线观看| 香蕉久久久| 欧美日韩高清一区二区三区 | 一级大黄毛片 | 天堂二区 | 成人在线观看免费观看 | 久久久久久免费 | 日本教室三级在线看 | 久久伊人国产精品 | 亚洲va久久久噜噜噜久牛牛影视 | 亚洲一区二区国产 | www成人在线观看 | 久久最新免费视频 | 午夜激情视频网站 | 欧美一级不卡视频 | 国产一区二区视频精品 | 91网站免费在线观看 | 精品久久久久久久久久久下田 | 国外成人在线视频 | 免费黄色大片网站 | 久草在线网址 | 国产一区免费 | 色婷婷综合久久久中文一区二区 | 在线播放亚洲视频 | 欧美成人一区二区三区电影 | 99视频有精品视频高清 | 污黄视频在线播放 | 中国videos露脸hd | 国内精品久久久久久影视8 国产一区二区成人在线 | 欧美黄色一区 | 欧美又黄又嫩大片a级 | 日韩欧美视频一区二区三区 | 99riav国产在线观看 | 午夜神马电影网 | 在线观看国产 |