在Spring MVC中使用PUT和DELETE方法 [英] Using PUT and DELETE methods in Spring MVC

查看:149
本文介绍了在Spring MVC中使用PUT和DELETE方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Spring MVC控制器(3.0版)中使用 RequestMethod.PUT RequestMethod.DELETE 。 2)。 Spring控制器类中有三个映射方法,如下所示(分别为PUT,GET和POST,仅用于演示目的)。

I'm trying to use RequestMethod.PUT and RequestMethod.DELETE in 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).

@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

When the page is loaded, the the GET method 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.PUT is never invoked.

Spring表单包含只有提交按钮和图像浏览器,

The Spring form contains only a submit button and an image browser as,

<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

In my spring-config.xml (dispatcher-servlet.xml in my case), I have added a reference to CommonsMultipartResolver:

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

和我的 web.xml 文件, HiddenHttpMethodFilter 配置如下,

and in my web.xml file, HiddenHttpMethodFilter is configured as follows,

<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 too)方法永远不会被调用(没有异常或错误)。我在这里缺少什么?

The PUT (and DELETE too) method is never invoked (with no exception or error). What am I missing here?

更新:

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

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'filterMultipartResolver' is defined

其中名称 dispatcher 是Servlet的名称 - org.springframework.web.servlet.DispatcherServlet 已经在web.xml中映射如下。

Where the name dispatcher is the name of the Servlet - org.springframework.web.servlet.DispatcherServlet already mapped in web.xml as follows.

<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在我们自己的扩展 /filter/OncePerRequestFilter.html> OncePerRequestFilter 显示的内容此处? (它是一个内置类)

I'm not sure what else is needed? Is the filter HiddenHttpMethodFilter required to implement on our own extending OncePerRequestFilter something like the one shown here? (it is a built-in class)

列出了重要的要点其他

推荐答案

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

Most browsers do not support action=PUT in HTML forms. They will just send POST requests instead. The HiddenHttpMethodFilter will help you get around the problem, but you have to include a hidden field _method=PUT in your form. If you use the spring:form taglib this will be done automatically for you, but your example seems to use plain HTML.

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

The NoSuchBeanDefinitionException is most probably an unrelated problem.

这篇关于在Spring MVC中使用PUT和DELETE方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆