在Spring MVC中使用PUT和DELETE方法
我正在尝试在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";
}
加载页面时,该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
(and DELETE too)方法永远不会调用(没有异常或错误)。我在这里想念什么?
更新:
通过以下配置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类似所示的位置?(这是一个内置的类)
回答:
大多数浏览器不支持action=PUT
HTML表单。他们只会发送POST请求。该HiddenHttpMethodFilter会帮助你解决问题,但你必须有一个隐藏字段_method=PUT
在你的形式。如果使用spring:form标签库,这将自动为你完成,但是你的示例似乎使用纯HTML。
这NoSuchBeanDefinitionException
很可能是一个不相关的问题。
以上是 在Spring MVC中使用PUT和DELETE方法 的全部内容, 来源链接: utcz.com/qa/415548.html