You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC表格不显示数据:如何正确展示listProducts数据?

问题描述

控制台显示查询到69条数据,但Thymeleaf页面的表格无法展示这些数据,相关代码如下:

Controller代码

@Controller
public class FileImportsController {

    private EntityImportRequestsService entityImportRequestsService;

    @Autowired
    public FileImportsController(EntityImportRequestsService entityImportRequestsService) {
        this.entityImportRequestsService = entityImportRequestsService;
    }

    @GetMapping("/imported_files")
    public String viewHomePage(Model model) {
        List<EntityImportRequestsTable> listProducts = entityImportRequestsService.findAll();
        System.out.println("Size is " + listProducts.size());

        model.addAttribute("listProducts", listProducts);
        return "index";
    }

}

实体类代码

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

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

    @Column(name = "transactionGroupId")
    private Integer transactionGroupId;

    @Column(name = "requestXmlSourceFile")
    private String requestXmlSourceFile;

    @Column(name = "createdOn")
    private LocalDateTime createdOn;
}

前端页面index.html代码

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

按以下步骤排查并修复:

  • 确认Thymeleaf基础配置

    1. 确保项目已引入Thymeleaf依赖(以Maven为例):
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
    1. 确认index.html放在src/main/resources/templates目录下,这是Spring Boot默认的Thymeleaf视图文件路径。
  • 检查Lombok有效性

    1. 确认项目已引入Lombok依赖:
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    
    1. IDE需安装Lombok插件,否则@Getter生成的getter方法无法被识别,Thymeleaf无法获取实体类属性值。
  • 修正Thymeleaf页面渲染逻辑
    当前页面缺少表头,且LocalDateTime直接渲染可能出现格式问题或渲染失败,修改页面代码如下:

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:th="http://www.thymeleaf.org">
    <head>
      <meta charset="utf-8"/>
      <title>Files Manager</title>
    </head>
    <body>
    <div align="center">
      <h1>Files List</h1>
      <table border="1" cellpadding="10">
        <!-- 新增表头行 -->
        <tr>
          <th>Request ID</th>
          <th>Transaction Group ID</th>
          <th>XML Source File</th>
          <th>Created On</th>
        </tr>
        <!-- 数据行循环 -->
        <tr th:each="item : ${listProducts}">
          <td th:text="${item.requestId}"></td>
          <td th:text="${item.transactionGroupId}"></td>
          <td th:text="${item.requestXmlSourceFile}"></td>
          <!-- 格式化LocalDateTime,避免渲染异常 -->
          <td th:text="${#temporals.format(item.createdOn, 'yyyy-MM-dd HH:mm:ss')}"></td>
        </tr>
      </table>
      <!-- 无数据时提示 -->
      <div th:if="${#lists.isEmpty(listProducts)}">
        No data found.
      </div>
    </div>
    </body>
    </html>
    

    说明:

    • 新增表头,提升页面可读性;
    • 使用Thymeleaf的temporals工具类格式化LocalDateTime,避免因日期格式问题导致渲染失败;
    • 新增无数据提示,便于排查是否真的没有数据传递到页面。
  • 验证数据传递
    可以在页面中临时添加调试代码,确认listProducts是否成功传递:

    <div th:text="'Total items: ' + ${#lists.size(listProducts)}"></div>
    

    若能显示正确的数量(69),说明数据已传递,问题出在属性渲染或格式上;若显示0或为空,需检查Controller层的数据查询和模型添加逻辑。

内容的提问来源于stack exchange,提问作者Peter Penzov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 19:35:23