Spring mvc uses put and delete methods to implement parameter passing problems

零到壹度
Release: 2023-03-23 06:12:02
Original
3213 people have browsed it

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>
Copy after login

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);
	}
}
Copy after login

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) {
			
	});
Copy after login

Background method

@RequestMapping(value = "/demo",method = RequestMethod.DELETE)
@ResponseBody
public Map demo(HttpServletRequest request, HttpServletResponse response,Integer id){
    return null;
    }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!