【问题标题】:Thymeleaf read property inside a replaceThymeleaf 读取替换内的属性
【发布时间】:2017-10-04 06:58:39
【问题描述】:

我有这段代码,其中tdk是我在SpringBoot文件application.properties中定义的一个公共变量server.contextPath

我想知道有没有办法代替

<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)

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    如果您使用的是 thymeleaf 3,您可以使用 fragment expressions 完成此操作。我认为它应该看起来像这样:

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

    【讨论】:

    • "~{${@environment.getProperty('server.contextPath') + '/common/header'}",模板可能不存在或可能无法被任何已配置的模板解析器访问(/tdk/login/login:6) 在
    • 那么,您确认生成的字符串与您真正想要的匹配了吗?我无法验证值 @environment.getProperty('server.contextPath')。
    • 好像没有替换变量
    • @environment.getProperty('myPropertyName') 是否在其他表达式中解析?
    • 是的,例如在这里可以工作:th:attr="placeholder=${@environment.getProperty('server.contextPath')}"
    猜你喜欢
    • 2020-09-27
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    相关资源
    最近更新 更多