bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

如何在SpringBoot中引入Thymeleaf

如何在SpringBoot中引入Thymeleaf?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

成都創新互聯公司是創新、創意、研發型一體的綜合型網站建設公司,自成立以來公司不斷探索創新,始終堅持為客戶提供滿意周到的服務,在本地打下了良好的口碑,在過去的10余年時間我們累計服務了上千家以及全國政企客戶,如小攪拌車等企業單位,完善的項目管理流程,嚴格把控項目進度與質量監控加上過硬的技術實力獲得客戶的一致贊揚。

springboot是什么

springboot一種全新的編程規范,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程,SpringBoot也是一個服務于框架的框架,服務范圍是簡化配置文件。

1.Thymeleaf簡介

Thymeleaf是個XML/XHTML/HTML5模板引擎,可以用于Web與非Web應用 

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

2.引入Thymeleaf

引入依賴

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

<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

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

@Controller
public class IndexController {
 // 返回視圖頁面
 @RequestMapping("index")
 public String index(){
  return "index";
 }

}

創建index.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
 Hello Thymeleaf!
</body>
</html>

創建TestController

@RestController
public class TestController {
 
 // 返回整個頁面
 @RequestMapping("/test")
 public ModelAndView test(){
  return new ModelAndView("test");
 }
}

創建test.html

<!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)"
   邏輯運算符 (>, <, <=,>=,==,!=)
   需要注意的是使用<,>的時候需要轉義

th:if="${stat.number} &gt; 1"
th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')"

6.條件 

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

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

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

<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.循環

<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

關于如何在SpringBoot中引入Thymeleaf問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創新互聯行業資訊頻道了解更多相關知識。

當前題目:如何在SpringBoot中引入Thymeleaf
網頁鏈接:http://vcdvsql.cn/article30/ggpeso.html

成都網站建設公司_創新互聯,為您提供全網營銷推廣微信公眾號營銷型網站建設靜態網站網站排名網站內鏈

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

微信小程序開發