Home > Mobile >  Why isn't my model loading in Spring controller?
Why isn't my model loading in Spring controller?

Time:09-07

I have a controller with a method:

@GetMapping("/showProducts")
public String returnAllProductsInATable(Model model) {
        model.addAttribute("products", listProducts)
        return "showProducts"
    }

showProduct is a page in templates folder:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <title>Список товаров</title>
</head>
<body>
<table>
  <tr th:each="product: ${products}">
    <td th:text="${product.id}"></td>
    <td th:text="${product.productSeries}"></td>
    <td th:text="${product.name}"></td>
    <td th:text="${product.price}"></td>
    <td th:text="${product.weight}"></td>
    <td th:text="${product.description}"></td>
  </tr>
</table>
</body>
</html>

Controller class is annotated with @RestController, the dependencies for spring-boot-starter-thymeleaf, spring-boot-starter-web and spring-webmvc are included, but instead of a model I see "showProducts" String, why is that?

CodePudding user response:

The answer is that @RestController includes @ResponseBody annotation, I just had to use @Controller annotation instead

CodePudding user response:

Because you use @Restcontroller

  • @restcontroller used to create RESTAPI gồm các phương thức như: GetMapping, PostMapping, PutMapping, DeleteMapping ...
  • @Controller Used to process and receive requests
  • Related