2017-12-27 10 views
1

양식 입력을 중첩 오브젝트 (object.subobject.property)의 속성으로 전달하는 데 문제가 있습니다. 특히 Institution institution 개체에 institution.country.getCountryName()처럼 액세스되는 countryName이라는 특성을 가진 개체가 포함되어 있지만 양식 제출시 Model을 올바르게 채울 수 없습니다. 아래에는 @PostMapping 중에 세 개의 인쇄 문이 있습니다. 그 출력은 보이는 같은 : 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"/> 

제대로 (도 id을하고) 모두 namevalue 속성을 설정해야합니다

이 시도해보십시오. 일반적으로 이것들에 대해 (Thymeleaf) 속성을 지정하지 않아도됩니다.

값은 th:object에 지정된 개체에 대한 선택 식 (*)이어야합니다.

+0

멋진 콩. 그게 효과가있는 것처럼 보입니다. 'th : field'와'th : value'의 다른 행동을 설명하기 위해 답을 넓힐 수 있습니까? 또한'th : id','th : value','th : name' 대신'th : field'를 사용할 수 있다고 읽었습니다. 참된? – medley56

+0

@ medley56 예, 'th : field'는 3 대 1 거래입니다. 확장 대답 조금. – holmis83