I am trying to delete an object on a page using a delete (href) or delete (form) link. I use the delete button because the link calls "GET" instead of "POST"
This is the JSP code that intends to do this:
<td><form:form method="DELETE" action="/client/invoices/${invoice.id}"><input type="submit" value="delete"></form:form></td>
The result is html:
<td><form id="command" action="/client/invoices/9" method="post"><input type="hidden" name="_method" value="DELETE"/><input type="submit" value="delete"></form></td>
So, I am very happy. It has a method that indicates that this is a DELETE action. Here is my controller code:
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) { invoiceServiceHibernate.removeInvoice(id); return "redirect:/invoices"; }
So what happens, this method is not called. I have another method that does POST to create an invoice, and clicking the delete button instead creates an invoice. I assume that the controller looks at the servlet as a POST request and uses the first method that processes the POST request, which in this case should create a new invoice.
I'm trying to make it "RESTful", so I want it to be /invoice/id and using POST, PUT, DELETE, GET , but I'm not sure how to encode this in the controller using Spring MVC.
I can make this work by adding "verbs" such as /invoices/id/delete and setting up the controller as
@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
Please note that RequestMethod.POST, but since the map values ββare explicitly /id/delete , it does not use the default POST, which maps to /invoices and /invoices/id .
I hope I get it. If anyone has any suggestions or code examples (highly recommended), I would appreciate it. I read these SO links for links: Link1 Link2 Link3