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

Thymeleaf下拉框无法填充但数据可正常渲染动态表问题咨询

Thymeleaf下拉框数据填充问题排查

相同数据源可正常渲染动态表格,可确定问题出在HTML代码层面。

代码对比

初始Thymeleaf代码

<tr>
    <label class="form-label">Employee ID</label>
    <select name="Employee">
        <option value="">Select Employee</option>
        <option
        th:each="Employee : ${listEmployees}"
        th:text="${firstname}">     
        </option>
    </select>
</tr>

尝试更新后的代码

<tr>
    <label class="form-label">Employee ID</label>
    <select name="Employee">
        <option value="">Select Employee</option>
        <option
        th:each="Employee : ${listEmployees}"
        th:value="${Employee.employeenum}"
        th:text="${Employee.firstname}">        
        </option>
    </select>
</tr>

核心错误点

  • 初始代码未给字段加迭代对象前缀:th:text="${firstname}"需绑定迭代时的变量名,改为th:text="${Employee.firstname}"才能正常读取对象属性
  • 字段名拼写不匹配:实体类中员工编号的字段名为employeenumb,更新后代码中写为employeenum,拼写错误导致无法读取值
  • 字段不存在:当前提供的BuildingAssignments实体类中未定义firstname字段,若实际业务是展示建筑名称,可将th:text的值替换为${Employee.buildingname},否则需要补充对应字段的get/set方法

后端参考代码

页面Controller代码

package com.example.app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import com.example.app.service.EmployeeService;

@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    
    @GetMapping("/")
    public String ViewHomePage(Model model) {
        model.addAttribute("listEmployees", employeeService.getAllEmployees());
        return "index";
    }
}

RestController代码

package com.example.app.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.app.model.Employee;
import com.example.app.service.EmployeeService;

@RestController
public class EmployeeRestController {

    @Autowired
    private EmployeeService employeeService;
    
    @RequestMapping
    public List<Employee> getAllEmployeees(){
            return employeeService.getAllEmployees();
    }
}

实体类代码

package com.example.app.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="building_locations")
public class BuildingAssignments {

    @Id
    @Column(name="employee_number")
    public int employeenumb;
    @Column(name="buildingID")
    public String buildingname;
    @Column(name="building_address")
    public String buildingadd;
    
    public int getEmployeenumb() {
        return employeenumb;
    }
    public void setEmployeenumb(int employeenumb) {
        this.employeenumb = employeenumb;
    }
    public String getBuildingname() {
        return buildingname;
    }
    public void setBuildingname(String buildingname) {
        this.buildingname = buildingname;
    }
    public String getBuildingadd() {
        return buildingadd;
    }
    public void setBuildingadd(String buildingadd) {
        this.buildingadd = buildingadd;
    }
    
    @Override
    public String toString() {
        return "BuildingAssignments [employeenumb=" + employeenumb + ", buildingname=" + buildingname + ", buildingadd=" + buildingadd + "]";
    }
}

内容的提问来源于stack exchange,提问作者Julie Gladden

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 22:27:01