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 ...
随机推荐
- 9个顶级开发IoT项目的开源物联网平台
https://blog.csdn.net/shnbiot/article/details/80432017 物联网(IoT)是帮助人工智能(AI)以更好的方式控制和理解事物的未来技术. 我们收集了一 ...
- TPS54331 TPS54332 3.5V to 28V Input, 3A, 570kHz Step-Down Converter with Eco-mode
The TPS54331 is a 28-V, 3-A non-synchronous buck converter that integrates a low RDS(on) high side M ...
- Python imprt动态模块
1.解释器内部用的动态导入 directory_variable = __improt__("directory.filename") print(directory_variab ...
- Python select实现socket并发
Python select Python的select()方法直接调用操作系统的IO接口,它监控sockets,open files, and pipes(所有带fileno()方法的文件句柄)何时 ...
- [Python]基础教程(3)、Python基础语法
Python 标识符 在 Python 里,标识符由字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大小 ...
- Python3 tkinter基础 OptionMenu 将list导入下拉列表中
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 编码原则 之 Explicit Dependencies Principle
Explicit Dependencies Principle The Explicit Dependencies Principle states: Methods and classes shou ...
- jsp/servlet学习三之会话管理初解
由于http的无状态性,使得会话管理或会话跟踪成为web应用开发一个无可避免的主题.默认下,一个web服务器无法区分一个http请求是否为第一次访问.例如,一个web邮件应用要求用户登陆后才能查看邮件 ...
- MG90S 舵机 使用方法 树莓派
来源 舵机的控制信号为周期是 20ms 的脉宽调制(PWM)信号,其中脉冲宽度从 0.5ms-2.5ms,相对应舵盘的位置为 0-180 度,呈线性变化. 也就是说,给它提供一定的脉宽,它的输出轴就会 ...
- Git 与 GitHub 入门级
今天我们来搞一下Git 这东西虽然没啥搞头儿,但是开发当中还必须得会用,谁让你我都是苦逼的开发呢~~~~ 一.下载与安装 这玩意简单,给你赋个图片,自己研究一下~~~~ 1.官网:https://gi ...