2017-09-23 67 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

相關問題