SpringMVC RequestMapping HiddenHttpMethodFilter

知识的价值不在于占有,而在于使用。这篇文章主要讲述SpringMVC RequestMapping HiddenHttpMethodFilter相关的知识,希望能为你提供帮助。
SpringMVC隐藏方法:
使用PUT和DELETE方法。默认html支持GET和POST方法。通过HiddenHttpMethodFilter将POST转成PUT和DELETE方法。
1、将HiddenHttpMethodFilter配置到web.xml中

SpringMVC RequestMapping HiddenHttpMethodFilter

文章图片
< filter> < filter-name> HiddenHttpMethodFilter< /filter-name> < filter-class> org.springframework.web.filter.HiddenHttpMethodFilter< /filter-class> < /filter> < filter-mapping> < filter-name> HiddenHttpMethodFilter< /filter-name> < url-pattern> /*< /url-pattern> < /filter-mapping>

SpringMVC RequestMapping HiddenHttpMethodFilter

文章图片
2、控制器代码
SpringMVC RequestMapping HiddenHttpMethodFilter

文章图片
package com.tiekui.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HiddenHttpMethodFilterTest { @RequestMapping(value="https://www.songbingjia.com/android/testHiddenHttpMethod/{id}",method=RequestMethod.DELETE) public String testHiddenHttpMethodDelete(@PathVariable("id") Integer id){ System.out.println("Delete" + id); return "success"; }@RequestMapping(value="https://www.songbingjia.com/android/testHiddenHttpMethod/{id}",method=RequestMethod.PUT) public String testHiddenHttpMethodPut(@PathVariable("id") Integer id){ System.out.println("PUT" + id); return "success"; }}

SpringMVC RequestMapping HiddenHttpMethodFilter

文章图片
 
【SpringMVC RequestMapping HiddenHttpMethodFilter】3、视图代码
SpringMVC RequestMapping HiddenHttpMethodFilter

文章图片
< form action="testHiddenHttpMethod/1" method="post"> < input type="hidden"name="_method" value="https://www.songbingjia.com/android/PUT"> < input type="submit" value="https://www.songbingjia.com/android/testHiddenHttpMethodPUT"> < /form> < br> < form action="testHiddenHttpMethod/1" method="post"> < input type="hidden"name="_method" value="https://www.songbingjia.com/android/DELETE"> < input type="submit" value="https://www.songbingjia.com/android/testHiddenHttpMethodDelete"> < /form>

SpringMVC RequestMapping HiddenHttpMethodFilter

文章图片

    推荐阅读