(转)SpringMvc如何使用form发送PUT和DELETE请求
Posted PacosonSWJTU
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(转)SpringMvc如何使用form发送PUT和DELETE请求相关的知识,希望对你有一定的参考价值。
一、当前现状
浏览器使用form提交信息的时候只支持GET和POST,如果需要在浏览器上使用PUT和DELETE请求方式的话,只能使用欺骗的方式了,SpringMvc提供了HiddenHttpMethodFilter类来提供支持,请看代码:
public class HiddenHttpMethodFilter extends OncePerRequestFilter {
/** Default method parameter: {@code _method} */
//我们的隐藏字段name必须为_method
public static final String DEFAULT_METHOD_PARAM = "_method";
private String methodParam = DEFAULT_METHOD_PARAM;
/**
* Set the parameter name to look for HTTP methods.
* @see #DEFAULT_METHOD_PARAM
*/
public void setMethodParam(String methodParam) {
Assert.hasText(methodParam, "'methodParam' must not be empty");
this.methodParam = methodParam;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
HttpServletRequest requestToUse = request;
if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
String paramValue = request.getParameter(this.methodParam);
if (StringUtils.hasLength(paramValue)) {
requestToUse = new HttpMethodRequestWrapper(request, paramValue);
}
}
filterChain.doFilter(requestToUse, response);
}
/**
* Simple {@link HttpServletRequest} wrapper that returns the supplied method for
* {@link HttpServletRequest#getMethod()}.
*/
private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
private final String method;
public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
super(request);
this.method = method.toUpperCase(Locale.ENGLISH);
}
//通过继承方式对getMethod方法做了下改变,就变成了PUT或者DELETE了
@Override
public String getMethod() {
return this.method;
}
}
}
二、配置步骤
1. web.xml
<!-- HTTP PUT Form -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2. putform.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="POST" action="<%=request.getServletContext().getContextPath()%>/home/putformBody">
<input type="hidden" name="_method" value="PUT">
<p>姓名:</p><input type="text" name="name" /><br/>
<p>性别:</p><input type="text" name="sex" /><br/>
<p>年龄:</p><input type="text" name="age" /><br/>
<button type="submit">提交</button>
</form>
</body>
</html>
3. putformBody(controller方法)
@RequestMapping(path="home/putformBody", method=RequestMethod.PUT, produces = "text/plain;charset=utf-8")
@ResponseBody
public String putformBody(HttpServletRequest req, HttpServletResponse resp) {
String name = req.getParameter("name");
String sex = req.getParameter("sex");
String age = req.getParameter("age");
return "name:" + name + ",sex:" + sex + ",age:" + age;
}
四、中间遇到的坑
1、拦截器的url-pattern必须配置为/*,不能配置/,否则不生效;
2、为对中文支持避免乱码配置CharacterEncodingFilter必须为放在第一个,否则即使是配置生效(断点调试能进去),但是依然中文乱码,初学的朋友参考web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<description>cockroach-springmvc-xml</description>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- UTF-8 encoding -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- HTTP PUT Form -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3、SpringMvc以@ResponseBody返回的时中文乱码,demo学习的话可以配置下@RequestMapping如下:
@RequestMapping(path="home/putformBody", method=RequestMethod.PUT, produces = "text/plain;charset=utf-8")
五 参考连接
以上是关于(转)SpringMvc如何使用form发送PUT和DELETE请求的主要内容,如果未能解决你的问题,请参考以下文章
使用 PUT 请求发送 multipart/form-data 在 Laravel 中不起作用
(转)获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8