使用Thymeleaf窗体构建POST请求
我无法将窗体输入传递给嵌套对象的属性(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;
}
回答:
为什么你的代码是不工作:你有name="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
中指定的对象的选择表达式(*
)。
以上是 使用Thymeleaf窗体构建POST请求 的全部内容, 来源链接: utcz.com/qa/267144.html