javascript
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
TitleHello Thymeleaf!
創建TestController
@RestController
public class TestController {
// 返回整個頁面
@RequestMapping("/test")
public ModelAndView test(){
return new ModelAndView("test");
}
}
創建test.html
TitleHello 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.41yes8.Utilities
內置在Context中,可以直接通過#訪問
#dates
#calendars
#numbers
#strings
arrays
lists
sets
maps
…
5.小結
本文講述了如何在Spring Boot中引入模板引擎Thymeleaf以及Thymeleaf基礎語法和實際使用
總結
以上是生活随笔為你收集整理的thymeleaf 获取yml中的值_SpringBoot引入Thymeleaf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 存储http请求返回参数_前端学习需要知
- 下一篇: 氮化镓充电器哪家做得好_氮化镓充电头推荐