0
点赞
收藏
分享

微信扫一扫

springMvc32-原生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的表单进行过滤,所以在页面中必须如下设置:

1. ​​<form action="..." method="post">​​
2.
3. ​​<input type="hidden" name="_method" value="put" />​​
4.
5. ​​......​​
6.
7. ​​</form>​​
8.
9.
10. ​​而不是使用:​​
11.
12.
13.
14.
15. ​​<form action="..." method="put">​​
16.
17. ​​......​​
18.
19. ​​</form>​​
20.
21.
22. ​​同时,HiddenHttpMethodFilter必须作用于dispatcher前,所以在web.xml中配置HiddenHttpMethodFilter时,需参照如下代码:​​
23.
24.
25.
26.
27. ​​<filter>​​
28.
29. ​​<filter-name>HiddenHttpMethodFilter</filter-name>​​
30.
31. ​​<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>​​
32.
33. ​​</filter>​​
34.
35. ​​<filter-mapping>​​
36.
37. ​​<filter-name>HiddenHttpMethodFilter</filter-name>​​
38.
39. ​​<servlet-name>spring</servlet-name>​​
40.
41. ​​</filter-mapping>​​
42.
43. ​​<servlet>​​
44.
45. ​​<servlet-name>spring</servlet-name>​​
46.
47. ​​<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>​​
48.
49. ​​<init-param>​​
50.
51. ​​<param-name>contextConfigLocation</param-name>​​
52.
53. ​​<param-value>classpath:dispatcher.xml</param-value>​​
54.
55. ​​</init-param>​​
56.
57. ​​</servlet>​​
58.
59. ​​<servlet-mapping>​​
60.
61. ​​<servlet-name>spring</servlet-name>​​
62.
63. ​​<url-pattern>*.html</url-pattern>​​
64.
65. ​​</servlet-mapping>​​

        同样的,作为Filter,可以在web.xml中配置HiddenHttpMethodFilter的参数,可配置的参数为methodParam,值必须为GET,、POST、 HEAD、OPTIONS、PUT、DELETE、TRACE中的一个。



举报

相关推荐

0 条评论