解決SpringMVC put,patch,delete請求參數拿不到的問題
廢話不多說,核心代碼如下:
在web.xml中添加如下代碼
<!-- 解決web端不能put,delete等請求的問題 --> <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>springMVC</servlet-name> </filter-mapping>
<!-- 解決put,patch等請求,data數據拿不到的問題 --> <filter> <filter-name>HttpPutFormContentFilter </filter-name> <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class> </filter> <filter-mapping> <filter-name>HttpPutFormContentFilter </filter-name> <servlet-name>springMVC</servlet-name> </filter-mapping>
我的環境是Spring4.3;
第一個filter是解決 jQuery不能put,patch等請求,但是我測試用的 jquery-3.1.1.js 是可以put,patch請求的;可能是前端用的js版本低了吧,我也沒研究;
但是put,patch等請求,data數據后台拿不到的問題,但是拼接到URL里面就可以拿到;第二個filter就解決這個問題;
順便記錄一個通過Spring解決跨域訪問的問題
在spring-mvc.xml中添加如下代碼:
<!-- 添加跨域訪問 --> <mvc:cors> <mvc:mapping path="/**" allowed-origins="*" allowed-methods="*" allowed-headers="*" allow-credentials="false" max-age="3600" /> </mvc:cors>
參考:https://blog.csdn.net/edison_03/article/details/76150906
https://blog.csdn.net/lankezhou/article/details/72491019