Thymelaf错误:模板解析过程中发生错误



我不知道我的视图出了什么问题,但它不可能工作,即使idStudent设置正确,视图也总是会出错。我不知道是否还有其他方法可以做到,但thymelaf总是无法通过

这是错误->Exception evaluating SpringEL expression: "student.id == {idStudent}" (template: "/content/course/list-course-student" - line 55, col 24)

这是我的控制器

@GetMapping("/list-courses-student")
public String listCoursesByUser(@RequestParam(required = false) int page,Model model) {

User currentUser = userService.findByName(this.currentUser.getUsername());  

int numberPages = coursesService.getNumberPages();
List<Course> courses = coursesService.findAllCourses(page,true);

model.addAttribute("coursesDisponibles",courses);
model.addAttribute("numberPages",numberPages);
model.addAttribute("currentPage",page);
model.addAttribute("idStudent",currentUser.getStudent().getId());
return "/content/course/list-course-student";
}

我的视图

<div class="card-body table-responsive p-0">
<table class="table table-hover text-nowrap">
<thead>
<tr>
<th>Profesor</th>
<th>Nombre</th>
<th>Description</th>
<th>Code</th>
<th>Inicio</th>
<th>Final</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
<th:block th:each="course : ${coursesDisponibles}">
<tr>
<td
th:text="${course.teacher == null} ? 'No Asignado' : ${course.teacher.nombre}"></td>
<td th:text="${course.nombre}"></td>
<td th:text="${course.descripcion}"></td>
<td th:text="${course.code}"></td>
<td
th:text="${#temporals.format(course.inicioDate, 'dd/MM/yyyy')}"></td>
<td
th:text="${#temporals.format(course.finalDate, 'dd/MM/yyyy')}"></td>

<td>
<th:block th:each="student : course.Estudiantes">

<th:block th:if="${student != null}">
<th:block th:if="${student.id == {idStudent}}">
<a class="btn btn-success btn-sm" href="">Añadido</a>
<a class="fas fa-inbox ml-3"
th:href="${'/tareas/list-tareas-student?page=1&course=' + {course.id}}"></a>
</th:block>
</th:block>

<th:block th:unless="${student == null}">
<a
th:classappend="${course.teacher  == null} ? 'btn btn-primary btn-sm disabled':'btn btn-primary btn-sm' "
th:href="${'/courses/add?idCourse=' + {course.id} + '&idStudent=' + {idStudent}}">Añadir</a>
</th:block>
</th:block>
</td>
</th:block>
</tr>
</tbody>
</table>
</div>

您最可能遇到的异常是NullPointerException,因为我认为您有一个错误的th:each声明:

<th:block th:each="student : course.Estudiantes">

而不是

<th:block th:each="student : ${course.Estudiantes}">

因此,表达式th:if=${student.id == idStudent}在尝试访问空student对象的id属性时抛出NullPointerException

还要注意,为了防止其他NPE,您可以在表达式中使用?运算符,如下所示:${student?.id}

相关内容

最新更新