2017-09-23 65 views
0
@GetMapping("add") 
public String addPart(Model model) 
{ 
    model.addAttribute("suppliers", this.partService.getSupplierNames()); 
    model.addAttribute("part", new AddPartViewModel()); 
    return "parts/parts-add"; 
} 

这是我的课Thymeleaf一个String字段绑定到一个选择框

public class AddPartViewModel 
    { 
     private String name; 
     private double price; 
     private int quantity; 
     private String supplierName; 
    //PUBLIC GETERS AND SETTERS AND AN EMPTY CONSTRUCTOR 
} 

Thymeleaf语法

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

这是我会在错误的地方。剩下的片段可以正常工作,即使只是将List<String> suppliers中的List<String> suppliers区块删除到选择框中即可。不是我试图把日:字段中<select>标签为好,即

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

但我仍然parcing

回答

1

th:field reffers的形式,支持bean的领域中得到一个错误,所以请确保您已在<form>标记中提供了适当的bean(使用th:object属性)。

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

包含有问题的选择应该是这样的你的形式:

<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> 
+0

你们个值做的工作对我来说 – Alexander

相关问题