Reputation: 39
I am working on my first Spring boot application. it uses MVC pattern, and Thymleaf to render HTML. I have a simple HTML template which displays model variable. Unfortunately I am getting following error while visiting to that particular mapping/url :
There was an unexpected error (type=Internal Server Error, status=500). Exception evaluating SpringEL expression: "employee.Lastname" (template: "Employees" - line 22, col 8)
I couldn't figure out what is the problem.
I am using thymleaf 3.0.11, Spring boot 2.1.2
I have checked following :
My employee model class is as follows (without 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();
}
My HTML template is as follows :
<!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>
and controller id as follows (just that particular method):
@RequestMapping("/Employees")
public String getEmployee(Model model)
{
model.addAttribute("Employee", employeeRepository.findAll());
return "Employees";
}
Upvotes: 0
Views: 374
Reputation: 307
It's likely not finding your getter method because your field name is capitalized.
Try changing it to private String lastname;
in your class, and ${employee.lastname}
in your template.
From section 8.8, Capitalization of inferred names, in the 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.
Upvotes: 3