如何在SpringMVC中使用REST风格的url
来源:互联网 发布:c 俄罗斯方块源码 编辑:程序博客网 时间:2024/06/05 22:36
1.url写法:
get:/restUrl/{id}
post:/restUrl
delete:/restUrl/{id}
put:/restUrl
2.controller写法:
1)GET请求的目标方法:
@RequestMapping(value="/restUrl/{id}", method=RequestMethod.GET) public String get(Map<String, Object> map, @PathVariable("id") Integer id){ Object obj = new Object(); map.put("obj", obj); return "success"; }
注意:
1.必须在@RequestMapping注解中添加method=RequestMethod.GET,表明这是一个处理get请求的目标方法
2.通过@PathVariable("id") Integer id注解,将url中的{id}值取出,并赋值给该注解修饰的入参id
2)POST请求的目标方法:
@RequestMapping(value="/restUrl", method=RequestMethod.POST) public String post(Object obj){ System.out.println(obj); return "success"; }
注意:
1.必须在@RequestMapping注解中添加method=RequestMethod.POST,表明这是一个处理post请求的目标方法
2.post请求的url中不需要写参数{id}
3)DELETE请求的目标方法:
@RequestMapping(value="/restUrl/{id}", method=RequestMethod.DELETE) public String delete(@PathVariable("id") Integerid){ System.out.println(id); return "success"; }
注意:
1.必须在@RequestMapping注解中添加method=RequestMethod.DELETE,表明这是一个处理delete请求的目标方法
2.url中必须带有参数{id}
4)PUT请求的目标方法:
@RequestMapping(value="/restUrl", method=RequestMethod.PUT) public String put(Object obj){ System.out.println(obj); return "success"; }
注意:
1.必须在@RequestMapping注解中添加method=RequestMethod.PUT,表明这是一个处理put请求的目标方法
2.url中不需要带有参数{id}
3.如果需要使用@ModelAttribute来进行一些修改前的操作(如:先去数据库查询一个实体,在使用put目标方法),请参考我的另一篇博客《@ModelAttribute注解的使用详解》
3.jsp页面中的链接写法:
1)get请求:
<a href="${pageContext.request.contextPath}/user/restUrl/{id}">get user</a>
注意:
1.这里的{id}不能直接写{id},而是你要动态赋值的
2)post请求:
<form action="${pageContext.request.contextPath }/restUrl" method="post" > name:<input type="text" name="username"><br> password:<input type="password" name="password"><br> <input type="submit" value="submit"></form>
注意:
1.因为超链接是get请求,所以要使用post风格的url请求必须使用表单
2.必须表明表单的提交方式为method=post
3)delete请求:
<a class="delete_href" href="${pageContext.request.contextPath }/restUrl/{id}">remove</a>
<form id="delete_form" action="" method="post"> <input type="hidden" name="_method" value="DELETE"></form>
$(function(){ $(".delete_href").on("click", function(){var href = $(this).attr("href"); $("#delete_form").attr("action", href).submit(); return false; })})
注意:
1.由于超链接只能发送get请求,我们需要发送delete请求的话,必须通过一个表单提交,将表单的post请求,转换成delete请求
2.在表单中添加一个隐藏域<input type="hidden" name="_method" value="DELETE">,能让表单在提交的时候将请求转换成delete请求
3.用js实现在点击超链接时,实际上提交的是表单。但是要注意在js之前,请先引入jquery文件
4)put请求:
<form action="${pageContext.request.contextPath }/restUrl" method="post" > <input type="hidden" name="_method" value="PUT"> <input type="hidden" name="id" value="${id }"> name:<input type="text" name="username"><br> password:<input type="password" name="password"><br> <input type="submit" value="submit"></form>
注意:
1.跟delete请求类似,我们需要一个隐藏域<input type="hidden" name="_method" value="PUT">,来将post请求转换成put请求
阅读全文
0 0
- 如何在SpringMVC中使用REST风格的url
- springMvc Rest风格Url
- SpringMVC中的Rest风格URL
- 使用springMVC构建REST风格的应用
- springmvc使用rest风格的404错误
- 使用springMVC创建REST风格的服务器
- SpringMVC -- @PathVariable/Rest风格的URL HiddenHttpMethodFilter (2)
- SpringMVC实现REST风格URL的两种方法
- SpringMVC用HiddenHttpMethodFilter配置Rest风格的URL
- SpringMVC中restful风格的URL
- springmvc Rest 风格的 CRUD
- SpringMVC的REST风格开发
- SpringMvc在拦截其中获得rest风格的变量
- 使用SpringMVC实现REST风格的CRUD功能
- SpringMVC DispatcherServlet在配置Rest url-pattern的一点技巧
- SpringMVC 配置Rest api中url-pattern的技巧
- 如何在springMVC 中对REST服务使用mockmvc 做测试
- 如何在springMVC 中对REST服务使用mockmvc 做测试
- win10下python环境变量设置
- pycharm专业版的激活码过期
- 小学数学解题思维窍门
- 论文笔记understanding black-box predictions via influence functions
- Java设计模式之备忘录模式
- 如何在SpringMVC中使用REST风格的url
- PGM图片的一些知识
- MIX2 适配看这一篇就够了,18:9 只需一行代码
- eclipse启动项目报内存溢出
- OKHttp 数据读写进度监听分析
- Redis大部分相关
- NSURLComponents 使用简介
- 批量修改myeclipse项目中已有的htm文件的编码格式
- java反编译工具JD-GUI资源