2017-05-05 63 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

它似乎不会替换变量 –