當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
(转)【SpringMvc】如何使用form发送PUT和DELETE请求
生活随笔
收集整理的這篇文章主要介紹了
(转)【SpringMvc】如何使用form发送PUT和DELETE请求
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
轉(zhuǎn)自: ? https://blog.csdn.net/cockroach02/article/details/82194126https://blog.csdn.net/cockroach02/article/details/82194126
一、當(dāng)前現(xiàn)狀
瀏覽器使用form提交信息的時(shí)候只支持GET和POST,如果需要在瀏覽器上使用PUT和DELETE請(qǐng)求方式的話,只能使用欺騙的方式了,SpringMvc提供了HiddenHttpMethodFilter類來提供支持,請(qǐng)看代碼:
public class HiddenHttpMethodFilter extends OncePerRequestFilter {/** Default method parameter: {@code _method} *///我們的隱藏字段name必須為_methodpublic 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;}@Overrideprotected 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);}//通過繼承方式對(duì)getMethod方法做了下改變,就變成了PUT或者DELETE了@Overridepublic 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")@ResponseBodypublic 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、為對(duì)中文支持避免亂碼配置CharacterEncodingFilter必須為放在第一個(gè),否則即使是配置生效(斷點(diǎn)調(diào)試能進(jìn)去),但是依然中文亂碼,初學(xué)的朋友參考web.xml如下:
3、SpringMvc以@ResponseBody返回的時(shí)中文亂碼,demo學(xué)習(xí)的話可以配置下@RequestMapping如下:
@RequestMapping(path="home/putformBody", method=RequestMethod.PUT, produces = "text/plain;charset=utf-8")五 參考連接
總結(jié)
以上是生活随笔為你收集整理的(转)【SpringMvc】如何使用form发送PUT和DELETE请求的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 看完佳能的这场影展,又让我有了打印照片的
- 下一篇: 与亲妈聊天时无话可说和爸妈没话聊