Thymeleaf: show text if the attribute and property exists

喜欢而已 提交于 2019-11-29 05:31:29
tduchateau

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!