【发布时间】:2012-11-09 18:03:26
【问题描述】:
在 Thymeleaf 中做一个简单的if-else 的最佳方法是什么?
我想在Thymeleaf中实现和
一样的效果<c:choose>
<c:when test="${potentially_complex_expression}">
<h2>Hello!</h2>
</c:when>
<c:otherwise>
<span class="xxx">Something else</span>
</c:otherwise>
</c:choose>
在 JSTL 中。
到目前为止我的想法:
<div th:with="condition=${potentially_complex_expression}" th:remove="tag">
<h2 th:if="${condition}">Hello!</h2>
<span th:unless="${condition}" class="xxx">Something else</span>
</div>
我不想评估potentially_complex_expression 两次。这就是我引入局部变量condition 的原因。我仍然不喜欢同时使用th:if="${condition} 和th:unless="${condition}"。
重要的是我使用了两个不同的 HTML 标签:比如h2 和span。
您能提出一个更好的方法来实现它吗?
【问题讨论】:
标签: java jsp if-statement jstl thymeleaf