/user/1 get请求 获取用户web
/user/1 post请求 新增用户spring
/user/1 put请求 更新用户app
/user/1 delete请求 删除用户jsp
须要在web.xml文件中配置一个HiddenHttpMethodFilter过滤器。该过滤过滤post请求,若是在post请求中有个一个_method参数,那么_method参数值就是请求方式。因此在jsp页面能够这样写post
1 <a href="user/1">GET请求</a> 2 3 <form action="user/1" method="post"> 4 <input type="submit" value="POST请求"/> 5 </form> 6 7 <form action="user/1" method="post"> 8 <input type="hidden" name="_method" value="PUT"> 9 <input type="submit" value="PUT请求"/> 10 </form> 11 12 <form action="user/1" method="post"> 13 <input type="hidden" name="_method" value="DELETE"> 14 <input type="submit" value="DELET请求"/> 15 </form>
web.xml配置过滤器url
1 <filter> 2 <filter-name>methodFilter</filter-name> 3 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 4 </filter> 5 6 <filter-mapping> 7 <filter-name>methodFilter</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping>
控制器spa
1 package com.proc; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.PathVariable; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 8 @Controller 9 public class User { 10 11 @RequestMapping(value="user/{id}",method=RequestMethod.GET) 12 public String get(@PathVariable("id") Integer id){ 13 System.out.println("获取用户:"+id); 14 return "hello"; 15 } 16 17 @RequestMapping(value="user/{id}",method=RequestMethod.POST) 18 public String post(@PathVariable("id") Integer id){ 19 System.out.println("添加用户:"+id); 20 return "hello"; 21 } 22 23 @RequestMapping(value="user/{id}",method=RequestMethod.PUT) 24 public String put(@PathVariable("id") Integer id){ 25 System.out.println("更新用户:"+id); 26 return "hello"; 27 } 28 29 @RequestMapping(value="user/{id}",method=RequestMethod.DELETE) 30 public String delete(@PathVariable("id") Integer id){ 31 System.out.println("删除用户:"+id); 32 return "hello"; 33 } 34 }
咱们一次点击GET请求、POST请求、PUT请求和DELETE请求code
获取用户:1 添加用户:1 更新用户:1 删除用户:1
一、在发出请求时必须是POST请求orm
二、在POST请求中添加一个名为_method的参数,其值用来指定是PUT请求仍是DELETE请求xml
三、配置HiddenHttpMethodFilter过滤器。该过滤器的做用是POST请求能够转换成PUT或DELET请求