Hiddenhttpmethodfilter request filtering for 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 delet E?id=1,

The browser only supports post and get, you want to implement the delete and put methods, you need to use a filter hiddenhttpmethodfilter

1, configuring the filter
 <Filter>        <Filter-name>Hidden</Filter-name>        <Filter-class>Org.springframework.web.filter.HiddenHttpMethodFilter</Filter-class>    </Filter>    <filter-mapping>        <Filter-name>Hidden</Filter-name>        <Url-pattern>/*</Url-pattern>    </filter-mapping>

2, the client initiates the request

The filter uses this parameter of _method to determine what type of filter it is, so you need to add _method's hidden field to the front-end submission form, paying attention to using the Post method for submission

 <formAction= "/REST/12"Method= "POST">    <inputtype= "hidden"name= "_method"value= "DELETE">    <inputtype= "Submit"value= "Delete">  </form>  <formAction= "/REST/12"Method= "POST">    <inputtype= "hidden"name= "_method"value= "PUT">    <inputtype= "Submit"value= "Put">  </form>  <formAction= "/REST/12"Method= "POST">    <inputtype= "Submit"value= "POST">  </form>  <formAction= "/REST/12"Method= "Get">    <inputtype= "Submit"value= "Get">  </form>

3, the writing of the back-end controller

The controller uses requestmapping directly to specify the method.

@RequestMapping (value = "/rest/{id}", method =requestmethod.delete) PublicString Testrestdelete (@PathVariableintID, model model) {Model.addattribute ("MSG", "Delete request" +ID); returnSUCCESS; } @RequestMapping (Value= "/rest/{id}", method =requestmethod.put) PublicString Testrestput (@PathVariableintId,model Model) {Model.addattribute ("MSG", "Put request" +ID); returnSUCCESS; } @RequestMapping (Value= "/rest/{id}", method =requestmethod.post) PublicString Testrestpost (@PathVariableintId,model Model) {Model.addattribute ("MSG", "POST request" +ID); returnSUCCESS; } @RequestMapping (Value= "/rest/{id}", method =requestmethod.get) PublicString Testrestdelete (@PathVariableintID, Modelmap modelmap) {Modelmap.addattribute ("MSG", "Get Request" +ID); returnSUCCESS; }

Special attention!!!!!!!!!!!!!

Delete and post requests are not supported on TOMCAT8, so the above can only be performed on TOMCAT7

The TOMCAT8 runtime can enter the appropriate controller, but when the view rendering is returned, the exception page is reported because the two methods are not supported

Hiddenhttpmethodfilter request filtering for 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.