国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot引入Thymeleaf的實(shí)現(xiàn)方法

SpringBoot引入Thymeleaf的實(shí)現(xiàn)方法

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

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

1.thymeleaf簡介

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

thymeleaf的主要目標(biāo)在于提供一種可被瀏覽器正確顯示的、格式良好的模板創(chuàng)建方式,因此也可以用作靜態(tài)建模,thymeleaf的可擴(kuò)展性也非常棒。你可以使用它定義自己的模板屬性集合,這樣就可以計(jì)算自定義表達(dá)式并使用自定義邏輯,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 # 關(guān)閉頁面緩存
 encoding: utf-8 # 模板編碼
 prefix: classpath:/templates/ # 頁面映射路徑
 suffix: .html # 試圖后的后綴
 mode: html5 # 模板模式
 
# 其他具體配置可參考o(jì)rg.springframework.boot.autoconfigure.thymeleaf.thymeleafproperties
# 上面的配置實(shí)際上就是注入該類的屬性值

demo示例

創(chuàng)建indexcontroller

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

創(chuàng)建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>

創(chuàng)建testcontroller

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

創(chuàng)建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.測試結(jié)果

SpringBoot引入Thymeleaf的實(shí)現(xiàn)方法
SpringBoot引入Thymeleaf的實(shí)現(xiàn)方法

4.thymeleaf基礎(chǔ)語法及使用

1.引入標(biāo)簽 

html標(biāo)簽里引入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" >是引入默認(rèn)的static下的css文件夾下的bootstrap文件,類似的標(biāo)簽有: 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>

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

5.運(yùn)算符 

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

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

6.條件 

if/unless th:if是該標(biāo)簽在滿足條件的時(shí)候才會顯示,unless是不成立時(shí)候才顯示

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

switch  thymeleaf支持switch結(jié)構(gòu),默認(rèn)屬性(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.循環(huán)

?
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

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

5.小結(jié)

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 阿v视频在线观看 | 欧美日韩激情 | 亚洲www视频| 日本视频中文字幕 | 日韩欧美视频 | 欧美一区第一页 | 色婷婷亚洲一区二区三区 | 激情婷婷 | 国产在线不卡观看 | 久久99精品一区二区三区三区 | 成人福利网站 | 中文字幕免费在线 | 黄色大片网 | 欧美日韩专区 | 欧美日本韩国一区二区三区 | 国产日韩一区二区三区 | 1区2区在线观看 | 国产成人在线视频 | 成人片网址 | 国产一区 | 成人av在线播放 | 国产亚洲欧美一区 | 亚洲一级在线 | 中文字幕国产视频 | 五月天婷婷社区 | 欧美一级二级三级 | 国产看片网站 | 亚洲精品一区二区三区在线 | 国产免费一区二区三区 | 亚洲精选久久 | 中文字幕一区二区三 | 操操操干干 | 午夜精 | 中文字幕一区二区三区四区五区 | 日韩午夜影院 | 欧美中文一区二区三区 | 91精品国产综合久久小仙女陆萱萱 | 欧美黄色网 | 免费观看的av | 日韩成人影院 | 欧美精品一区二区三区蜜桃视频 |