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