SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解
request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用:
http://blog.csdn.net/walkerjong/article/details/7946109 (关于更多参数绑定常用的注解)
@PathVariable
$.ajax({
url: ctx + '/management/cart/delete/'+id,
async: false,
cache: false,
type: "POST",
success: function (data) {
},
error: function (xhr) {
}
});
@ResponseBody
@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
public int delete(@PathVariable int id){
return 0;
}
//若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称
public int delete(@PathVariable("id") int rsId){
return 0;
}
@RequestParam
A) 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String--> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;
B)用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST;
C) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;
$.ajax({
url: ctx + '/management/cart/delete?id='+id,
async: false,
cache: false,
type: "POST",
success: function (data) {
},
error: function (xhr) {
}
});
@ResponseBody
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public int delete(@RequestParam("id") int id){
return 0;
}
@RequestBody
该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,而是application/json, application/xml等;
基于ajax的方法请求,将contentType设置为application/json
var cartId = 1;
$.ajax({
url: ctx + '/management/cart/delete',
async: false,
cache: false,
type: "POST",
contentType: "application/json",
data: cartId,
success: function (data) { },
error: function (xhr) { }
}); @ResponseBody
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public int delete(@RequestBody Integer id) {
return 0;
}
@RequestBody接收的是一个Json对象的字符串,而不是一个Json对象。然而在ajax请求往往传的都是Json对象,后来发现用 JSON.stringify(data)的方式就能将对象变成字符串。同时ajax请求的时候也要指定contentType:"application/json" 这样就可以轻易的将一个对象或者List传到Java端,使用@RequestBody即可绑定对象或者List.
var params = [1,2,3];
$.ajax({
url: ctx + '/management/cart/delete',
async: false,
cache: false,
type: "POST",
contentType: "application/json",
data: JSON.stringify(params),
success: function (data) { },
error: function (xhr) { }
});
@ResponseBody
@RequestMapping(value = "/deletes", method = RequestMethod.POST)
public int deletes(@RequestBody List<Integer> ids) {
return 0;
}
@ResponseBody
@RequestMapping(value = "/create", method = RequestMethod.POST)
public int create(@RequestBody ExtTaskAssignmentCfg extTaskAssignmentCfg) {
UserInfo userInfo = contextService.getContextUserInfo();
extTaskAssignmentCfg.setInsman(userInfo.getUsername());
extTaskAssignmentCfg.setInsdate(new Date());
return this.extTaskAssignmentCfgService.insert(extTaskAssignmentCfg);
} var jsonObject = {};
jsonObject.id = assignmentCfgId;
jsonObject.activitiId = activityId;
jsonObject.configurationDetail = configurationDetail;
var jsonStr = JSON.stringify(jsonObject);
$.ajax({
url: url,
async: false,
cache: false,
type: "POST",
contentType:"application/json",
data: jsonStr,
success: function (data) {
},
error: function (xhr) {
alert("保存受理人分配配置失败")
}
})
@RequestMapping(value = "saveUser", method = {RequestMethod.POST }})
@ResponseBody
public void saveUser(@RequestBody List<User> users) {
userService.batchSave(users);
}
var saveDataAry=[];
var data1={"userName":"test","address":"gz"};
var data2={"userName":"ququ","address":"gr"};
saveDataAry.push(data1);
saveDataAry.push(data2);
$.ajax({
type:"POST",
url:"user/saveUser",
dataType:"json",
contentType:"application/json",
data:JSON.stringify(saveDataAry),
success:function(data){
}
});
SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解的更多相关文章
- 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...
- springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...
- 11.@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: handler method ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)
引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...
- (转)@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...
- 转载:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
转载自:https://blog.csdn.net/walkerjong/article/details/7946109#commentBox 因为写的很好很全,所以转载过来 引言:接上一篇文章, ...
- Spring @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
背景 昨天一个人瞎倒腾spring boot,然后遇到了一点问题,所以把这个问题总结一下. 主要讲解request 数据到handler method 参数数据的绑定,所用到的注解和什么情形下使用. ...
- @PathVariable @RequestParam @RequestBody等参数绑定注解详解
一.分类 handler method 参数绑定常用的注解,我们根据他们处理的Request的内容不同分为四类: 处理request uri 部分的注解: @PathVariable;(这里指ur ...
随机推荐
- jQuery基本的属性操作
attr和prop,prop常用来操作标签的固有属性,比方说checkbox的checked属性.select的selected属性,而attr常用来操作我们自己给标签添加的属性. $('div'). ...
- CEF 右键添加开发者选项菜单项
在项目开发过程中,有时候需要进行调试测试,然后我们可以在cef上下文菜单中添加自定义开发者工具菜单项,这样会比较方便,最后效果: 实现过程: 让自己的MyClientHandler来继承 CefCon ...
- HDU 5333 Undirected Graph(动态树)
题意 给定一棵 \(n\) 个节点, \(m\) 条边的无向图,每个点有点权,有 \(q\) 个询问,每次询问若删去存在一个节点权值在 \([L,R]\) 范围外的边,剩下的图构成了多少个连通块(询问 ...
- Spring boot+mybatis+thymeleaf 实现登录注册,增删改查
本文重在实现理解,过滤器,业务,逻辑需求,样式请无视.. 项目结构如下 1.idea新建Spring boot项目,在pom中加上thymeleaf和mybatis支持.pom.xml代码如下 < ...
- python使用os.listdir和os.walk获得文件的路径
python使用os.listdir和os.walk获得文件的路径 目录 情况1:在一个目录下面只有文件,没有文件夹,这个时候可以使用os.listdir 情况2:递归的情况,一个目录下面既有目录 ...
- Mysql增量恢复
mysqldump增量恢复何时需要使用备份的数据? 备份最牛逼的层次,就是永远都用不上备份.--老男孩 不管是逻辑备份还是物理备份,备份的数据什么时候需要用?===================== ...
- linux软件管理之概述
软件包管理 ====================================================================================安装/查询/卸载 一 ...
- Linux下执行.sh命令出现-bash: ./bin/start.sh: /bin/bash^M: bad interpreter: No such file or directory
原因是 文件的格式是dos,修改为unix 就OK了 查看文件格式 用vim 打开出错的文件 按 ESC键 再按shift+冒号 输入 set ff 回车 可以看见 该文件 ...
- python自动化测试入门篇-jemter
接口测试基础-jemter 接口文档地址:http://doc.nnzhp.cn 使用jmeter实现简单的http request的接口测试 一.get获取学生信息接口 第一步:新建一个Thread ...
- 数据结构与算法之PHP排序算法(快速排序)
一.基本思想 快速排序又称划分交换排序,是对冒泡排序的一种改进,亦是分而治之思想在排序算法上的典型应用. 它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部 ...