我想显示排名前 5 的类(class)及其名称和预订时间。
我从数据库中选择了前 5 门类(class),并将其加载到一个列表中。
这是我的代码:
while (rs.next())
{
CourseBooking cb = new CourseBooking ();
List topCourses = new ArrayList();
cb.setCoursName(rs.getString(1));
cb.setBookedTimes(rs.getString(2));
topCourses.add(cb);
model.addAttribute("topCourses ", topCourses );
}
但是当我使用“th:each”时,它只显示前 5 行的最后一行,并且不能
阅读整个列表。
<tr th:each="m : ${topCourses }">
<td th:text="${m.coursName}"></td>
<td th:text="${m.bookedTimes}"></td>
</tr>
最佳答案
(根据评论 - 您需要将列表的创建移到 for 循环之外。)您的代码应如下所示:
List topCourses = new ArrayList<CourseBooking>();
while (rs.next()) {
CourseBooking cb = new CourseBooking ();
cb.setCoursName(rs.getString(1));
cb.setBookedTimes(rs.getString(2));
topCourses.add(cb);
}
model.addAttribute("topCourses ", topCourses );
关于java - 为什么我的 Thymeleaf 仅显示列表的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62278416/