【问题标题】:Fetch value from thymeleaf message expression using # and doing null check使用 # 从 thymeleaf 消息表达式中获取值并进行空值检查
【发布时间】:2020-09-05 13:36:51
【问题描述】:

我正在使用 # 从 XML 模板中获取值。但是当我为不可用的标签获取值时。我将其属性名称作为标签名称。 如果该标签在 xml 文件中不可用,我想设置一个默认值。

<th:block th:replace="fragments/header :: nav-header-back-text(header_txt=
 #{label.btn.unsubscribe+'_'+${operator_id}} ?  
#{label.btn.unsubscribe+'_'+${operator_id}} : #{label.btn.unsubscribe})">

假设 operator_id 是 com,那么我想要 如果 label.btn.unsubscribe_com 可用,则显示它,否则显示来自消息资源的 label.btn.unsubscribe 的值,但我得到类似 ??label.btn.unsubscribe+'_'+${operator_id}??.

我正在使用带有spring boot的thymeleaf,上面的代码写在html文件中。

【问题讨论】:

    标签: html spring-boot internationalization thymeleaf


    【解决方案1】:

    您不能在 #{...} 表达式中构建这样的字符串。相反,您需要使用 #messages 助手。像这样的东西应该工作。我没有设置你所有的消息,但这样的东西应该可以工作:

    <th:block th:replace="fragments/header :: nav-header-back-text(header_txt=${#messages.msgOrNull('label.btn.unsubscribe_' + operator_id)} ?: #{label.btn.unsubscribe})">
    

    【讨论】:

    • 当消息文件中的消息可用时,它工作正常。我也会尝试您的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多