【问题标题】:Thymleaf switch statement with multiple caseThymeleaf switch 语句与多个案例
【发布时间】:2015-06-21 20:26:53
【问题描述】:

简而言之

我想在 thymeleaf 中使用 switch 语句,一旦将逻辑写入多个 case 语句。

详细说明

我想在百里香中实现这个

switch(status.value){
  case 'COMPLETE':
  case 'INVALID':
     //print exam is not active
     break;
  case 'NEW':
     //print exam is new and active
     break;
}

我当前的 thymleaf 代码因运行时错误而失败

 <div th:switch="${status.value}">
      <div th:case="'COMPLETE','INVALID'">
         <!-- print object is not active -->
      </div>
      <div th:case="NEW'">
         <!-- print object is new and active -->
      </div>
 </div>                             

但上面的代码失败并出现错误

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "'COMPLETE','INVALID'"...

注意:我知道上述错误消息的原因。我所需要的只是知道一种为单个输出实现多案例切换的方法

【问题讨论】:

  • 没有办法让它成为你想要的样子。如 pens-fan-69 所述,如果您希望避免代码重复,只需在两种情况下都使用片段。

标签: java spring-boot thymeleaf


【解决方案1】:

失败是由于您在第一种情况下没有有效的表达式。具体来说,

'COMPLETE','INVALID'

不是有效的表达式。如果状态为 COMPLETE 或 INVALID,我怀疑您尝试做的是包含 div。不幸的是,我相信您将不得不单独复制这些条件的标记。让我建议以下标记:

<!-- th:block rather than unneeded div -->
<th:block th:switch="${status.value}">
    <div th:case="'COMPLETE'">
        <!-- print object is not active -->
    </div>
    <div th:case="'INVALID'">
        <!-- print object is not active -->
    </div>
    <div th:case="'NEW'">
        <!-- print object is new and active -->
    </div>
</th:block>

或者,您可以求助于 th:if 在这种情况下实际上可能会更好:

<div th:if="${status.value} eq 'COMPLETE' or ${status.value} eq 'INVALID'">
    <!-- print object is not active -->
</div>
<div th:if="${status.value} eq 'NEW'">
    <!-- print object is new and active -->
</div>

或者更简单:

<div th:unless="${status.value} eq 'NEW'">
    <!-- print object is not active -->
</div>
<div th:if="${status.value} eq 'NEW'">
    <!-- print object is new and active -->
</div>

【讨论】:

  • 是的,我知道这不是一个有效的表达方式。我张贴来具体解释我的需求。谢谢您的回答。实际上我目前在我的应用程序中使用th:if
  • 好吧,在这种情况下,我认为您已经有了最好的实现。不幸的是,Thymeleaf 中的多个案例并没有像 Java 中那样“失败”,除非您想使用默认案例(在这种情况下您可以使用默认案例,尽管它不会比 if/unless 改善很多标记)。如果您有很多案例并且想要使用 switch,那么减少代码重复的一种选择是对应该具有相同标记的案例使用片段。
  • 还有一个默认情况的可能性(这对于 swtich/case 来说几乎是一个好主意,只要把这个放在最后:
    对于所有其他 (未定义)案例
  • th:block with th:switch 从来没有为我工作过。我不得不改用 div
【解决方案2】:

我今天遇到了同样的问题,th:switch 中使用的对象是 Java 枚举。我最终发现这是一个 Java equals() 与 == 的问题。在th:switch 我有一个枚举对象,但在我的th:case 我有一个字符串对象。我通过使用 Thymeleaf 字符串函数将枚举对象转换为字符串来解决它,然后一切正常。

  <div th:switch="${#strings.toString(datafile.status)}">
      <td th:case="'SUCCESS'" class="table-success">SUCCESS</td>
      <td th:case="'FAILED'" class="table-danger">FAILED</td>
      <!-- default case -->
      <td th:case="*" th:text="${#strings.toString(datafile.status)}" class="table-secondary">xxx</td>
  </div>

在上面的示例中,我使用开关有条件地将 Bootstrap 样式应用于表格单元格。


另一种解决方案是在 Java 代码中执行逻辑并将输出值公开为对象属性,然后只需在 Thymeleaf 模板中引用该属性。像这样的:

public String getBootstrapTableRowClassForStatus() {
    Objects.requireNonNull(status);
    switch (status) {
        case SUCCESS:
            return "table-success";
        case FAILED:
            return "table-danger";
        case PROCESSING:
            return "table-info";
        default:
            return "table-secondary";
    }
}

然后我使用 Thymeleaf th:class:

<tr th:class="${datafile.bootstrapTableRowClassForStatus}">

在我的页面上,这将根据我在 Java 对象中的 Status 枚举值将 Bootstrap 颜色样式应用于我的表格行。

【讨论】:

    【解决方案3】:

    由于 pens-fan-69 回答了这种类型的开关

    switch(status.value){
      case 'COMPLETE':
      case 'INVALID':
         break;
      case 'NEW':
         break;
    }
    

    只能在 Thymeleaf 中翻译成下面的switch 语句(如果你想保留switch-case 语法)而不是用if-else 回复它

    <th:block th:switch="${status.value}">
        <div th:case="'COMPLETE'">
            <!-- print object is not active -->
        </div>
        <div th:case="'INVALID'">
            <!-- print object is not active -->
        </div>
        <div th:case="'NEW'">
            <!-- print object is new and active -->
        </div>
    </th:block>
    

    我遇到了一个问题,在这种情况下我不得不复制大量的 HTML 代码(在这种情况下是 COMPLETEINVALID)。这可以通过使用th:fragment 并像这样重用它们来缓解:

    <th:block th:fragment="complete-or-invalid-case">
      <!-- your HTML for complete or invalid case -->
    </th:block>
    
    <th:block th:fragment="new-case">
      <!-- your HTML for new case -->
    </th:block>
    

    然后在switch 中,您可以重用这些块,从而减少代码重复

    <th:block th:switch="${status.value}">
        <div th:case="'COMPLETE'" th:include="this :: complete-or-invalid-case"></div>
        <div th:case="'INVALID'" th:include="this :: complete-or-invalid-case"></div>
        <div th:case="'NEW'" th:include="this :: new-case"></div>
    </th:block>
    

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 2011-07-02
      • 2014-03-15
      • 2021-03-22
      相关资源
      最近更新 更多