Home > Blockchain >  Display items into Spring MVC table
Display items into Spring MVC table

Time:12-21

I have this Endpoint:

    @GetMapping("/imported_files")
    public String viewHomePage(Model model) {
        List<EntityImportRequestsTable> listProducts = entityImportRequestsService.findAll();
        model.addAttribute("listProducts", listProducts);
        return "index";
    }

Entity:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@Entity
@Table(name = "EntityImportRequests")
public class EntityImportRequestsTable implements Serializable {

    @Id
    @Column(name = "requestId")
    private String requestId;
}

Web page index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8"/>
  <title>Product Manager</title>
</head>
<body>
<div align="center">
  <h1>Product List</h1>
  <table border="1" cellpadding="10">
    <tr>
      <th>requestId</th>
    </tr>
    <tr th:each="imported_files : ${listProducts}">
      <td th:text="${imported_files.requestId}">1</td>
    </tr>
  </table>
</div>
</body>
</html>

When I open the page data is not displayed. What is the proper way to display the data rows from listProducts?

CodePudding user response:

Try something like this. Just make sure that your listProducts is not empty in your controller.

<tr th:each="product: ${listProducts}">
    <td th:text="${product.requestId}" />
    <td th:text="${product.name}" />
    <td th:text="${product.price}" />
</tr>

Or whatever fields your product entity has next to the name, price, etc.

  • Related