【问题标题】:Thymeleaf "th:each" ignores "th:switch"?Thymeleaf“th:each”忽略“th:switch”?
【发布时间】:2018-05-11 06:26:28
【问题描述】:

首先,我检查我从中获取曲目列表的项目是否是 CD。如果这是真的,我想遍历列表并为每个条目创建一个段落。我的问题是,对于不是 CD 的项目,我会在 ${item.getTrackList()} 处收到错误,因为它们没有属性“trackList”。为什么 "th:each" 表达式忽略了 switch-case 语句?

<div th:switch="${type}" th:remove="all-but-first">
    <div th:case="CD" th:each="track : ${item.getTrackList()}">
        <p th:text="${track}"></p>
    </div>
</div>

【问题讨论】:

    标签: java html spring thymeleaf


    【解决方案1】:

    http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#attribute-precedence

    th:eachth:case 之前进行评估。您将不得不将其移低,例如:

    <div th:switch="${type}" th:remove="all-but-first">
      <th:block th:case="CD">
        <div th:each="track : ${item.trackList}">
          <p th:text="${track}"></p>
        </div>
      </th:block>
    </div>
    

    如果您不需要额外的 div,例如:

    <div th:switch="${type}" th:remove="all-but-first">
      <th:block th:case="CD">
        <p th:each="track : ${item.trackList}" th:text="${track}"></p>
      </th:block>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2018-05-09
      • 2021-06-10
      • 2018-01-07
      • 2023-03-31
      • 2015-06-17
      • 2020-10-09
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多