當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
【thymeleaf】【SpringBoot】Thymeleaf 获取.properties中的配置项变量
生活随笔
收集整理的這篇文章主要介紹了
【thymeleaf】【SpringBoot】Thymeleaf 获取.properties中的配置项变量
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前言
略。
Thymeleaf 獲取.properties中的配置項(xiàng)變量
假設(shè)我在 Thymeleaf 中寫JavaScript的時(shí)候,發(fā)現(xiàn)我需要讀取application.properties中的配置項(xiàng)變量,該怎么辦呢?
application.properties
foo.bar=somethingThymeleaf
<script th:inline="javascript"> alert("偽代碼:application.properties中的配置foo.bar"); </script>我找到解決這個(gè)問(wèn)題的辦法有三種:
- 通過(guò)Model傳遞數(shù)據(jù)給Thymeleaf。這個(gè)不展開(kāi)說(shuō)了。
- 通過(guò) environment Bean獲取application.properties中的配置項(xiàng)
- 通過(guò)ConfigurationProperties Bean獲取application.properties中的配置項(xiàng)
通過(guò) environment Bean獲取application.properties中的配置項(xiàng)
<script th:inline="javascript"> alert([[${@environment.getProperty('foo.bar')}]]); </script>通過(guò)ConfigurationProperties Bean獲取application.properties中的配置項(xiàng)
需要搭配一個(gè)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
總結(jié)
以上是生活随笔為你收集整理的【thymeleaf】【SpringBoot】Thymeleaf 获取.properties中的配置项变量的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【cmd】windows cmd 中判断
- 下一篇: Spring @Value 设置默认值