springboot发送PUT或DELETE请求

一、序章
根据项目需要使用的是Restful风格,但是浏览器使用form提交信息的时候只支持GET和POST。

二、实现
1、SpringBoot
因为SpringBoot是SpringBoot自动配置好HiddenHttpMethodFilter 的,不需要像SpringMvc一样配置。

直接在form标签下添加以下input标签即可,value值就是指定的请求方式。

<input type="hidden" name="_method" value="put"/>

2、SpringMvc (也整理一下SpringMvc供参考,没有具体操作过)

在web.xml中配置过滤器,如下:

<!-- 使用Rest风格的URI,将页面普通的post请求转为指定的delete或put请求-->
    <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>
    <filter>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

在表单中加入一个隐藏的参数: _method , 值是 DELETE 或者 PUT。如下:

  <form action="test" method="post">
        <input type="hidden"  name="_method" value="PUT"></input>
        <input type="submit" value="Test"></input>
   </form>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值