2017-09-23 12 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は、選択ボックス

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> 

これは私がエラーを取得する唯一の場所である私のクラスです。フラグメントの残りの部分は、たとえth:fieldタグを削除しても、List<String> suppliersが選択ボックスに正しく入りこんでも正しく動作します。すなわち、同様<select>タグにフィールド

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

を、それでも、私は、フォーム・バッキングBeanのフィールドに

答えて

1

th:field reffersをparcing中にエラーが発生しますので、あなたを確認してください:ない私は目を入れてみました<form>タグで適切なBeanを提供しました(th:object属性を使用)。

あなたがしようとしたように、<select>タグで選択:th:fieldを提供する必要があります。しかし、適切なth:value属性を<option>タグに指定して、任意の値をフィールドに割り当てることができます。問題の選択を含む

フォームは次のようになります。番目の値

<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