我无法将表单输入传递给嵌套对象的属性(object.subobject.property
)。具体来说,我有一个Institution institution
对象,其中包含一个Country country
对象,该对象具有一个名为countryName
的属性,可以像institution.country.getCountryName()
一样访问,但我无法正确填充Model
提交表格。下面我在@PostMapping
期间有三个打印语句。该输出看起来像:
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=
和null
对象中Model
的值institutionAttributes
是多少。我期待的更像是
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;
}
答案 0 :(得分: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"/>
它应该正确设置name
和value
属性(以及id
)。它通常可以避免您单独为这些属性指定(Thymeleaf)属性。
th:field
值必须是相对于*
中指定的对象的选择表达式(th:object
)。