Nil Kulkarni
Nil Kulkarni

Reputation: 39

Thymleaf template is not able to evaluate expression related to model

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 :

  1. I am hitting expected template ( i have run same template with static content)
  2. I have also tried displaying other variables except Lastname and it is working
  3. NO value is null or empty

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

Answers (1)

Chris Goldman
Chris Goldman

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

Related Questions