日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

thymeleaf 获取yml中的值_SpringBoot引入Thymeleaf

發布時間:2025/3/15 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 thymeleaf 获取yml中的值_SpringBoot引入Thymeleaf 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.Thymeleaf簡介

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

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

2.引入Thymeleaf

引入依賴

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

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

配置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

Title

Hello Thymeleaf!

創建TestController

@RestController

public class TestController {

// 返回整個頁面

@RequestMapping("/test")

public ModelAndView test(){

return new ModelAndView("test");

}

}

創建test.html

Title

Hello Thymeleaf!

By: ModelAndView

3.測試結果

4.Thymeleaf基礎語法及使用

1.引入標簽

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

2.引入URL

@{...}

例如:

絕對路徑 是訪問絕對路徑下的URL, 相對路徑 是訪問相對路徑下的URL。

是引入默認的static下的css文件夾下的bootstrap文件,類似的標簽有: th:href 和 th:src

3.獲取變量

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

4.字符串替換

或者

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

5.運算符

在表達式中可以使用各類算術運算符

例如 (+, -, *, /, %)

例如:th:with="isEven=(${stat.number} % 1 == 0)"

邏輯運算符 (>, =,==,!=)

需要注意的是使用的時候需要轉義

th:if="${stat.number} > 1"

th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')"

6.條件

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

Login

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

User is an administrator

User is a manager

User is some other thing

7.循環

Onions2.41yes

8.Utilities

內置在Context中,可以直接通過#訪問

#dates

#calendars

#numbers

#strings

arrays

lists

sets

maps

5.小結

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

總結

以上是生活随笔為你收集整理的thymeleaf 获取yml中的值_SpringBoot引入Thymeleaf的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。