2017-05-05 64 views
8

我有這樣的一段代碼,其中TDK是我在名爲SpringBoot文件application.properties定義的公共變量server.contextPathThymeleaf讀取屬性內更換

我想知道是否有一種方法來替代它

<head th:replace="tdk/common/header :: common-header" /> 

<head th:replace="@environment.get('server.contextPath')/common/header :: common-header" /> 

我使用

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    <version>1.5.3.RELEASE</version> 
</dependency> 

我也試過:

<head th:replace="~{${@environment.getProperty('serverContextPath') + '/common/header'} :: common-header}" /> 

這個結果:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "~{${@environment.getProperty('serverContextPath') + '/common/header'}", template might not exist or might not be accessible by any of the configured Template Resolvers (/tdk/registration/signup:6) 

回答

2

如果使用thymeleaf 3,你可以使用fragment expressions做到這一點。我認爲它應該看起來像這樣:

<head th:replace="~{${@environment.getProperty('myPropertyName') + '/common/header'} :: common-header}" /> 
+0

「〜{$ {@ environment.getProperty('server.contextPath')+'/ common/header'}」,模板可能不存在或可能不可訪問通過任何配置的模板解析器(/ tdk/login/login:6) \t at –

+0

那麼,你是否證實生成的字符串與你真正想要的匹配?我無法驗證值@ environment.getProperty('server.contextPath')。 – Metroids

+0

它似乎不會替換變量 –