Spring MVC uses Hiddenhttpmethodfilter to configure restful URLs

Source: Internet
Author: User

/** Rest-style URLs. Take CRUD For example: add:/order POST Modify:/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 a delete request?1. Need to configure Hiddenhttpmethodfilter in the Web. xml File<!--configuration Org.springframework.web.filter.HiddenHttpMethodFilter: You can convert a POST request to a DELETE or POST request--&gt        ; <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org .springframework.web.filter.hiddenhttpmethodfilter</filter-class> </filter> <filter-m Apping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</u Rl-pattern> </filter-mapping>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 PUTThe JSP files are as follows: <form action= "SPRINGMVC/TESTREST/1" method= "POST" > <input type= "hidden" name= "_method" Val            Ue= "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> <br><br > <form action= "springmvc/testrest" method= "POST" > <input type= "Submit" value= "Testr EST POST "/> </form> <br><br> <a href=" SPRINGMVC/TESTREST/1 ">test R EST get</a> <br><br>How do I get the ID in the target method of SPRINGMVC? Use @PathVariable annotations*/@RequestMapping (Value="/testrest/{id}", method =requestmethod.put) PublicString testrestput (@PathVariable Integer id) {System. out. println ("testrest Put:"+ID); returnSUCCESS; } @RequestMapping (Value="/testrest/{id}", method =requestmethod.delete) PublicString testrestdelete (@PathVariable Integer id) {System. out. println ("testrest Delete:"+ID); returnSUCCESS; } @RequestMapping (Value="/testrest", method =requestmethod.post) PublicString testrest () {System. out. println ("testrest POST"); returnSUCCESS; } @RequestMapping (Value="/testrest/{id}", method =requestmethod.get) PublicString testrest (@PathVariable Integer id) {System. out. println ("testrest GET:"+ID); returnSUCCESS; }    /** * @PathVariable can be used to map placeholders in the URL to the parameters of the target method. * @param ID * @return*/@RequestMapping ("/testpathvariable/{id}")     PublicString testpathvariable (@PathVariable ("ID") (Integer ID) {System. out. println ("testpathvariable:"+ID); returnSUCCESS; } @RequestMapping ("/TESTANTPATH/*/ABC")     PublicString Testantpath () {System. out. println ("Testantpath"); returnSUCCESS; }

Spring MVC uses Hiddenhttpmethodfilter to configure restful URLs

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.