在Thymeleaf怎么做if-else?

Mac*_*rko 119 java jsp if-statement jstl thymeleaf

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

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

Dan*_*dez 191

Thymeleaf具有等效于<c:choose><c:when>:所述th:switchth:case在Thymeleaf 2.0中引入的属性.

它们按照您的预期工作,*用于默认情况:

<div th:switch="${user.role}"> 
  <p th:case="'admin'">User is an administrator</p>
  <p th:case="#{roles.manager}">User is a manager</p>
  <p th:case="*">User is some other thing</p> 
</div>
Run Code Online (Sandbox Code Playgroud)

有关语法(或百万美文教程)的快速解释,请参见http://www.thymeleaf.org/whatsnew20.html#swit.

免责声明,根据StackOverflow规则的要求:我是百里香的作者.


Luc*_*cky 84

我尝试使用此代码来查明客户是否已登录或匿名.我确实使用了th:ifth:unless条件表达式.这很简单的方法.

<!-- IF CUSTOMER IS ANONYMOUS -->
<div th:if="${customer.anonymous}">
   <div>Welcome, Guest</div>
</div>
<!-- ELSE -->
<div th:unless="${customer.anonymous}">
   <div th:text=" 'Hi,' + ${customer.name}">Hi, User</div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 这不符合OP的问题,因为这是为了避免重复复杂的表达式.和其他解决方案一样,这打破了"自然模板"的想法,这是Thymeleaf的一个主要卖点.我不知道该怎么办,但只是想指出它以防有人得到答案. (4认同)

bla*_*ger 21

除了DanielFernández之外,我想分享一下我与安全相关的例子.

<div th:switch="${#authentication}? ${#authorization.expression('isAuthenticated()')} : ${false}">
    <span th:case="${false}">User is not logged in</span>
    <span th:case="${true}">Logged in user</span>
    <span th:case="*">Should never happen, but who knows...</span>
</div>
Run Code Online (Sandbox Code Playgroud)

这是带有混合"身份验证"和"授权"实用程序对象的复杂表达式,它为百日咳模板代码生成"真/假"结果.

'authentication'和'authorization'实用程序对象来自thymeleaf extras springsecurity3库.当'authentication'对象不可用或者authorization.expression('isAuthenticated()')计算为'false'时,表达式返回$ {false},否则返回$ {true}.


小智 18

您可以使用

If-then-else:  (if) ? (then) : (else)
Run Code Online (Sandbox Code Playgroud)

例:

'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
Run Code Online (Sandbox Code Playgroud)

对于提出相同问题的新人来说,它可能会有用.


Yat*_*oel 11

用作th:switchif-else

<span th:switch="${isThisTrue}">
  <i th:case="true" class="fas fa-check green-text"></i>
  <i th:case="false" class="fas fa-times red-text"></i>
</span>
Run Code Online (Sandbox Code Playgroud)

用作th:switchswitch

<span th:switch="${fruit}">
  <i th:case="Apple" class="fas fa-check red-text"></i>
  <i th:case="Orange" class="fas fa-times orange-text"></i>
  <i th:case="*" class="fas fa-times yellow-text"></i>
</span>
Run Code Online (Sandbox Code Playgroud)


jar*_*eks 10

另一种解决方案 - 您可以使用局部变量:

<div th:with="expr_result = ${potentially_complex_expression}">
    <div th:if="${expr_result}">
        <h2>Hello!</h2>
    </div>
    <div th:unless="${expr_result}">
        <span class="xxx">Something else</span>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

有关局部变量的更多信息:http:
//www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#local-variables


GKi*_*lin 9

在更简单的情况下(当html标签相同时):

<h2 th:text="${potentially_complex_expression} ? 'Hello' : 'Something else'">/h2>
Run Code Online (Sandbox Code Playgroud)


Pau*_*Pau 6

另一个解决方案就是not用来得到相反的否定:

<h2 th:if="${potentially_complex_expression}">Hello!</h2>
<span class="xxx" th:if="${not potentially_complex_expression}">Something else</span>
Run Code Online (Sandbox Code Playgroud)

文档中所述,它与使用相同th:unless.正如其他答案所解释的:

此外,th:if还有一个inverse属性,th:unless我们可以在前面的例子中使用它,而不是在OGNL表达式中使用not

使用not也有效,但恕我直言,它使用更可读,th:unless而不是否定条件not.