2017-12-27 119 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