0


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?

2 Answers 2

1

Typo here :

Change th:each="Persons : ${persons} to th:each="person : ${persons}

Sign up to request clarification or add additional context in comments.

1 Comment

OMG. Sorry, I thought the first String "Person" was just text to be printed in the page and had no effect on the framework. Sorry it takes time to remove JSF from your background :-)
1

The variable Persons in your th:each is different spelled than in your iteration. Change your code like this:

 <tr th:each="person : ${persons}"> <th>Name</th> <th>Surname</th> <td th:text="${person.name}" /> <td th:text="${person.surname}" /> </tr> 

2 Comments

Thanks for providing as well the solution.
@Carla sure. But answered 15 minutes earlier!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.