Search code examples
javaspringspring-mvcweb-applicationsthymeleaf

Why my view HTML file with Thymeleaf doesn't display list of Objects in Spting application?


I'm having trouble with displaying list of People when I pass it to the view adding it as a modelAttribute

Here is a short snippet of code related to the issue:

@Controller
@RequestMapping("/people")
 public class PeopleController {

  private final PersonDAO personDAO;

 @Autowired
 public PeopleController(PersonDAO personDAO) {
     this.personDAO = personDAO;
 }

 @GetMapping()
 public String index(Model model){
     model.addAttribute("people", personDAO.index());
     return "people/index";
 }
}

Here is my HTML file:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
instance"
  xsi:schemaLocation="http://thymeleaf.org/ ">
<head>
 <meta charset="UTF-8">
 <title>All people</title>
</head>
<body>

 <div th:each="person : ${people}">
  <a th:href="@{/people/{id}(id =${person.getId()})}" th:text ="${person.getName() + ', ' + 
  person.getSurname()
  + ', ' + person.getAge() + ', ' + person.getUsername() + ', ' + person.getPassword()}">VALUE</a>
 </div>
 <br/>
 <hr/>
 <a href="/people/new">Create new person</a>
</body>
I could display full list of People , which I got from *personDAO.index()* just before returning my view *return"people/index"* , in console and everything works fine. So I suppose that list **SELECT** all objects properly and passing it to model.

That's what behind personDAO.index(); method:

List<Person> people = session.createQuery("from People", Person.class ).list();

But the web page is empty. May be the problem is hidden inside my view file. I would be thankful for helping me to figure out what's wrong.


Solution

    1. I created the Person model as in the example:
    @Entity
    @Table(name = "person")
    public class Person {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private long id;
        private String name;
        private String surname;
        private String age;
        private String username;
        private String password;
    
        public Person() {
        }
    
        // getter/setter ..
    }
    
    1. I created a PersonRepository to get the people. You can check the Working with Spring Data Repositories page for more.
    @Repository
    public interface PersonRepository extends JpaRepository<Person, Long> {}
    
    1. I created PeopleController.
    @Controller
    @RequestMapping("/people")
    public class PeopleController {
    
        private final PersonRepository personRepository;
    
        public PeopleController(PersonRepository personRepository) {
            this.personRepository = personRepository;
        }
    
        @GetMapping()
        public String index(Model model) {
            model.addAttribute("people", personRepository.findAll());
            return "people/index";
        }
    }
    
    1. I have created index.html inside the /resources/templates/people/ folder.
    <!DOCTYPE html>
    <html lang="en"
          xmlns:th="http://thymeleaf.org/"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://thymeleaf.org/ ">
    <head>
        <meta charset="UTF-8">
        <title>All People</title>
    </head>
    <body>
        <div th:each="person : ${people}">
            <a th:href="@{/people/{id}(id =${person.getId()})}"
               th:text ="${person.getName() + ', ' + person.getSurname() + ', ' +
                           person.getAge() + ', ' + person.getUsername() + ', ' +
                           person.getPassword()}">VALUE</a>
        </div>
        <br/>
        <hr/>
        <a href="/people/new">Create new person</a>
    </body>
    
    1. I added a few records to the person table as an example.

    enter image description here

    1. I think I have reached the result you want to see.

    enter image description here