gpt4 book ai didi

java - Thymeleaf 模板无法评估与模型相关的表达式

转载 作者:行者123 更新时间:2023-12-02 01:24:31 24 4
gpt4 key购买 nike

我正在开发我的第一个 Spring boot 应用程序。它使用 MVC 模式和 Thymleaf 来渲染 HTML。我有一个简单的 HTML 模板,它显示模型变量。不幸的是,我在访问该特定映射/url 时遇到以下错误:

There was an unexpected error (type=Internal Server Error, status=500). Exception evaluating SpringEL expression: "employee.Lastname" (template: "Employees" - line 22, col 8)

我无法弄清楚问题出在哪里。

我使用的是thymleaf 3.0.11,Spring boot 2.1.2

我检查了以下内容:

  1. 我正在点击预期的模板(我已经运行了包含静态内容的相同模板)
  2. 我还尝试显示除姓氏之外的其他变量,并且它正在工作
  3. NO 值为 null 或为空

我的员工模型类如下(没有 getter-setters):

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>

和 Controller ID 如下(只是特定方法):

@RequestMapping("/Employees")
public String getEmployee(Model model)
{

model.addAttribute("Employee", employeeRepository.findAll());
return "Employees";

}

最佳答案

可能找不到您的 getter 方法,因为您的字段名称大写。

尝试在类中将其更改为 private String lastname;,在模板中将其更改为 ${employee.lastname}

来自第 8.8 节,推断名称的大写,在 JavaBeans Specification 中:

Java programmers are accustomed to having normal identifiers start with lower case letters. Vigorous reviewer input has convinced us that we should follow this same conventional rule for property and event names.

关于java - Thymeleaf 模板无法评估与模型相关的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57153495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com