什么是在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>
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
我不想评估if两次.这就是我引入局部变量的原因else.
我仍然不喜欢同时使用potentially_complex_expression和condition.
重要的是我使用了2个不同的html标签:让我们说th:if="${condition}和th:unless="${condition}".
你能建议一个更好的方法来实现吗?