You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

课程管理系统中Lesson列表前端不显示问题排查求助

问题排查与修复方案

你的前端页面无法显示Lesson列表,核心是两个Thymeleaf语法错误:

1. 循环集合变量名不匹配

后端getLessonList方法中,向Model添加的集合变量名为lessons(复数):

model.addAttribute("lessons",lessonService.findAll());

但前端循环时错误使用了单数${lesson}:

<tr th:each="lesson : ${lesson}">

修正:将集合变量改为复数${lessons}:

<tr th:each="lesson : ${lessons}">

2. 关联对象访问路径错误

前端直接使用${student}和${teacher},但这两个变量并未单独传入Model,它们是每个Lesson对象的关联属性。需要通过当前遍历的lesson对象来访问关联的学生和教师:
修正:修改学生和教师姓名的取值表达式:

<td th:text="${lesson.student.firstName} + ' ' + ${lesson.student.lastName}"></td>
<td th:text="${lesson.teacher.firstName} + ' ' + ${lesson.teacher.lastName}"></td>

完整修正后的前端代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Lekcje</title>
</head>
<body>
<h1>Lekcje</h1>
</br>

<p2><a href="/lessons/create">dodaj</a></p2>
</br>
</br>

<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>Student</th>
        <th>Nauczyciel</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="lesson : ${lessons}">
        <td th:text="${lesson.id}"></td>
        <td th:text="${lesson.student.firstName} + ' ' + ${lesson.student.lastName}"></td>
        <td th:text="${lesson.teacher.firstName} + ' ' + ${lesson.teacher.lastName}"></td>
    </tr>
    </tbody>
</table>
</body>
</html>

额外注意:确保你的Lesson实体类中正确定义了student和teacher的关联属性(比如@ManyToOne注解),并且lessonService.findAll()方法查询时关联加载了Student和Teacher(比如使用JOIN FETCH或者开启懒加载的即时初始化),否则会出现懒加载异常或属性为空的情况。

内容的提问来源于stack exchange,提问作者bm09

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 15:55:15