我無法將窗體輸入傳遞給嵌套對象的屬性(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=
值是如何在Model
和institutionAttributes
對象即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 & 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;
}
酷豆。這似乎工作。你能擴展你的答案來描述'th:field'和'th:value'的不同行爲嗎?另外,我在某處讀到'th:id','th:value','th:name'可以代替'th:field'。真正? – medley56
@ medley56是的,'th:field'幾乎是一個3合1的交易。擴大了答案。 – holmis83