Java – programming spring MVC controller and JSP for httpdelete
I tried to delete an entity on a page by deleting a link (a href) or a delete button (form) I'm using the delete button because the link requires "get" instead of "post"
This is the JSP code to do this: @ h_ 419_ 3@
<td><form:form method="DELETE" action="/client/invoices/${invoice.id}"><input type="submit" value="delete"></form:form></td>
The generated HTML is like this: @ h_ 419_ 3@
<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'm very happy It has_ Method indicates that it is a delete operation This is my controller code: @ h_ 419_ 3@
@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"; }
Then, what happens is that this method is not called I have another method, which performs post to create an invoice and clicks the delete button instead of creating an invoice My guess is that the controller treats the servlet as a post request and uses the first method of processing the post request to create a new invoice in this case@ H_ 419_ 3@
I try to make this "restful", so I want this to be / invoice / ID and use post, put, delete, get, but I don't know how to write code in the controller using spring MVC@ H_ 419_ 3@
I can set the controller to @ h by attaching "verbs" such as / verpoints / ID / delete_ 419_ 3@
@RequestMapping(value = "/{id}/delete",method = RequestMethod.POST)
Notice that requestmethod Post, but since the mapping value explicitly has / ID / delete, it does not use the default post mapped to / invoices and / invoices / ID@ H_ 419_ 3@
I hope I know I would appreciate any suggestions or sample code (strongly preferred) I have read these so links for reference: link1 link2 Link3@H_419_3 @
Solution
You're on the web Is hiddenhttpmethodfilter set in XML? The filter converts published method parameters to HTTP methods and allows method conversion in spring MVC form tags
<filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <servlet-name>servletName</servlet-name> </filter-mapping>