I have a Spring Hibernate MVC and trying to implement CRUD
operations. I have gotten create, get, delete in a "RESTful
" kinda way but I am having difficulties with update.
在试图更新物体时,参数是/id? 表格是控制器法:
@RequestMapping(value = "/{id}", params = "form", method = RequestMethod.GET)
public String updateForm(@PathVariable("id") Long id, Model uiModel) {
uiModel.addAttribute("invoice", invoiceServiceHibernate.getInvoice(id));
return "invoices/update";
}
因此,更新的JSP表格使用该表格填充了该物体。 The JSP Update form of the following:
<c:if test="${!empty invoice}">
<form:form method="PUT" commandName="invoice">
<table>
<tr>
<td>Amount</td>
<td><form:input value="${invoice.amount}" path="amount" /></td>
</tr>
... more labels and fields ...
<tr>
<td colspan="3">
<input type="submit" value="Update Invoice" />
</td>
</tr>
</table>
</form:form>
提交时,要求采用的控制器方法应为:
@RequestMapping(method = RequestMethod.PUT)
public String update(Invoice invoice, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
if (bindingResult.hasErrors()) {
uiModel.addAttribute("invoice", invoice);
return "invoices/update";
}
uiModel.asMap().clear();
invoiceServiceHibernate.updateInvoice(invoice);
return "redirect:/invoices/";
}
我已尝试了<代码>update、saveOrUpdate
和,>mer
invoiceserviceHibernate.updateInvoice(invoice)
,但并不奏效。
控制员最后开立了新的发票,而不是更新旧的发票。 为什么如此? 我读到。 连接并会谈使用@ModelAttribute。 我对它进行了审判,但它仍然不可行。 使用@ModelAttribute或@Valid没有。 这笔钱并不产生新的发票,但它没有更新目前的发票。
任何建议?
感谢!