Configuration in 1.web.xml
<!--
Configuration Org.springframework.web.filter.HiddenHttpMethodFilter: Can convert a POST request to a DELETE or put request
-
<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. Control Layer Code:
/**
* Rest-style URLs. Take CRUD For example: add:/order POST modification:/ORDER/1 PUT update?id=1 get:
*/ORDER/1 GET get?id=1 Delete:/ORDER/1 Delete delete?id=1
*
* How do I send a PUT request and DELETE request? 1. You need to configure Hiddenhttpmethodfilter 2. Need to send a POST request
* 3. Need to carry a hidden field name= "_method" when sending a POST request with a value of DELETE or PUT
*
* How to get the ID in the target method of Springmvc? Using @PathVariable annotations
*
*/
@RequestMapping (value = "/testrest/{id}", method = Requestmethod.put)
Public String testrestput (@PathVariable Integer ID) {
System.out.println ("Testrest Put:" + ID);
return SUCCESS;
}
@RequestMapping (value = "/testrest/{id}", method = Requestmethod.delete)
Public String testrestdelete (@PathVariable Integer ID) {
System.out.println ("Testrest Delete:" + ID);
return SUCCESS;
}
3.jsp page
<form action= "SPRINGMVC/TESTREST/1" method= "POST" >
<input type= "hidden" name= "_method" value= "PUT"/>
<input type= "Submit" value= "Testrest PUT"/>
</form>
<br><br>
<form action= "SPRINGMVC/TESTREST/1" method= "POST" >
<input type= "hidden" name= "_method" value= "DELETE"/>
<input type= "Submit" value= "Testrest DELETE"/>
</form>
Springmvc POST request to DELETE or put request configuration Hiddenhttpmethodfilter