访问百里香模板中的application.properties值

时间:2019-05-12 18:15:51

标签: thymeleaf

我有一个我的spring boot应用程序,在我的application.properties中,其中一个属性是url=myurl.net。 在同一应用程序中,我有一个百里香叶html模板。我想将url值添加到该模板中。我正在百里香html模板<font face=arial size=2 > access the url : </font> ${environment.getProperty(‘url’)}

中使用以下代码

我得到的输出:

  

访问网址:$(environment.getProperty(‘url’)}

我期望的输出:

  

访问网址:myurl.net

我得到的不是实际值,而是相同的文本。有人可以帮我吗感谢您的帮助。

4 个答案:

答案 0 :(得分:3)

您必须使用

${@environment.getProperty('css.specific.name')},这将获取application.properties file(css.specific.name)

答案 1 :(得分:1)

在控制器中映射您支持的值,然后直接从Thymeleaf调用它。

@Controller
public class XController {

    @Value("${pcn.app.url}")
    private String url;     // Directly instead of using envireonment

    @RequestMapping(value = "form-show", method = RequestMethod.GET)
    public ModelAndView showForm() {
        ModelAndView model = new ModelAndView();
        model.setViewName("your-view");

        // The key which will look in your HTML with.
        model.addObject("urlValue", url);
        return model;
    }
}

在您的html中通常这样称呼

<html>
    <body>
        <span th:text="#{urlValue}"></span>
    </body>
</html>

答案 2 :(得分:1)

或者,您将设置定义为数据结构,并通过bean作用域访问它们

application.properties

foo.bar=something

Foo.java

@Configuration
@EnableConfigurationProperties(Foo.class)
@ConfigurationProperties("foo")
public class Foo {
  private String bar;

  public void setBar(String bar) { this.bar = bar; }
  public String getBar() { return bar; }
}

sometemplate.html

...
<span th:text="@foo.getBar()"></span>
...

答案 3 :(得分:0)

我正在使用如下

@Service("mapService")
public class MapService {

    @Value("${maps.google.api.key}")
    private String googleMapApiKey;

    @RequestMapping(value = "/google/key", method = RequestMethod.GET)
    public String getGoogleMapApiKey() {
        return googleMapApiKey;
    }
}


<script th:src="@{'https://maps.googleapis.com/maps/api/js?'+'key='+${@mapService.getGoogleMapApiKey()}+'&libraries=places&callback=initAutocomplete'}"></script>