當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
【thymeleaf】【SpringBoot】Thymeleaf 获取.properties中的配置项变量
生活随笔
收集整理的這篇文章主要介紹了
【thymeleaf】【SpringBoot】Thymeleaf 获取.properties中的配置项变量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
略。
Thymeleaf 獲取.properties中的配置項變量
假設我在 Thymeleaf 中寫JavaScript的時候,發現我需要讀取application.properties中的配置項變量,該怎么辦呢?
application.properties
foo.bar=somethingThymeleaf
<script th:inline="javascript"> alert("偽代碼:application.properties中的配置foo.bar"); </script>我找到解決這個問題的辦法有三種:
- 通過Model傳遞數據給Thymeleaf。這個不展開說了。
- 通過 environment Bean獲取application.properties中的配置項
- 通過ConfigurationProperties Bean獲取application.properties中的配置項
通過 environment Bean獲取application.properties中的配置項
<script th:inline="javascript"> alert([[${@environment.getProperty('foo.bar')}]]); </script>通過ConfigurationProperties Bean獲取application.properties中的配置項
需要搭配一個ConfigurationProperties Bean,如下:
@Configuration("fooBean") @ConfigurationProperties("foo") public class FooProperties {private String bar;public void setBar(String bar) { this.bar = bar; }public String getBar() { return bar; } } <script th:inline="javascript"> alert([[${@fooBean.getBar()}]]); </script>參考
https://stackoverflow.com/questions/56102116/access-application-properties-value-in-thymeleaf-template
https://sayyy.blog.csdn.net/article/details/80844244
https://www.cnblogs.com/itplay/p/10736340.html
總結
以上是生活随笔為你收集整理的【thymeleaf】【SpringBoot】Thymeleaf 获取.properties中的配置项变量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【cmd】windows cmd 中判断
- 下一篇: mysql 重置更新祖级字段