2013-03-05 7 views
5

内のエンティティとタグを「選択」の使用:Spring Data JPA私はこのようになりますselectタグでフォームを作成していThymeleaf

<form th:object="${version}" method="post" class="form-horizontal"> 
    ... 
    <div class="control-group" th:classappend="${#fields.hasErrors('product')} ? 'error'"> 
     <label class="control-label" for="product" th:text="#{version.product}">Product</label> 
     <div class="controls"> 
      <select id="product" th:field="*{product}"> 
       <option value="" th:text="#{common.select.prompt}"></option> 
       <option th:each="p : ${productList}" th:value="${p.id}" th:text="${p.name}"></option> 
      </select> 
      <span class="help-inline" th:errors="*{product}"></span> 
     </div> 
    </div> 
    ... 
</form> 

DomainClassConverterクラスI実体Productidを選択した自動変換するのに役立ちますフォームを提出してください。 productも私はVersionクラスにproductフィールド上@NotNullを使用しています(NOT NULLでなければなりません

私が持っている問題 - 。私はデータを編集するために戻ってきたときに、Productが選択されていない

<-- p.s. is not a sad smileその後、

  <select id="product" th:field="*{product.id}"> 
       <option value="" th:text="#{common.select.prompt}"></option> 
       <option th:each="p : ${productList}" th:value="${p.id}" th:text="${p.name}"></option> 
      </select> 
      <span class="help-inline" th:errors="*{product.id}"></span> 

それは私がそれを編集するために戻ってきたときに選択した状態になるが、バリデータは動作しません(0123:私はこのようなselectth:fieldth:errors)を変更した場合選択されたIDがnullであっても、は常にインスタンス化されます。

非常に一般的なシナリオ(リストからエンティティを選択)のように見えますが、見栄えの良い例は見つかりません。秘密の知識を共有してください。

+0

編集に戻るとどういう意味ですか? –

+0

私はバージョンエンティティの作成と編集に同じページを使用します –

答えて

2

解決済み。問題は、equals()hashCode()のメソッドをオーバーライドしていないために存在しました。

関連する問題