Spring MVC uses the Put,delete method to implement the problem of the parameter transfer

Source: Internet
Author: User
This article is to share with you is Spring MVC use Put,delete method to implement the problem of the operation of the instructions, the content is very good, hope to help the needy friends

Recently in the previous project modification interface, using resutful, using the Delete method to pass the background has been received, consult related data found that Springmvc for the Delete method is not supported, need to add a filter in Web. xml

<filter><!--The filter is used to process post requests to convert to standard delete and put requests--><filter-name>hiddenhttpmethodfilter</ Filter-name><filter-class>org.springframework.web.filter.hiddenhttpmethodfilter</filter-class> </filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><!-- Servlet is Springmvc's servlet name--><servlet-name>springmvc</servlet-name></filter-mapping>

The relevant explanation for SPRINGMVC does not support Put,delete request, the core code of the filter is as follows

protected void dofilterinternal (HttpServletRequest request, httpservletresponse response, Filterchain Filterchain) Throws Servletexception, IOException {String paramvalue = Request.getparameter (This.methodparam), if ("POST". Equals ( Request.getmethod ()) && stringutils.haslength (paramvalue)) {String method = Paramvalue.touppercase ( Locale.english); HttpServletRequest wrapper = new Httpmethodrequestwrapper (request, method); Filterchain.dofilter (wrapper, response);} else {filterchain.dofilter (request, Response);}}

Convert the Post method to a standard put or delete method

The corresponding front-end access request is changed to

$.ajax ({type: "POST", URL: "Demo", DataType: "JSON", Async:false,data: {provinceids:id,//This parameter specifies the background accept method type, Put/delete_met Hod: "Delete",},success:function (data) {});

Background method

@RequestMapping (value = "/demo", method = Requestmethod.delete) @ResponseBodypublic Map Demo (HttpServletRequest request , HttpServletResponse Response,integer ID) {    return null;    }

It is important to note that only context-type:application/x-www-form-urlencoded requests are filtered.

Reference: https://blog.csdn.net/jslcylcy/article/details/52789575

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.