日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

(转)【SpringMvc】如何使用form发送PUT和DELETE请求

發(fā)布時(shí)間:2023/12/3 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转)【SpringMvc】如何使用form发送PUT和DELETE请求 小編覺(jué)得挺不錯(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類來(lái)提供支持,請(qǐng)看代碼:

public class HiddenHttpMethodFilter extends OncePerRequestFilter {/** Default method parameter: {@code _method} *///我們的隱藏字段name必須為_(kāi)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);}//通過(guò)繼承方式對(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如下:

<?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返回的時(shí)中文亂碼,demo學(xué)習(xí)的話可以配置下@RequestMapping如下:

@RequestMapping(path="home/putformBody", method=RequestMethod.PUT, produces = "text/plain;charset=utf-8")

五 參考連接

  • 如何發(fā)送PUT請(qǐng)求和DELETE請(qǐng)求
  • html 對(duì) form 表單中 put,delete,patch的支持
  • HTTP PUT請(qǐng)求時(shí),表單數(shù)據(jù)無(wú)法傳遞
  • springmvc 明明到處都配置了編碼為UTF-8,可還是亂碼!!!
  • 總結(jié)

    以上是生活随笔為你收集整理的(转)【SpringMvc】如何使用form发送PUT和DELETE请求的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。