Thymeleaf: show text if the attribute and property exists

前端 未结 1 1333
粉色の甜心
粉色の甜心 2020-12-16 10:57

Is there a simple way in thymeleaf to show the content of an attribute property if the property and the attribute exist? If there\'s an attribute \"error\" with a property \

相关标签:
1条回答
  • 2020-12-16 11:19

    Sure! Since the processor associated with the th:if attribute has a higher precedence than the one associated with the th:text attribute, it will be evaluated first. Thus you can write:

    <span th:if="${error != null && error.summary != null}" th:text="${error.summary}">Static summary</span>
    

    You could even shorten it using:

    <span th:text="${error?.summary}">Static summary</span>
    

    But I think in this case, whether the summary exist or not, the span tag will be created, which is a bit ugly.

    See more info about conditional expressions here.

    0 讨论(0)
提交回复
热议问题