在Spring MVC中使用PUT和DELETE方法

时间:2012-11-29 15:57:49

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

我正在尝试在Spring MVC控制器(版本3.0.2)中使用RequestMethod.PUTRequestMethod.DELETE。 Spring控制器类中有三个映射方法,如下所示(分别为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";
}

加载页面时,GET方法被调用为显而易见但在所有其他情况下(提交页面时),唯一要调用的方法是POST,指定的方法永远不会调用RequestMethod.PUT


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>

生成的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>

在我的spring-config.xml(我的情况为dispatcher-servlet.xml)中,我添加了对CommonsMultipartResolver的引用:

<bean id="filterMultipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

在我的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>

永远不会调用PUT(和DELETE)方法(没有异常或错误)。我在这里缺少什么?


更新:

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>

会引发以下异常。

  

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   名为'filterMultipartResolver'的bean已定义

名称dispatcher是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>

我不确定还需要什么?过滤器HiddenHttpMethodFilter是否需要在我们自己的OncePerRequestFilter上展示here之类的内容? (这是一个内置的课程)

列出了重点:here

2 个答案:

答案 0 :(得分:15)

大多数浏览器不支持HTML表单中的action=PUT。他们只会发送POST个请求。 HiddenHttpMethodFilter可以帮助您解决问题,但您必须在表单中包含隐藏字段_method=PUT。如果您使用spring:form taglib,这将自动为您完成,但您的示例似乎使用纯HTML。

NoSuchBeanDefinitionException很可能是一个无关的问题。

答案 1 :(得分:1)

您应该更改配置。

<servlet-name>/*</servlet-name>  

<servlet-name>[dispatch servlet name]</servlet-name>