Springmvc POST request to DELETE or put request configuration Hiddenhttpmethodfilter

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.