Spring MVC的RESTful风格请求

在HTTP中,请求方式有4中,分别对应增、删、改、查。

新增: POST
修改: PUT
查询: GET

删除: DELETE

在JSP页面,它的请求方式只有两种,GET和POST,那么如何创建PUT和DELETE的请求方式呢?

1、需要在web.xml文件配置中配置org.springframework.web.filter.HiddenHttpMethodFilter

<!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter,作用:可以把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>

2、需要发送POST请求,并且请求时需要携带一个name="_method"的隐藏域,value为DELETE 或 PUT

<form action="hello1">
	<input type="submit" value="Test POST"/>
</form>
<form action="hello2">
	<input type="hidden" name="_method" value="PUT">
	<input type="submit" value="Test POST"/>
</form>
<form action="hello3">
	<input type="hidden" name="_method" value="DELETE">
	<input type="submit" value="Test DELETE"/>
</form>

hello1:是POST请求

hello2:是PUT请求

hello3:是DELETE请求

猜你喜欢

转载自blog.csdn.net/java_xuetu/article/details/80041766