我试图在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,但为什么我的控制器方法没有被调用?