String Thymeleaf将字符串字段绑定到选择框

String Thymeleaf将字符串字段绑定到选择框,string,select,thymeleaf,String,Select,Thymeleaf,这是我的课 @GetMapping("add") public String addPart(Model model) { model.addAttribute("suppliers", this.partService.getSupplierNames()); model.addAttribute("part", new AddPartViewModel()); return "parts/parts-add"; } 胸腺素语法 public class A

这是我的课

@GetMapping("add")
public String addPart(Model model)
{
    model.addAttribute("suppliers",  this.partService.getSupplierNames());
    model.addAttribute("part", new AddPartViewModel());
    return "parts/parts-add";
}
胸腺素语法

    public class AddPartViewModel
    {
        private String name;
        private double price;
        private int quantity;
        private String supplierName;
  //PUBLIC GETERS AND SETTERS AND AN EMPTY CONSTRUCTOR
}
但在打包过程中,我仍然会遇到一个错误,th:field reffers指向表单支持bean的字段,因此请确保使用th:object属性在标记中提供了正确的bean

关于select:th:field应该在标记中提供,就像您尝试做的那样。但是您还应该在标记中提供适当的th:value属性,以便可以将任何值分配给字段

包含有问题的select的表单应如下所示:

            <select class="form-control" id="supplierName" th:field="*{supplierName}">

你的价值观为我做了这件事
            <select class="form-control" id="supplierName" th:field="*{supplierName}">
<form th:object="${part}">

    <div class="form-group">
        <label for="supplierName">Example select</label>
        <select class="form-control" th:field="*{supplierName}">
            <option th:each="name : ${suppliers}" th:value="${name}" th:text="${name}"></option>
        </select>
    </div>

    <!-- the rest of form's inputs and buttons -->

</form>