java - Thymeleaf "th:each"忽略 "th:switch"?

标签 java html spring thymeleaf

首先,我检查从中获取轨道列表的项目是否是 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>

最佳答案

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>

关于java - Thymeleaf "th:each"忽略 "th:switch"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47520702/

相关文章:

java - 使用 Jconsole 处理内存泄漏

java - 使用 thumbnail(...) 方法时,Glide 不会调整图像大小

php - 使用div中的while条件获取mysql数据

javascript - 为什么这个嵌入的 iframe 视频不播放?

java - 在方法中使用 spring 注解启动调度程序

Java Spring 在特定时区的特定时间执行计划任务

java - 为什么 super 不在这里扩展?来自java教程: a short course on the basics by ray gallardo et al

java - 我应该如何让我的玩家矩形跳跃?

java - ARCore 世界对齐

javascript - Google 图表错误 - 不会显示第二个图表