在JQuery中,我们可以进行REST ful中delete和put的请求,但是在java EE标准中,默认只有在POST请求的时候,servlet 才会通过getparameter()方法取得请求体中的相应的请求参数的数据。而PUT,delete请求的请求体中数据则默认不会被解析。

关于delete请求:delete请求用来从服务器上删除资源。因此我们只需要把要删除的资源的ID上传给服务器,即使是批量删除的时候,也可以通过URL传参的方式将多个id传给servlet,因此,可以满足我们的需求,可以直接发送请求。

关于put请求(指的是带有请求体)

没有文件时:SpringMVC提供了一个将post转换为put和delete的方法,通过在web.xml中注册一个HiddenHttpMethodFilter过滤器。

上传文件时:我们可以通过在web.xml中注册一个MultipartFilter,一定要在HiddenHttpMethodFilter之前。

SpringMVC实现PUT,DELETE请求
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:dispatcher-servlet.xml
1
dispatcher
/
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
dispatcher
然后我们看源码:
@Override
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);
}
}
this.methodParam属性被默认初始化为"_method",通过request.getParameter(this.methodParam);判断是put还是delete,
"POST".equals(request.getMethod()),而且必须要求是post方式提交的,
然后它把request进行包装后传给下一个filter。
因此,我们需要在提交的时候添加一个字段
或者在$.ajax中
function login() {
$.ajax({
type: "post",//请求方式
url: "", //发送请求地址
timeout: 30000,//超时时间:30秒
data: {
"username": $('#username').val(),
"password": $("#password").val(),
"_method": delete
},
dataType: "json",//设置返回数据的格式
success: function (data) {
console.log(data);
},
error: function () { //请求出错的处理
}
});
}

然后我们就可以在后台@RequestMapping(value = "", method = RequestMethod.PUT)注解中标识我们的方法,最后就可以成功地获得数据。

SpringMVC实现PUT请求上传文件

可是后来我又有遇到另外一个需求那就是修改的时候需要传送文件到put方法中,于是这种方法就不可行了,但是我在HiddenHttpMethodFilter源码中看到这样一句话

* 
NOTE: This filter needs to run after multipart processing in case of a multipart
* POST request, due to its inherent need for checking a POST body parameter.
* So typically, put a Spring {@link org.springframework.web.multipart.support.MultipartFilter}
* before
和MultipartFilter源码中这样的注释
/**
* Set the bean name of the MultipartResolver to fetch from Spring's
* root application context. Default is "filterMultipartResolver".
*/
也就是说我们可以通过在web.xml中注册一个MultipartFilter,一定要在HiddenHttpMethodFilter之前。
MultipartFilter
org.springframework.web.multipart.support.MultipartFilter
MultipartFilter
dispatcher
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
然后再在Spring的 root application context中添加如下代码
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
FormData对象是html5的一个对象,目前的一些主流的浏览器都已经兼容。FormData对象是html5的一个对象,目前的一些主流的浏览器都已经兼容。
function test() {
var form = new FormData(document.getElementById("tf"));
form.append("_method", 'put');
$.ajax({
url: url,
type: 'post',
data: form,
processData: false,
contentType: false,
success: function (data) {
window.clearInterval(timer);
console.log("over..");
},
error: function (e) {
alert("错误!!");
window.clearInterval(timer);
}
});
get();//此处为上传文件的进度条
}

最后,就可以实现将文件上传提交给put方法。