在 Spring MVC 中使用 PUT 和 DELETE 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13629653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using PUT and DELETE methods in Spring MVC
提问by Tiny
I'm trying to use RequestMethod.PUTand RequestMethod.DELETEin Spring MVC controller (version 3.0.2). There are three methods mapped with a URL in the Spring controller class as follows (PUT, GET and POST respectively, for the demonstration purpose only).
我正在尝试在 Spring MVC 控制器(3.0.2 版)中使用RequestMethod.PUT和RequestMethod.DELETE。Spring 控制器类中有如下三种方法与 URL 映射(分别为 PUT、GET 和 POST,仅用于演示目的)。
@RequestMapping(method = {RequestMethod.PUT}, value = {"admin_side/Temp"}, headers = {"content-type=multipart/form-data"})
public String update(@ModelAttribute("tempBean") TempBean tempBean, BindingResult error, Map model, HttpServletRequest request, HttpServletResponse response) {
if (ServletFileUpload.isMultipartContent(request)) {
System.out.println("true");
}
System.out.println("Request method PUT");
return "admin_side/Temp";
}
@RequestMapping(method = {RequestMethod.GET}, value = {"admin_side/Temp"})
public String showForm(@ModelAttribute("tempBean") TempBean tempBean, BindingResult error, Map model, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Request method GET");
return "admin_side/Temp";
}
@RequestMapping(method = {RequestMethod.POST}, value = {"admin_side/Temp"})
public String onSubmit(@ModelAttribute("tempBean") TempBean tempBean, BindingResult error, Map model, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Request method POST");
return "admin_side/Temp";
}
When the page is loaded, the the GETmethod is invoked as obvious but in all other cases (when the page is submitted), the only method to be invoked is POST, the method designated with RequestMethod.PUTis never invoked.
当页面加载时,该GET方法被调用是显而易见的,但在所有其他情况下(当页面被提交时),唯一要调用POST的方法是,指定的方法RequestMethod.PUT永远不会被调用。
The Spring form contains only a submit button and an image browser as,
Spring 表单只包含一个提交按钮和一个图像浏览器,
<form:form id="mainForm"
name="mainForm"
method="PUT"
action="Temp.htm"
enctype="multipart/form-data"
commandName="tempBean">
<input type="file" id="myFile" name="myFile"/>
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"/>
</form:form>
The generated HTML is as follows,
生成的HTML如下,
<form id="mainForm"
name="mainForm"
action="Temp.htm"
method="post"
enctype="multipart/form-data">
<input type="hidden" name="_method" value="PUT"/>
<!--This hidden field is implicitly included-->
<input type="file" id="myFile" name="myFile"/>
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"/>
</form>
In my spring-config.xml(dispatcher-servlet.xmlin my case), I have added a reference to CommonsMultipartResolver:
在我的spring-config.xml(dispatcher-servlet.xml就我而言)中,我添加了对以下内容的引用CommonsMultipartResolver:
<bean id="filterMultipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
and in my web.xmlfile, HiddenHttpMethodFilteris configured as follows,
在我的web.xml文件中,HiddenHttpMethodFilter配置如下,
<filter>
<filter-name>MultipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
<init-param>
<param-name>multipartResolverBeanName</param-name>
<param-value>filterMultipartResolver</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MultipartFilter</filter-name>
<servlet-name>/*</servlet-name>
</filter-mapping>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<servlet-name>/*</servlet-name>
</filter-mapping>
The PUT(and DELETEtoo) method is never invoked (with no exception or error). What am I missing here?
的PUT(和DELETE太)方法永远不会调用(没有异常或错误)。我在这里缺少什么?
Update :
更新 :
With the following configuration in web.xml,
使用以下配置web.xml,
<filter>
<filter-name>MultipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
<init-param> <!-- Makes no difference, if excluded. -->
<param-name>multipartResolverBeanName</param-name>
<param-value>filterMultipartResolver</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MultipartFilter</filter-name>
<servlet-name>dispatcher</servlet-name> <!--Changed from /* to dispatcher-->
</filter-mapping>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<servlet-name>dispatcher</servlet-name> <!--Changed from /* to dispatcher-->
</filter-mapping>
it throws the following exception.
它抛出以下异常。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'filterMultipartResolver' is defined
org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有定义名为“filterMultipartResolver”的 bean
Where the name dispatcheris the name of the Servlet - org.springframework.web.servlet.DispatcherServletalready mapped in web.xml as follows.
其中 namedispatcher是 Servlet 的名称 -org.springframework.web.servlet.DispatcherServlet已在 web.xml 中映射如下。
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
I'm not sure what else is needed? Is the filter HiddenHttpMethodFilterrequired to implement on our own extending OncePerRequestFiltersomething like the one shown here? (it is a built-in class)
我不确定还需要什么?HiddenHttpMethodFilter我们自己实现的过滤器是否需要扩展OncePerRequestFilter类似于此处显示的内容?(它是一个内置类)
Important points are listed here.
采纳答案by Guillaume
Most browsers do not support action=PUTin HTML forms. They will just send POSTrequests instead. The HiddenHttpMethodFilterwill help you get around the problem, but you have to include a hidden field _method=PUTin your form. If you use the spring:form taglib this will be done automatically for you, but your example seems to use plain HTML.
大多数浏览器不支持action=PUTHTML 表单。他们只会发送POST请求。该HiddenHttpMethodFilter会帮助你解决问题,但你必须有一个隐藏字段_method=PUT在您的形式。如果您使用 spring:form taglib,这将自动为您完成,但您的示例似乎使用纯 HTML。
The NoSuchBeanDefinitionExceptionis most probably an unrelated problem.
这NoSuchBeanDefinitionException很可能是一个无关的问题。
回答by Hlex
You should change your config.
你应该改变你的配置。
<servlet-name>/*</servlet-name>
to
到
<servlet-name>[dispatch servlet name]</servlet-name>

