zoukankan      html  css  js  c++  java
  • Spring3.0之后->Spring 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的表单进行过滤,所以在页面中必须如下设置:

    [java] view plaincopy
     
    1. <form action="..." method="post">  
    2.         <input type="hidden" name="_method" value="put" />  
    3.         ......  
    4. </form>  


            而不是使用:

    [java] view plaincopy
     
    1. <form action="..." method="put">  
    2.         ......  
    3. </form>  


            同时,HiddenHttpMethodFilter必须作用于dispatcher前,所以在web.xml中配置HiddenHttpMethodFilter时,需参照如下代码:

    [java] view plaincopy
     
    1.       <filter>    
    2.               <filter-name>HiddenHttpMethodFilter</filter-name>    
    3.               <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>    
    4.       </filter>    
    5.       <filter-mapping>    
    6.               <filter-name>HiddenHttpMethodFilter</filter-name>    
    7.               <servlet-name>spring</servlet-name>    
    8.       </filter-mapping>  
    9.       <servlet>  
    10. <servlet-name>spring</servlet-name>  
    11. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    12. <init-param>  
    13.     <param-name>contextConfigLocation</param-name>  
    14.     <param-value>classpath:dispatcher.xml</param-value>  
    15. </init-param>  
    16. lt;/servlet>  
    17.       <servlet-mapping>  
    18. <servlet-name>spring</servlet-name>  
    19. <url-pattern>*.html</url-pattern>  
    20. lt;/servlet-mapping>  


            同样的,作为Filter,可以在web.xml中配置HiddenHttpMethodFilter的参数,可配置的参数为methodParam,值必须为GET,、POST、 HEAD、OPTIONS、PUT、DELETE、TRACE中的一个。这样就可以很好的解决PUT和DELETE方法问题。美中不足的就是多加了一个hidden fileds,有一些bad small。

    转载请注明:http://www.xujin.org或http://www.virgocloud.com

  • 相关阅读:
    LFS Linux From Scratch 笔记2(经验非教程)BLFS
    LFS Linux From Scratch 笔记(经验非教程)
    Using VNC on a debian/Ubuntu server with a OS X Mac
    Use weechat (IRC client) on OS X. MacBook Pro
    HC
    Vim 7.4.1952 with Python/Ruby/Lua/Perl/C Syntax built for Ubuntu 16.04 x86_64
    How to make workflow chart using several tools in Linux?
    STM32 串口 重定向
    有哪些大家不说,但需要知道的社会规则?
    如何理解 UL94HB , UL94-V0 , UL94-V1 , UL94-V2
  • 原文地址:https://www.cnblogs.com/ACMer/p/4274394.html
Copyright © 2011-2022 走看看