一、序章
根据项目需要使用的是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>