Thymeleaf nie rozpoznaje atrybutu z kontrolera (Spring Boot)

0

Witam,
Próbuję wyświetlić na stronie wszystkich klientów z lokalnej bazy danych(PostgreSQL), jednak kiedy chcę przelecieć po liście używając Thymeleafa nie rozpoznaje on listy przekazanej przez kontroler. Próbowałem to zrobić omijając całkowicie bazę danych poprzez zwykłą listę klientów, ale nic to nie zmieniło.

Kod:

homePage.html

 !DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:th="http://www.thymeleaf.org">

<head>
<title>Opera</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Opera home page'" />

<ul th:each="customer : ${customers}">
<li>
    <span th:text="${customer.firstName}">Name</span>
    <span th:text="${customer.lastName}">Surname</span>
    <span th:text="${customer.phone}">Phone</span>
    <span th:text="${customer.email}">e-mail</span>
</li>
</ul>
</body>
</html>

HomePageController.java

 @Controller
@RequestMapping("/")
public class HomePageController {

    private CustomerRepository customerRepository;

    @Autowired
    public HomePageController(CustomerRepository customerRepository){
        this.customerRepository = customerRepository;
    }


    @RequestMapping(method = RequestMethod.GET)
    public String homePage(Model model) {
        List<Customer> customers = Lists.newArrayList(customerRepository.findAll());
        model.addAttribute("customers", customers);
        return "homePage";
    }
}

CustomerRepository.java

 @Repository
public interface CustomerRepository extends CrudRepository<Customer, Long> {
}

Customer.java

@Entity
@Getter @Setter
public class Customer {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    @NotNull private String firstName;
    @NotNull private String lastName;
    @NotNull private String phoneNumber;
    @NotNull private String email;

    protected Customer(){}

    public Customer(Long id, String firstName, String lastName, String phoneNumber, String email){
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }

    @Override
    public String toString(){
        return String.format(
            "Customer[id=%d, firstName='%s', lastName='%s', phoneNumber='%s', emailr='%s']",this.getId(),
            firstName, lastName, phoneNumber, email);
    }
}

Załączam także screenshota problemu

Dodam, że wolę szukać dwa dni w googlu jak mam jakiś problem niż pisać na forum, ale poległem tym razem(pewnie złe zapytania). Tak więc pytam tutaj :(

2

public String homePage(Model model) { to jest bardzo nieładne podejście jeśli chcesz wysyłać parametry a nie je odbierać. Dużo bardziej elegancko jest nie mieć parametrów a zwracać ModelAndView bo od razu wiadomo co się dzieje i wiadomo że ten model w ogóle leci do widoku.
Poza tym uruchamiałeś to? Bo IntelliJ czasem kłamie z tymi parametrami w widoku.

0

W Intellij to jest bug, jeśli korzystasz ze SpringBoota.

1 użytkowników online, w tym zalogowanych: 0, gości: 1