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.
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 ..
}
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> {}
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";
}
}
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>
person table
as an example.