在 HTML Table 和 Spring Boot 中使用控制器阵列

Use Controller array in HTML Table with Spring Boot

我的控制器函数 return 阵列成功。

我的控制器代码是:

private JdbcTemplate jdbcTemplate;

@Autowired
ConfigurationController configcon = new ConfigurationController(jdbcTemplate);

@RequestMapping(value = "/")
public String index(Model model) {
    model.addAttribute("users", configcon.getQuery("customers"));
    return "forward:/index.html" ;
}

但是如何在 webapp/index.html?

中使用这个数组(例如,用户)

我想显示 html table 中的数据库值。

请指教

谢谢。

为此您需要一个模板引擎。 Spring 支持:

来源:docs

这些语言允许您根据您的模型动态生成 HTML 页面。使用 Thymeleaf,您可以使用 th:each 属性来遍历您的模型,例如:

<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
    </tr>
  </thead>
  <tbody>
    <tr th:each="customer : ${customers}">
      <td th:text="${customer.id}">&nbsp;</td>
      <td th:text="${customer.name}">&nbsp;</td>
    </tr>
  </tbody>
</table>

在这个例子中,我循环遍历模型 ${customers}(因为你在控制器中这样命名它),并且为每个客户生成一行,其中包含两列,一列用于 ID,另一列一个是名字。这些代表您的客户 class.

中的属性(具有适当的 getter/setter)

每个模板引擎都提供了一种不同的方式来遍历您的模型,将它们全部显示出来对于这个答案来说可能太多了。