为 HttpDelete 编写 Spring MVC 控制器和 jsp
我试图通过删除链接(a href)或删除按钮(表单)删除页面上的实体。我正在使用删除按钮,因为链接需要“GET”而不是“POST”
这是打算执行此操作的 JSP 代码:
<td><form:form method="DELETE" action="/client/invoices/${invoice.id}"><input type="submit" value="delete"></form:form></td>
生成的 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>
所以,我很高兴。它有 _method 表明它是一个 DELETE 操作。这是我的控制器代码:
@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";
}
所以,发生的情况是这个方法没有被调用。我有另一种方法,它执行 POST 来创建发票,然后单击删除按钮来创建发票。我的猜测是,控制器将 servlet 视为 POST 请求,并使用第一个处理 POST 请求的方法,在本例中是创建新发票。
我尝试使其成为“RESTful”,所以我希望它是 /invoice/id
并使用 POST、PUT、DELETE、GET
但我不知道如何编码在使用 Spring MVC 的控制器中。
我可以通过附加“动词”(例如 /invoices/id/delete
)并将控制器设置为
@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
Note、RequestMethod.POST 来实现此功能,但由于映射值明确具有 /id/delete
,它不使用映射到 /invoices
和 /invoices/id
的默认 POST。
我希望我说清楚了。如果有人有任何建议或示例代码(强烈推荐),我将不胜感激。我已阅读这些 SO 链接以供参考: 链接1 链接2 链接3
I am trying to delete an entity on a page via a delete link (a href) or delete button (form). I am using delete button since a link calls for a "GET" instead of a "POST"
This is the JSP code which intends on doing that:
<td><form:form method="DELETE" action="/client/invoices/${invoice.id}"><input type="submit" value="delete"></form:form></td>
The resulting html is this:
<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 pretty happy. It has _method which indicates that it 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 is that this method is not called. I have another method which does a POST to create an invoice and clicking the delete button instead created an invoice. My guess is that the controller looks at the servlet as a POST request and uses the first method which handles a POST request which in this case is to create a new invoice.
I try to make this "RESTful" so I want this to be /invoice/id
and using POST, PUT, DELETE, GET
but I am not sure how to code that in the controller using Spring MVC.
I am able to get this to work by appending "verbs" such as /invoices/id/delete
and setting up the controller as
@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
Note, the RequestMethod.POST but since the map values explicitly have /id/delete
, it does not use the default POST which is mapped to /invoices
and /invoices/id
.
I hope I am clear. If anyone have any suggestions or sample code (strongly preffered), I would appreciate it. I've read these SO links for references:
Link1
Link2
Link3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否设置了 HiddenHttpMethodFilter 在你的 web.xml 中?该过滤器将发布的方法参数转换为 HTTP 方法,并允许在 Spring MVC 表单标签中支持方法转换。
Did you set the HiddenHttpMethodFilter in your web.xml? That filter converts posted method parameters into HTTP methods and allows support for method conversion in the Spring MVC form tags.
这是 Java Config 中的等效项(需要 Servlet API 3.0+)
Here is the equivalent in Java Config (requires Servlet API 3.0+)