【发布时间】:2019-11-30 20:53:52
【问题描述】:
我正在开发我的第一个 Spring Boot 应用程序。它使用 MVC 模式和 Thymleaf 来呈现 HTML。我有一个显示模型变量的简单 HTML 模板。不幸的是,我在访问该特定映射/url时遇到了以下错误:
出现意外错误(类型=内部服务器错误,状态=500)。 评估 SpringEL 表达式的异常:“employee.Lastname”(模板:“Employees” - 第 22 行,第 8 列)
我不知道是什么问题。
我使用的是 thymleaf 3.0.11,Spring boot 2.1.2
我检查了以下内容:
- 我达到了预期的模板(我已经运行了具有静态内容的相同模板)
- 我也尝试过显示除姓氏之外的其他变量,它正在工作
- NO 值为 null 或为空
我的员工模型类如下(没有getter-setter):
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long employeeId;
private String name;
private int salary;
private String Lastname;
@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.REFRESH })
@JoinTable(name = "Project_Employee", joinColumns = @JoinColumn(name = "employeeId"), inverseJoinColumns = @JoinColumn(name = "projectId"))
private Set<Project> projects = new HashSet<Project>();
public Employee() {
super();
}
我的 HTML 模板如下:
<!DOCTYPE html>
<html lang="en" xmlns:th= "http://www.thymeleaf.org">
<head>
<meta charset= "UTF-8"/>
<title>Employee View</title>
</head>
<body>
<h1>WELCOME</h1>
<table>
<tr >
<th>First Name </th>
<th>Last Name </th>
<th>Salary </th>
</tr>
<tr th:each = "employee: ${Employee}">
<td th:text ="${employee.name}"></td>
<td th:text ="${employee.salary}"></td>
<td th:text ="${employee.Lastname}"></td>
</tr>
</table>
</body>
</html>
和控制器ID如下(只是那个特定的方法):
@RequestMapping("/Employees")
public String getEmployee(Model model)
{
model.addAttribute("Employee", employeeRepository.findAll());
return "Employees";
}
【问题讨论】:
标签: java spring spring-boot model-view-controller thymeleaf