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

Thymeleaf表格部分列显示null但控制台数据非空问题求助

问题:Thymeleaf表格显示TimeTable字段为null,但控制台数据正常

我尝试在Thymeleaf表格中以特定格式展示数据库中的课程表数据,控制台打印的TimeTable对象包含有效的startTime、endTime、roomNumber等值,但表格中这些字段却显示null。以下是相关代码及信息:

Controller类代码

@GetMapping("/timetable")
public String listTimeTable(Model model) {
    List<TimeTable> timeTableRecords = timeTableService.listTimeTable();

    // Create a map to store the table data
    Map<Subject, Map<WeekDays, TimeTable>> tableData = new LinkedHashMap<>();

    for (TimeTable record : timeTableRecords) {
        Subject subject = record.getSubject();
        WeekDays day = record.getDay();

        // Check if the subject already exists in the map
        if (!tableData.containsKey(subject)) {
            // Create a new inner map for the subject
            tableData.put(subject, new LinkedHashMap<>());
            
        }

        // Check if the day already exists in the inner map
        if (!tableData.get(subject).containsKey(day)) {
            // Add the record to the inner map
            tableData.get(subject).put(day, record);
            System.out.println("List Table Data: " + tableData);
        }
    }

    model.addAttribute("tableData", tableData);
    return "timetable/timetable";
}

控制台输出示例

List Table Data: {Subject [name=Mathematics, code=M101, subjectType=Theory]={Monday=TimeTable [day=Monday, startTime=23:15, endTime=12:16, roomNumber=78, subject=Mathematics, lectureroom=Class 1, section=A]}}

Thymeleaf表格代码

<div class="card card-body">
        <table class="table  table-striped table-hover table-responsive-sm align-middle" id="tableRefresh">
            <thead class="table-dark text-center">
               <tr>
                <th>Subject</th>
                <th>Monday</th>
                <th>Tuesday</th>
                <th>Wednesday</th>
                <th>Thursday</th>
                <th>Friday</th>
                <th>Saturday</th>
                <th>Sunday</th>
            </tr>
    </thead>
    <tbody>
         <tr th:each="listrecord : ${tableData}">
            <!-- Subject column -->
            <td th:text="${listrecord.key.name}"></td>

            <!-- Weekday columns -->
            <td th:each="day : ${T(com.sch.common.entity.WeekDays).values()}"
                th:text="${listrecord.value[day]?.startTime} + ' - ' + ${listrecord.value[day]?.endTime} + ' ' + ${listrecord.value[day]?.roomNumber}">
            </td>
        </tr>
        
    </tbody>
    </table>
    
    </div>

解决方案

1. 检查TimeTable类的Getter方法

Thymeleaf访问对象属性依赖JavaBean规范的Getter方法,控制台的toString()方法可能直接输出字段值,但如果TimeTable类没有对应属性的Getter,Thymeleaf就无法获取值,导致显示null。

确保TimeTable类中存在以下方法:

public LocalTime getStartTime() {
    return startTime;
}

public LocalTime getEndTime() {
    return endTime;
}

public String getRoomNumber() { // 匹配字段实际类型
    return roomNumber;
}

方法名必须严格匹配(字段名首字母大写,前缀加get)。

2. 验证WeekDays枚举的匹配性

确认Thymeleaf中遍历的WeekDays.values()枚举实例,与Controller中存入Map的record.getDay()实例是同一个:

  • 在Controller中添加调试代码:System.out.println(record.getDay() == WeekDays.MONDAY);,确认枚举是单例实例。
  • 在Thymeleaf中临时添加调试输出,查看枚举的名称和toString结果:
    <td th:each="day : ${T(com.sch.common.entity.WeekDays).values()}">
        Debug: [[${day}]] / [[${day.name()}]]
    </td>
    
    确保与Map中key的枚举实例完全一致。

3. 优化Thymeleaf表达式写法

改用更明确的Map获取方式,同时避免空值拼接的混乱:

<td th:each="day : ${T(com.sch.common.entity.WeekDays).values()}">
    <span th:if="${listrecord.value.containsKey(day)}">
        <span th:text="${listrecord.value.get(day).startTime}"></span> - 
        <span th:text="${listrecord.value.get(day).endTime}"></span> 
        <span th:text="${listrecord.value.get(day).roomNumber}"></span>
    </span>
</td>

4. 调整Controller的Map构建逻辑

当前代码中,同一科目和日期的重复记录不会被覆盖,如果存在旧数据可能导致显示异常。可以去掉判断逻辑,直接存入Map:

// 移除if判断,直接覆盖旧记录
tableData.get(subject).put(day, record);

内容的提问来源于stack exchange,提问作者Solomon Otoo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 14:24:57