相关疑难解决方法(0)

在Thymeleaf怎么做if-else?

什么是在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_expressioncondition.

重要的是我使用了2个不同的html标签:让我们说th:if="${condition}th:unless="${condition}".

你能建议一个更好的方法来实现吗?

java jsp if-statement jstl thymeleaf

119
推荐指数
8
解决办法
24万
查看次数

标签 统计

if-statement ×1

java ×1

jsp ×1

jstl ×1

thymeleaf ×1