I'm unable to display in my template page the fields of an Object I have in the model. I have checked similar issues on StackOverFlow but they all point to a wrong naming (model name != EL expression used). I have double-checked it but still cannot find the cause of the issue. Here is my html:
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Spring Boot Thymeleaf Example</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <body> <h2>Hello</h2> <table> <tr th:each="Persons:${persons}"> <th>Name</th> <th>Surname</th> <td th:text="${person.name}" /> <td th:text="${person.surname}" /> </tr> </tbody> </table> </body> </html> And this is my Controller class:
@Controller @RequestMapping("/persons") public class PersonController { private static List<Person> persons = new ArrayList(); static { Person p = new Person("admin", "admin"); persons.add(p); } @GetMapping public String getAllPersons(Model model) { model.addAttribute("persons",persons); return "persons"; } Obviously, there is a Person class with fields(and getter/setters):
public class Person { private String name; private String surname; public Person(String name, String surname) { super(); this.name = name; this.surname = surname; } // getters/setters } When requesting the "/persons" page the following error is displayed:
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null Can you suggest me how to fix the issue?