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 ...
随机推荐
- Bootstrap3基础 page-header 标题下加分割线
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- _pet
可以控制各职业召唤物的属性.用于增强BB `comment` 备注 `classIndex` 职业序号 `DmgAddPct` 宠物伤害倍率 `SpAddPct` 法术伤害 `HpAddPct`血量倍 ...
- 【BZOJ】 4810: [Ynoi2017]由乃的玉米田
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4810 思路还是比较显然,第一反应应该就是莫队. 考虑怎么维护三个询问,想到了要维护每一个数 ...
- [Hibernate] One-To-Many 配置文件和注解的方式以及HQL语句
一对多需要在一的类配置多的类的set泛型集合. 多的一端需要添加一的类作为属性,其和数据库对应的是对应表的主键. 一个购物车有多个商品,购物车有个cart_id作为主键,商品除了自己的items_id ...
- Type I and type II errors | 第一类错误和第二类错误
偶尔能看懂,但是死活记不住,归根结底是没有彻底理解! Type I and type II errors - wiki type I error is the rejection of a true ...
- Python自学:第二章 动手试一试
print(1 + 7) print(16 - 8) print(2 * 4) print(8 / 1) 输出为: 8 8 8 8.0 message = " print("I l ...
- 正则表达式中pw、IDCard和EM匹配
1密码强度正则 //密码强度正则,最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符 var pPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])( ...
- 打造springboot高性能服务器(spring reactor的使用)
推荐:https://www.cnblogs.com/ivaneye/p/5731432.htmlpom依赖: <dependency> <groupId>org.spring ...
- Mac安装brew(遇到的坑)
1.安装方法: 网上都会有 命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/i ...
- zabbix3.4.7页面中文乱码
无须重启任何服务,刷新页面即可.