我有一个 Spring MVC 网络应用程序。在其中有一个带有按钮的表单,该按钮应该从另一个资源中删除一个资源:
<td>
<form action="/product-bases/${productBase.id}/positions/${position.id}" method="DELETE">
<input type="submit" value="delete" />
</form>
</td>
我的 Controller :
@Controller
@RequestMapping(value = "/product-bases/{id}/positions")
public class ProductBasePositionController {
@RequestMapping(value = "/{positionId}", method = RequestMethod.DELETE)
public ModelAndView delete(@PathVariable Integer productBaseId, @PathVariable Integer positionId) {
所以理论上服务器应该路由到 Controller 。但遗憾的是它没有,因此发布了这篇文章 ;)
我得到了
HTTP Status 405 - Request method 'GET' not supported
type Status report
message Request method 'GET' not supported
description The specified HTTP method is not allowed for the requested resource (Request method 'GET' not supported).
Apache Tomcat/7.0.19
显然我还没有定义/positions/id 的 get,但我为什么要这样做,我现在想删除..
(我也试图从我的 spring-test-mvc 框架在模拟 servlet 上运行它,中间没有任何 tomcat 实现,它给了我一个 400 - 错误的请求.. )
那么我在这里缺少什么?
哦,只是为了走捷径:post 和 get 将适用于其他资源,所以我的其余设置没问题。
启动服务器甚至告诉我:
RequestMappingHandlerMapping [INFO] Mapped "{[/product-bases/{id}/positions/{positionId}],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView our.view.controller.ProductBasePositionController.delete(java.lang.Integer,java.lang.Integer)
有和我一样困惑的吗?如果不是这样,请赐教!
最佳答案
表单只能通过GET
或POST
提交(也可能是PUT
,但我怀疑它是否被广泛实现),作为表单提交需要一种将数据传输到服务器的方法。
DELETE
方法没有请求主体,因此不支持在表单操作中指定它。
关于java - 调用时不支持获取 "405 - Request method ' GET'method=DELETE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11471604/