Home > Article > Backend Development > Spring mvc uses put and delete methods to implement parameter passing problems
This article shares with you a detailed explanation of how spring mvc uses the put and delete methods to implement parameter passing problems. The content is quite good. I hope it can help friends in need
Recently, I modified the interface in a previous project, using resutful, and using the delete method to pass parameters, but the background has not been received. After checking the relevant information, I found that springmvc does not support the delete method, and a new filter needs to be added to web.xml
<filter> <!--该过滤器用于处理post请求转换为标准的delete与put请求 --> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <!--servlet为springMvc的servlet名 --> <servlet-name>springMVC</servlet-name> </filter-mapping>
The relevant explanation is that springmvc does not support put and delete request parameters. 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 the 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, //该参数指定后台接受方法类型,put/delete _method : "delete", }, success : function(data) { });
Background method
@RequestMapping(value = "/demo",method = RequestMethod.DELETE) @ResponseBody public Map demo(HttpServletRequest request, HttpServletResponse response,Integer id){ return null; }
It should be noted that only context-type: application/x-www -form-urlencoded requests will be filtered.
Reference: https://blog.csdn.net/jslcylcy/article/details/52789575
The above is the detailed content of Spring mvc uses put and delete methods to implement parameter passing problems. For more information, please follow other related articles on the PHP Chinese website!