2017-12-27 1084 views
1

我無法將窗體輸入傳遞給嵌套對象的屬性(object.subobject.property)。具體而言,我有一個Institution institution對象,其中包含一個Country country對象,該對象具有一個名爲countryName的屬性,訪問類似institution.country.getCountryName(),但我無法在提交表單時正確填充Model。下面我在@PostMapping期間有三個打印語句。該輸出看起來像:使用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=值是如何在ModelinstitutionAttributes對象即null。我期待更多的東西一樣

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

爲什麼不表格填充Country對象,我怎麼可以把它做呢?

我有一個表格:

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

哪些問題從控制器的POST請求:

@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"但國家名稱是不是財產機構,但國家。您必須提供酒店的路徑: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:field值必須是相對於th:object中指定的對象的選擇表達式(*)。

+0

酷豆。這似乎工作。你能擴展你的答案來描述'th:field'和'th:value'的不同行爲嗎?另外,我在某處讀到'th:id','th:value','th:name'可以代替'th:field'。真正? – medley56

+0

@ medley56是的,'th:field'幾乎是一個3合1的交易。擴大了答案。 – holmis83