Thymeleaf/Spring:如何检查消息属性是否存在

dtr*_*unk 2 spring thymeleaf

是否可以检查消息属性是否存在?

示例:我想在仅包含消息属性的循环中删除可折叠的 div-Element。

<div th:each="payment : ${paymentList}">
    <input type="radio" data-toggle="radio-collapse" data-target="#collapse1" [...] /> [...]
    <div id="collapse1" th:utext="#{|payment.${payment.id}.additionalInfo|}" [...]>
        Hello, world!
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

如果没有支付的附加信息,则不需要该元素。比方说我们的ID DIRECT_DEBITPAYPALSAFERPAY和下列消息属性:

payment.DIRECT_DEBIT=Direct debit
payment.DIRECT_DEBIT.additionalInfo=Direct debit info text...
payment.PAYPAL=PayPal
payment.PAYPAL.additionalInfo=PayPal info text...
payment.SAFERPAY=Saferpay
Run Code Online (Sandbox Code Playgroud)

正如您所看到的, 没有 addInfo 消息属性SAFERPAY

Met*_*ids 6

您应该使用该#messages对象来检查消息是否存在。例如:

th:if="${#messages.msgOrNull('payment.' + payment.id + '.additionalInfo') != null}"

<div id="collapse1" th:if="${#messages.msgOrNull('payment.' + payment.id + '.additionalInfo') != null}" th:utext="#{|payment.${payment.id}.additionalInfo|}" [...]>
    Hello, world!
</div>
Run Code Online (Sandbox Code Playgroud)