标签:新建 协议 action str pac request font 扩展 ext
@Controller @RequestMapping("/rest") public class RestController { @RequestMapping(value="/user/{id}",method=RequestMethod.GET) public String get(@PathVariable("id") Integer id){ System.out.println("get"+id); return "/hello"; } @RequestMapping(value="/user/{id}",method=RequestMethod.POST) public String post(@PathVariable("id") Integer id){ System.out.println("post"+id); return "/hello"; } @RequestMapping(value="/user/{id}",method=RequestMethod.PUT) public String put(@PathVariable("id") Integer id){ System.out.println("put"+id); return "/hello"; } @RequestMapping(value="/user/{id}",method=RequestMethod.DELETE) public String delete(@PathVariable("id") Integer id){ System.out.println("delete"+id); return "/hello"; } }
浏览器 form 表单只支持 GET 与 POST 请求,而DELETE、PUT 等 method 并不支 持,Spring3.0 添加了一个过滤器----HiddenHttpMethodFilter,
可以将这些请求转换 为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。
1).在web.xml文件中配置:
<!-- HiddenHttpMethodFilter过滤器可以将POST请求转化为put请求和delete请求! --> <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>
2)在表单域中需要携带一个name值为_method, value值为put或者delete的参数,如下所示:
<form action="${pageContext.request.contextPath }/user/1" method="post"> <input type="hidden" name="_method" value="put"/> <input type="submit" value="Submit"/> </form> <form action="${pageContext.request.contextPath }/user/1" method="post"> <input type="hidden" name="_method" value="delete"/> <input type="submit" value="Submit"/> </form>
此时点击Submit按钮,两个表单会分别以put 和delete点的方式进行提交
SpringMVC笔记(七)RESTFul风格的SringMVC
标签:新建 协议 action str pac request font 扩展 ext
原文地址:http://www.cnblogs.com/Actexpler-S/p/7425221.html