javascript
spring mvc学习(33):原生apiSpring MVC过滤器-HiddenHttpMethodFilter
瀏覽器form表單只支持GET與POST請求,而DELETE、PUT等method并不支持,spring3.0添加了一個過濾器,可以將這些請求轉換為標準的http方法,使得支持GET、POST、PUT與DELETE請求,該過濾器為HiddenHttpMethodFilter。
? ? ? ? HiddenHttpMethodFilter的父類是OncePerRequestFilter,它繼承了父類的doFilterInternal方法,工作原理是將jsp頁面的form表單的method屬性值在doFilterInternal方法中轉化為標準的Http方法,即GET,、POST、 HEAD、OPTIONS、PUT、DELETE、TRACE,然后到Controller中找到對應的方法。例如,在使用注解時我們可能會在Controller中用于@RequestMapping(value = "list", method = RequestMethod.PUT),所以如果你的表單中使用的是<form method="put">,那么這個表單會被提交到標了Method="PUT"的方法中。
? ? ? ? 需要注意的是,由于doFilterInternal方法只對method為post的表單進行過濾,所以在頁面中必須如下設置:
<form action="..." method="post"><input type="hidden" name="_method" value="put" />......</form>而不是使用:<form action="..." method="put">......</form>同時,HiddenHttpMethodFilter必須作用于dispatcher前,所以在web.xml中配置HiddenHttpMethodFilter時,需參照如下代碼:<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></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>*.html</url-pattern></servlet-mapping>
 ? ? ? ? 同樣的,作為Filter,可以在web.xml中配置HiddenHttpMethodFilter的參數,可配置的參數為methodParam,值必須為GET,、POST、 HEAD、OPTIONS、PUT、DELETE、TRACE中的一個。
總結
以上是生活随笔為你收集整理的spring mvc学习(33):原生apiSpring MVC过滤器-HiddenHttpMethodFilter的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 《高性能MYSQL》
- 下一篇: Skype国际版使用国内卡
