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

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

问题描述

我正在尝试在 Spring MVC 控制器(3.0.2 版)中使用 RequestMethod.PUTRequestMethod.DELETE.Spring 控制器类中有如下三种方法与 URL 映射(分别为 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)方法永远不会被调用(没有异常或错误).我在这里错过了什么?

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 需要我们自己实现扩展 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 标记库,这将自动为您完成,但您的示例似乎使用纯 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天全站免登陆