【问题标题】:Thymeleaf error : An error happened during template parsingThymeleaf 错误:模板解析过程中发生错误
【发布时间】:2021-12-30 13:22:04
【问题描述】:

我不知道我的视图有什么问题,但它不可能工作,即使 idStudent 设置正确,视图也总是会出错。我不知道是否还有其他方法可以做到这一点,但百里香总是失败

这是错误-> 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>

【问题讨论】:

  • 请尝试${student.id == idStudent}。请注意您如何在表达式中再次使用大括号。
  • 还是失败了Exception evaluating SpringEL expression: "student.id == idStudent" (template: "/content/course/list-course-student"
  • 整个堆栈轨道会有更多细节。这里没有足够的信息......

标签: spring thymeleaf


【解决方案1】:

你最有可能遇到的例外是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}

【讨论】:

    猜你喜欢
    • 2018-11-30
    • 2021-01-06
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 2019-10-08
    • 2018-01-27
    • 1970-01-01
    • 2019-03-17
    相关资源
    最近更新 更多