2017-12-27 31 views
1

フォーム入力をネストされたオブジェクト(object.subobject.property)の属性に渡す際に問題があります。特にInstitution institutionオブジェクトにはのようにアクセスされたcountryNameという属性を持つCountry countryオブジェクトが含まれていますが、フォーム提出時にはModelが正しく入力されません。以下、私は@PostMappingの間に3つの印刷ステートメントを持っています。その出力は次のようになります。country=の値は両方ModelinstitutionAttributesオブジェクトでnullあるかThymeleafフォームを使用してPOSTリクエストを作成する

Institution(instId=398, instName=Alfred Wegener Institute for Polar and Marine Research, abbrvName=AWI, country=null, action=finish institution edit) 
{modelinstitution=Institution(instId=398, instName=Alfred Wegener Institute for Polar and Marine Research, abbrvName=AWI, country=null, action=finish institution edit), org.springframework.validation.BindingResult.modelinstitution=org.springframework.validation.BeanPropertyBindingResult: 0 errors} 
org.springframework.validation.BeanPropertyBindingResult: 0 errors 

注意してください。フォームがCountryオブジェクトを移入し、どのように私はそれがそうすることができますされていないのはなぜ私がもっと

country=Country(countryName=United States, countryId=12, parentCountry=) 

のようなものを期待していましたか?

私の形式は次のとおりです。

コントローラからのPOSTリクエストを発行
<form id="lead_form" data-toggle="validator" th:action="@{/institutions}" th:object="${institution}" method="post" enctype="application/x-www-form-urlencoded"> 
    <input type="hidden" id="action" name="action" value="finish institution edit"/> 
    <input type="hidden" id="instId" name="instId" th:value="${institution.instId}"/> 

    <div class="row"> 
     <div class="col-md-2 text-left"> 
      <label for="instIdDisplay" class="control-label">Institution ID: </label> 
      <input class="form-control border-input" th:value="${institution.instId}" id="instIdDisplay" name="instIdDisplay" type="number" disabled="true"/> 
     </div> 
     <div class="col-md-5 text-left"> 
      <label for="instName" class="control-label">Institution Name: </label> 
      <input class="form-control border-input" th:value="${institution.instName}" placeholder="Required" id="instName" name="instName" type="text" required="true"/> 
     </div> 
     <div class="col-md-2 text-left"> 
      <label for="abbrvName" class="control-label">Abbreviation: </label> 
      <input class="form-control border-input" th:value="${institution.abbrvName}" placeholder="Optional" id="abbrvName" name="abbrvName" type="text"/> 
     </div> 
     <div class="col-md-3 text-left"> 
      <label for="countryName" class="control-label">Country: </label> 
      <input class="form-control border-input" th:value="${institution.country.countryName}" placeholder="Required" id="countryName" name="countryName" type="text"/> 
     </div> 
    </div> 
    <hr/> 
    <div class="row"> 
     <div class="col-md-4" align="left"> 
      <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Save &amp; Close</button> 
     </div> 
     <div class="col-md-4"/> 
     <div class="col-md-4" align="right"> 
     <!--<input id="form_button" class="btn btn-lg btn-success" type="submit" value="Save And Close"/>--> 
      <a th:href="@{/institutions}" class="btn btn-lg btn-danger">Cancel</a> 
     </div> 
    </div> 
</form> 

あなたのコードが動作しないのはなぜ
@PostMapping(value = "") 
String institutionsEditPOST(
     @ModelAttribute("modelinstitution") Institution institutionAttributes, 
     Model model, 
     BindingResult result) { 
    System.out.println(institutionAttributes); 
    System.out.println(model); 
    System.out.println(result); 
    String newAction; 
    String action = institutionAttributes.getAction(); 

    System.out.println("ENTER INSTITUTION POST BEFORE ACTION: "+action); 

    String instName = institutionAttributes.getInstName(); 
    String abbrvName = institutionAttributes.getAbbrvName(); 
    String countryName = institutionAttributes.getCountry().getCountryName(); 

    model.addAttribute("instName", instName); 
    model.addAttribute("abbrvName", abbrvName); 
    model.addAttribute("countryName", countryName); 

    newAction = "institutions"; 

    return "forms/fragments/"+newAction; 
} 

答えて

1

:あなたはname="countryName"持っているが、COUNTRYNAMEがのプロパティではありません機関、しかし国の。あなたはプロパティへのパスを提供する必要があります:name="country.countryName"。しかし、代わりに便利なth:fieldを使用することをお勧めします。

はこれを試してみてください:

<input class="form-control border-input" th:field="*{country.countryName}" placeholder="Required" id="countryName" type="text"/> 

それが適切namevalue属性(ともid)の両方を設定する必要があります。通常、これらの属性を個別に(Thymeleaf)指定することからあなたを保護します。

の値は、th:objectで指定されたオブジェクトに対する選択式(*)でなければなりません。

+0

クール豆。それはうまくいくようです。 'th:field'と' th:value'のさまざまな振る舞いを説明する答えを広げてください。また、 'th:id'、' th:value'、 'th:name'を' th:field'だけで置き換えることができるところもあります。本当ですか? – medley56

+0

@ medley56はい、「th:field」は3対1の取引です。拡張された答えは少しです。 – holmis83

関連する問題