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

猜你喜欢

转载自www.cnblogs.com/jr1260/p/9152275.html