在JQuery中,咱們能夠進行REST ful中delete和put的請求,可是在java EE標準中,默認只有在POST請求的時候,servlet 纔會經過getparameter()方法取得請求體中的相應的請求參數的數據。而PUT,delete請求的請求體中數據則默認不會被解析。javascript
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <servlet-name>dispatcher</servlet-name> </filter-mapping>
而後咱們看源碼:html
@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); } }
_method
",經過request.getParameter(this.methodParam);
判斷是put仍是delete,"POST".equals(request.getMethod())
,並且必需要求是post方式提交的,<form action="" id="formData" name="formData" method="post"> <input type="text" name="username" id="username"/> <input type="hidden" name="_method" value="delete"/> <input type="submit" value="submit"/> </form>
或者在$.ajax
中html5
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)
註解中標識咱們的方法,最後就能夠成功地得到數據。java
但是後來我又有遇到另一個需求那就是修改的時候須要傳送文件到put方法中,因而這種方法就不可行了,可是我在HiddenHttpMethodFilter
源碼中看到這樣一句話web
* <p><b>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.</b> * So typically, put a Spring {@link org.springframework.web.multipart.support.MultipartFilter} * <i>before</i> this HiddenHttpMethodFilter in your {@code web.xml} filter chain.
和MultipartFilter源碼中這樣的註釋ajax
/** * Set the bean name of the MultipartResolver to fetch from Spring's * root application context. Default is "filterMultipartResolver". */
<filter> <filter-name>MultipartFilter</filter-name> <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class> </filter> <filter-mapping> <filter-name>MultipartFilter</filter-name> <servlet-name>dispatcher</servlet-name> </filter-mapping> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter>
<bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="209715200"/> <property name="defaultEncoding" value="UTF-8"/> <property name="resolveLazily" value="true"/> </bean>
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();//此處爲上傳文件的進度條 }
<form id="tf" method="post" name="formDada" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="text" name="id"/> <input type="text" name="name"/> <input type="button" value="提" onclick="test()"/> </form>
最後,就能夠實現將文件上傳提交給put方法。spring