我有两个对象,Contractor
,带有字段ContractorData
,ContractorData
有一个类型为 Invoice
的对象列表:
Contractor
id
email
ContractorData
ContractorData
id
FistName
LastName
...
List<InvoiceData>
对于承包商更新,我有一个基本控制器
@GetMapping("/")
public String index(Model model, OAuth2Authentication authentication) {
String email = String.valueOf(((LinkedHashMap<String, Object>) authentication.getUserAuthentication().getDetails()).get("email"));
Contractor contractor = contractorRepository.findByEmail(email);
if (contractor == null) {
contractor = new Contractor();
contractor.setEmail(email);
contractorRepository.save(contractor);
}
model.addAttribute("contractor", contractor);
return "index";
}
和更新控制器:
@RequestMapping(value = "/contractor/update/{id}", method = RequestMethod.POST, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String updateContractor(@PathVariable("id") String id, Contractor contractor, Model model) {
Contractor contractorPO = contractorRepository.findById(id).get();
ContractorData contractorData = ContractorData.builder()
.firstName(contractor.getContractorData().getFirstName())
.lastName(contractor.getContractorData().getLastName())
.businessName(contractor.getContractorData().getBusinessName())
.businessLocation(contractor.getContractorData().getBusinessLocation())
.nip(contractor.getContractorData().getNip())
.regon(contractor.getContractorData().getRegon())
.build();
contractorPO.setContractorData(contractorData);
contractorRepository.save(contractorPO);
model.addAttribute("contractor", contractorPO);
return "index";
}
在这里,一切都完美无缺。
但是,我有一个控制器,指向一个可以在其上添加InvoiceData
的页面:
@RequestMapping(value = "/add/{id}")
public String addPage(@PathVariable("id") String id, Model model) {
InvoiceData invoiceData = new InvoiceData();
model.addAttribute("contractorid", id);
model.addAttribute("invoicedata", invoiceData);
return "add";
}
以及用于添加发票的控制器:
@RequestMapping(value = "/addinvoice/{id}", method = RequestMethod.POST, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String addInvoice(@PathVariable("id") String id, InvoiceData data, Model model) {
Contractor contractor = contractorRepository.findById(id).get();
contractor.getInvoices().add(data);
contractorRepository.save(contractor);
model.addAttribute("contractor", contractor);
return "index";
}
在 HTML 表单端,索引表单.html如下所示:
<form action="#" th:action="@{/contractor/update/{id}(id=${contractor.id})}" th:object="${contractor}" method="post">
<ul class="form-style-1">
<li>
<label>First Name<span class="required">*</span></label>
<input type="text" th:field="*{contractorData.firstName}" id="firstName" th:value="${contractor.contractorData?.firstName}">
</li>
<li>
<label>Last Name<span class="required">*</span></label>
<input type="text" th:field="*{contractorData.lastName}" id="lastName" th:value="${contractor.contractorData?.lastName}">
</li>
... and more
<li>
<input type="submit" value="Submit" />
</li>
</ul>
</form>
add.html
的形式看起来是一样的,但在对象上有所不同:
<form action="#" th:action="@{addinvoice/{id}(id=${contractorid})}" th:object="${invoicedata}" method="post">
<ul class="form-style-1">
<li>
<label>Reception date<span class="required">*</span></label>
<input type="date" th:field="*{receptionDate}" id="receptionDate">
</li>
... and more
<li>
<input type="submit" value="Submit" />
</li>
</ul>
</form>
问题在于保存InvoiceData
。当我填写表单并单击提交时 - 我收到错误:
出现意外错误(类型=错误请求,状态=400(。对象='发票数据'的验证失败。错误计数:4
重要编辑
在控制台输出上,我也收到一个错误:
Field error in object 'invoiceData' on field 'contractDate': rejected value [2019-04-11]; codes [typeMismatch.invoiceData.contractDate,typeMismatch.contractDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [invoiceData.contractDate,contractDate]; arguments []; default message [contractDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'contractDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2019-04-11'; nested exception is java.lang.IllegalArgumentException]
但正如我之前所说,ContractorData
更新表单一切都很好......
这里的问题是java.date。因为在 HTML 中,日期的格式为"yyyy-MM-dd"。您收到错误,因为Spring 无法将此日期转换为 Java。日期。
你可能已经声明了这样的接待日期
Date receptionDate;
现在,更改为
@DateTimeFormat(pattern = "yyyy-MM-dd")
Date receptionDate;