瀏覽器form表單只支持GET與POST請(qǐng)求,而DELETE、PUT等method并不支持,spring3.0添加了一個(gè)過(guò)濾器,可以將這些請(qǐng)求轉(zhuǎn)換為標(biāo)準(zhǔn)的http方法,使得支持GET、POST、PUT與DELETE請(qǐng)求,該過(guò)濾器為HiddenHttpMethodFilter。 HiddenHttpMethodFilter的父類(lèi)是OncePerRequestFilter,它繼承了父類(lèi)的doFilterInternal方法,工作原理是將jsp頁(yè)面的form表單的method屬性值在doFilterInternal方法中轉(zhuǎn)化為標(biāo)準(zhǔn)的Http方法,即GET,、POST、 HEAD、OPTIONS、PUT、DELETE、TRACE,然后到Controller中找到對(duì)應(yīng)的方法。例如,在使用注解時(shí)我們可能會(huì)在Controller中用于@RequestMapping(value = "list", method = RequestMethod.PUT),所以如果你的表單中使用的是<form method="put">,那么這個(gè)表單會(huì)被提交到標(biāo)了Method="PUT"的方法中。
需要注意的是,由于doFilterInternal方法只對(duì)method為post的表單進(jìn)行過(guò)濾,所以在頁(yè)面中必須如下設(shè)置:
- <form action="..." method="post">
- <input type="hidden" name="_method" value="put" />
- ......
- </form>
而不是使用:
- <form action="..." method="put">
- ......
- </form>
同時(shí),HiddenHttpMethodFilter必須作用于dispatcher前,所以在web.xml中配置HiddenHttpMethodFilter時(shí),需參照如下代碼:
- <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>spring</servlet-name>
- </filter-mapping>
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:dispatcher.xml</param-value>
- </init-param>
- lt;/servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>*.html</url-pattern>
- lt;/servlet-mapping>
同樣的,作為Filter,可以在web.xml中配置HiddenHttpMethodFilter的參數(shù),可配置的參數(shù)為methodParam,值必須為
GET,、POST、 HEAD、OPTIONS、PUT、DELETE、TRACE中的一個(gè)。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。