【问题标题】:Looping through collection in collection thymeleaf在集合 thymeleaf 中循环遍历集合
【发布时间】:2026-01-03 03:25:01
【问题描述】:

我需要循环进入路线中的点集合 我有这样的事情:

<tr th:if="${routes!=null}" th:each="route : ${routes}">
            <td><span th:text="${route?.name}"> name </span></td>
            <td><span th:text="${route?.pointsForRoute}"> pointsForRoute </span></td>
            <td><span th:text="${route?.routeLength}"> routeLength </span></td>
            <td><span th:text="${route?.heightDiffrence}"> heightDiffrence </span></td>
            <tr th:if="${route.points!=null}" th:each="point : {route?.points}">
            <td><span th:text="${point?.name}"> name </span></td>
        </tr>

我的原因是:org.thymeleaf.exceptions.TemplateProcessingException:无法解析为每个:“point : {route?.points}”(模板:“show-routes” - 第 41 行,第 51 列)

路线类:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;

@Column(name="name")
String name;

@Column(name="points")
int pointsForRoute;

@Column(name="length")
double routeLength;

@Column(name="heightDiffrence")
int heightDiffrence;

@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH})
@JoinTable(name = "PunktyTrasy",
joinColumns = @JoinColumn(name="idT"),
inverseJoinColumns = @JoinColumn(name = "idP"))
private List<Point> points = new ArrayList<>();

提前感谢您的帮助!

【问题讨论】:

  • 请在您的 thymeleaf 页面上提供您发送数据的控制器代码路线、点points 列表是否在 routes 列表中?正如我所看到的,您从 DB 和您的 thymleaf 页面上分别获取积分列表,您正在尝试从 routes 列表中获取 points 列表。!

标签: spring-mvc thymeleaf


【解决方案1】:

您忘记了 th:each="point : {route?.points}" 中的 $ 所以替换

th:each="point : {route?.points}"

th:each="point : ${route?.points}"

【讨论】: