SpringMVC基础-SpringMVC的四种请求方式
来源:互联网 发布:焊接数值模拟软件 编辑:程序博客网 时间:2024/05/16 17:32
SpringMVC注解@RequestMapping注解除了params,header属性,还有一个非常重要的属性——method,method的四种取值GET,POST,DELETE,PUT方式刚好对应了SpringMVC的四种请求方式。
SpringMVC的四种请求方式
通过rest风格占位符方式,利用@PathVariable注解将占位符的值赋给调用方法参数,GET请求方式
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET) public String testRest(@PathVariable("id") Integer id) { System.out.println("testRest get " + id); return SUCCESS; }
POST请求方式
@RequestMapping(value = "/testRest", method = RequestMethod.POST) public String testRest() { System.out.println("testRest post"); return SUCCESS; }
DELETE请求方式
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE) public String testRestDelete(@PathVariable Integer id) { System.out.println("testRestDelete " + id); return SUCCESS; }
PUT请求方式
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT) public String testRestPut(@PathVariable Integer id) { System.out.println("testRestPut " + id); return SUCCESS; }
- 上面有些请求路径是一样的,比如delete请求和get请求,都为springmvc/testRest/1,但是RequestMapping的method属性限制了请求方式,对应的请求方式对应了相应的请求路径
- rest url方式试图消除传统的url传递参数的模式
- PUT与DELETE请求方式是基于POST请求方式的
- GET:/xxx/{id} 表示通过id得到一条数据
- POST:/xxx 表示新增一条数据
- DELETE:/xxx/{id} 表示通过id删除一条数据
- PUT:/xxx/{id} 表示通过id修改一条数据
GET,POST,DELETE,PUT 刚好对应数据库crud操作
如何使用DELETE,PUT请求方式
<!-- rest风格put 表示修改一条数据--><form action="springmvc/testRest/1" method="post"> <input type="hidden" name="_method" value="PUT"/> <input type="submit" value="testRest PUT"/></form><!-- rest风格delete 表示删除一条数据--><form action="springmvc/testRest/1" method="post"> <input type="hidden" name="_method" value="DELETE"/> <input type="submit" value="testRest DELETE"/></form>
基于上面的java代码,以及DELETE,PUT请求方式是基于POST请求方式,必须在表单提交中写上一个隐藏域,而且必须将name设置为_method,value属性值写上对应的请求方式。
0 0
- SpringMVC基础-SpringMVC的四种请求方式
- SpringMVC 中对应的REST的四种请求方式
- SpringMVC的REST风格的四种请求方式
- SpringMVC的REST风格的四种请求方式
- SpringMVC中rest风格以及四种请求方式
- springMVC返回数据的四种方式
- springMVC返回数据的四种方式
- springmvc(基础五) Spring3 mvc 获取请求参数的几种方式
- springmvc 获取请求参数的几种方式
- springMVC 获取请求参数的几种方式
- SpringMVC的几种返回方式(get请求)
- springmvc@RequestMapping的method请求方式
- SpringMVC基础-@ModelAttribute注解&PUT请求方式修改数据
- SpringMVC的四种HandlerMapping
- SpringMVC的四种HandlerMapping
- springMVC请求参数接收方式
- springmvc(基础四) Springmvc 数据绑定(2)
- SpringMVC映射请求参数的方式(参数绑定方式)
- ReactNative开发如何加载iOS程序包里的文件
- ALSA声卡驱动中的DAPM详解之二:widget-具备路径和电源管理信息的kcontrol
- 802.11A/B/G/N四种协议
- 329. Longest Increasing Path in a Matrix
- jQuery实现放大镜效果
- SpringMVC基础-SpringMVC的四种请求方式
- MVC
- 文件下载中文乱码
- USACO-Section 2.2 Party Lamps (枚举)
- Android traces.txt只保留最后一次发生ANR时的traces信息,那么以前的traces信息就丢失了么?
- 99. Permutations
- 进程上下文和中断上下文
- Android之异步消息处理机制
- 提升SQLite数据插入效率低、速度慢的方法