Thymeleaf+SpringBoot视图无法解析Model字段的IntelliJ报错问题
解决Thymeleaf视图中无法访问Model字段的IntelliJ报错问题
问题场景
使用Thymeleaf+SpringBoot+IntelliJ 2022.2.4开发项目时,视图页面中th:field="*{loginDate}"处提示cannot resolve field错误,核对官方文档操作流程无误,但问题仍存在。
相关代码
视图页面代码
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"> <title>ADD NEW RECORD</title> <link rel="stylesheet" type="text/css" th:href = "@{/webjars/bootstrap/css/bootstrap.min.css}"/> </head> <body> <div class="container-fluid text-center"> <div><h2>ADD NEW RECORD</h2></div> <form th:action="@{students/save}" th:object="${newStudent}" method="post" > <div class ="border"> <div class="form-group row"> <label class="col-sm-4 col-form-label">Date</label> <div class="col-sm-8"> <input type="date" th:field="*{loginDate}" class="form-control"> </div> </div> </div> </form> </div> </body> </html>
控制器代码
package com.sfsb.studentsSFSB.students.controller; import com.sfsb.studentsSFSB.students.model.Students; import com.sfsb.studentsSFSB.students.service.StudentsServiceImpl; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import java.util.List; @Controller public class StudentController { private StudentsServiceImpl service; public StudentController(StudentsServiceImpl service) { this.service = service; } @GetMapping("") public String showHomePage(){ return "index"; } @GetMapping("/students/new") public String showForm(Model model){ model.addAttribute("newStudent", new Students()); return "student_form"; } @GetMapping("/students") public String showStudentsList(Model model){ List<Students> listStudents = service.findAll(); model.addAttribute("listStudents", listStudents); return "students"; } }
模型类代码
package com.sfsb.studentsSFSB.students.model; import jakarta.persistence.*; import org.springframework.format.annotation.DateTimeFormat; import java.time.Duration; import java.time.LocalDate; import java.time.LocalTime; import java.time.format.DateTimeFormatter; @Entity @Table(name = "students") public class Students { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "FirstName") private String firstName; @Column(name = "LastName") private String lastName; @Column(name = "Date") @DateTimeFormat(pattern = "yyyy-MM-dd") private LocalDate loginDate; @Column(name = "LoginTime") @DateTimeFormat(pattern = "HH:mm") private LocalTime loginTime; @Column(name = "BreakPeriod") @DateTimeFormat(pattern = "HH:mm") private LocalTime breakPeriod; @Column(name = "LogoutTime") @DateTimeFormat(pattern = "HH:mm") private LocalTime logoutTime; @Column(name = "HoursWorkedPerDay") @DateTimeFormat(pattern = "HH:mm") private LocalTime workHoursPerDay; @Column(name = "Task") private String task; @DateTimeFormat(pattern = "HH:mm") private LocalTime workHoursPerWeek; @DateTimeFormat(pattern = "HH:mm") private LocalTime totalHoursForCurrentContract; @DateTimeFormat(pattern = "HH:mm") private LocalTime hoursLeftOnCurrentContract; //Method to calculate work hours per day based on login,break and logout values public LocalTime calculateWorkHoursPerDay(){ Duration breakDuration = Duration.between(LocalTime.MIDNIGHT, breakPeriod); Duration workDuration = Duration.between(loginTime,logoutTime).minus(breakDuration); String workHoursInString = LocalTime.MIDNIGHT.plus(workDuration).toString(); workHoursPerDay = LocalTime.parse(workHoursInString); return workHoursPerDay; } }
解决办法
1. 为模型类字段添加Getter/Setter方法
Thymeleaf通过字段的getter方法访问属性,你的Students类中所有字段(包括loginDate)都没有对应的getter和setter,这是核心问题。在Students类中添加所有字段的getter和setter:
// 示例:loginDate的getter和setter public LocalDate getLoginDate() { return loginDate; } public void setLoginDate(LocalDate loginDate) { this.loginDate = loginDate; } // 其他字段同理添加对应的getter和setter
可以使用IntelliJ快捷键(Alt+Insert)快速生成所有字段的Getter/Setter。
2. 清理IntelliJ缓存并重启
IDE缓存可能导致识别异常,操作步骤:
- 打开IntelliJ,点击
File->Invalidate Caches... - 选择
Invalidate and Restart,等待IDE重启完成
3. 检查Thymeleaf配置和命名空间
确认视图页面的Thymeleaf命名空间正确:
xmlns:th="http://www.thymeleaf.org"
同时确保Spring Boot项目中已正确引入Thymeleaf依赖(pom.xml或build.gradle中包含spring-boot-starter-thymeleaf)。
4. 验证模型对象传递
确认控制器中model.addAttribute("newStudent", new Students());代码正常执行,无异常抛出,确保newStudent对象成功传递到视图。
内容的提问来源于stack exchange,提问作者Kevin
相关产品推荐
相关产品推荐

