【问题标题】:How to do if-else in Thymeleaf?如何在 Thymeleaf 中执行 if-else?
【发布时间】: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 标签:比如h2span

您能提出一个更好的方法来实现它吗?

【问题讨论】:

    标签: java jsp if-statement jstl thymeleaf


    【解决方案1】:

    Thymeleaf 等效于 &lt;c:choose&gt;&lt;c:when&gt;:在 Thymeleaf 2.0 中引入的 th:switchth:case 属性。

    它们按您的预期工作,默认情况下使用*

    <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>
    

    有关语法的快速解释(或 Thymeleaf 教程),请参阅 this

    免责声明:根据 StackOverflow 规则的要求,我是 Thymeleaf 的作者。

    【讨论】:

    • 好的,但是..我如何根据客户类型(即匿名或登录)调用 javascript...
    • 参见“文本内联”一章,特别是thymeleaf.org/documentation.html“使用 Thymeleaf”教程中的“JavaScript 内联”部分
    • 我该怎么做 th:switch on an iterator.index 值?我想在值为5则切换到默认案例
    • @DanielFernández:你能帮我处理一下stackoverflow.com/questions/48499723/…。我无法直接在这个问题上标记你。真的卡住了。
    【解决方案2】:

    我尝试使用此代码来确定客户是登录还是匿名。我确实使用了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>
    

    【讨论】:

    • 这不能回答 OP 的问题,因为那是为了避免重复复杂的表达式。和其他解决方案一样,这打破了“自然模板”的想法,这是 Thymeleaf 的主要卖点。不知道自己该怎么办,但只是想指出以防有人有答案。
    • @Lucky 这给了我一个错误 EL1007E:(pos 0): 找不到属性或字段“状态”
    • @Jackie 在上面的示例中,customer 是一个对象,anonymous 是 Customer 类中的一个布尔字段。确保您的对象不为空且字段名称正确。
    【解决方案3】:

    除了 Daniel Ferná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>
    

    这是一个复杂的表达式,混合了“身份验证”和“授权”实用程序对象,可为 thymeleaf 模板代码生成“真/假”结果。

    “身份验证”和“授权”实用程序对象来自 thymeleaf extras springsecurity3 library。 当 'authentication' 对象不可用或 authorization.expression('isAuthenticated()') 评估为 'false' 时,表达式返回 ${false},否则返回 ${true}。

    【讨论】:

    • 这是一个很好的提示,但请注意,您可以使用 truefalse 文字——您不需要为它们使用变量替换表达式。另请参阅Yatendra's answer
    【解决方案4】:

    你可以使用

    If-then-else:  (if) ? (then) : (else)
    

    例子:

    'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
    

    这对于提出相同问题的新人可能很有用。

    【讨论】:

      【解决方案5】:

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

      <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>
      

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

      【讨论】:

      • 我认为您在代码中添加了一个额外的“=”符号。如果我是对的,请再次验证并更正。
      • 在我看来代码没问题。我不知道您指的是哪个额外的“=”符号。
      • 是的,代码没问题。抱歉,我将它与 th:each 混淆了,其中表达式有 : 而不是 =
      • 但是这个答案非常接近我的解决方案。虽然做 th:with 表达对我来说似乎是多余的。基本上这个解决方案使用 th:if 和 th:unless 条件语句。
      • 我认为这不是多余的。如果你只使用 th:if 和 th:unless 复杂表达式会被执行两次...
      【解决方案6】:

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

      <h2 th:text="${potentially_complex_expression} ? 'Hello' : 'Something else'">/h2>
      

      【讨论】:

      【解决方案7】:

      另一种解决方案是使用not 得到相反的否定:

      <h2 th:if="${potentially_complex_expression}">Hello!</h2>
      <span class="xxx" th:if="${not potentially_complex_expression}">Something else</span>
      

      正如documentation 中所解释的,这与使用th:unless 相同。正如其他答案所解释的那样:

      另外,th:if 有一个逆属性 th:unless,我们可以有 在前面的示例中使用 而不是在 OGNL 中使用 not 表达

      使用not 也可以,但恕我直言,使用th:unless 而不是用not 否定条件更具可读性。

      【讨论】:

      • 这仍然会重复表达式,这正是 OP 试图避免的事情。
      【解决方案8】:

      使用th:switch 作为if-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>
      

      使用th:switch 作为switch

      <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>
      

      【讨论】:

        【解决方案9】:
        <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>
        
        
        <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>
        

        【讨论】:

          【解决方案10】:
          <div style="width:100%">
          <span th:each="i : ${#numbers.sequence(1, 3)}">
          <span th:if="${i == curpage}">
          <a href="/listEmployee/${i}" class="btn btn-success custom-width" th:text="${i}"></a
          </span>
          <span th:unless="${i == curpage}">
          <a href="/listEmployee/${i}" class="btn btn-danger custom-width" th:text="${i}"></a> 
          </span>
          </span>
          </div>
          

          enter image description here

          【讨论】:

            【解决方案11】:

            当我想根据用户的性别显示照片时,这对我有用:

            &lt;img th:src="${generou}=='Femenino' ? @{/images/user_mujer.jpg}: @{/images/user.jpg}" alt="AdminLTE Logo" class="brand-image img-circle elevation-3"&gt;

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2022-01-17
              • 2021-11-25
              • 2017-06-25
              • 1970-01-01
              • 2012-11-02
              • 2013-01-18
              • 1970-01-01
              • 2021-07-25
              相关资源
              最近更新 更多