2

我有一个 Spring MVC 网络应用程序。在其中一个带有按钮的表单应该从另一个资源中删除一个资源:

<td>
    <form action="/product-bases/${productBase.id}/positions/${position.id}" method="DELETE">
        <input type="submit" value="delete" />
    </form>
</td>

我的控制器:

@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) {

所以理论上服务器应该路由到控制器。但可惜它没有,因此帖子;)

我越来越

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 - bad request..)

那么我在这里错过了什么?

哦,只是为了偷工减料: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)

有和我一样困惑的人吗?如果不是这样,请赐教!

4

4 回答 4

6

表单可以通过GETPOST仅提交(也许也可以PUT,但我怀疑这是否被广泛实施),因为表单提交需要一种将数据传输到服务器的方法。

DELETE方法没有请求正文,因此不支持在表单操作中指定它。

于 2012-07-13T13:45:41.873 回答
2

您的 web.xml 中有 HiddenHttpMethodFilter 过滤器吗?

<filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/view.html#rest-method-conversion

于 2012-07-13T18:10:47.487 回答
1

错误消息表明浏览器实际上是在发送 GET 请求而不是 DELETE 请求。

你需要做的是:

  • 检查浏览器当时所在网页的来源,以及

  • 使用浏览器的 Web 调试器,查看请求 URL 和方法实际上是什么。

于 2012-07-13T13:46:11.453 回答
0

在 Microsoft Azure 门户中,当我打开身份验证/授权时,Java 应用程序出现此错误。如果您遇到同样的问题,请恢复您的操作。

于 2017-02-23T06:28:16.383 回答