解決SpringMVC put,patch,delete請求數據拿不到的問題


解決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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM