Spring PUT和DELETE:不支持请求方法

时间:2016-01-22 11:22:20

标签: spring spring-mvc httprequest http-delete http-put

我试图在Spring MVC控制器中使用RequestMethod.PUT和RequestMethod.DELETE。我的控制器有一个用于PUT和DELETE的@RequestMapping,但尽管如此,我在尝试使用它时遇到错误:

Request method 'GET'/'POST' not supported

控制器:

@RequestMapping(value = "/admin/user/{id}", method = RequestMethod.PUT)
public ModelAndView updateUser(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("user") User user, @PathVariable long id) {
    ...
}

@RequestMapping(value = "/admin/user/delete/{id}", method = RequestMethod.DELETE)
public @ResponseBody ModelAndView deleteUser(HttpServletRequest request, HttpServletResponse response, @PathVariable long id) {
    ...
}

我在web.xml中添加了过滤器:

<filter>
    <filter-name>httpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>httpMethodFilter</filter-name>
    <servlet-name>springDispatcher</servlet-name>
</filter-mapping>

PUT的弹簧形式:

<form:form id="registerForm" method="put" action="${user.id}" modelAttribute="user" class="form-horizontal">

生成的HTML:

<form id="registerForm" class="form-horizontal" action="13" method="post"><input type="hidden" name="_method" value="put"/>

我知道,如果浏览器不支持PUT / DELETE,则使用POST / GET,但为什么我的控制器方法没有被调用?

0 个答案:

没有答案