怎样做一个简单的最好办法if- else在Thymeleaf?
我想在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。
您能建议一种更好的方法吗?