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 等参数绑定注解详解的更多相关文章

  1. 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    @RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...

  2. springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...

  3. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...

  4. 11.@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: handler method ...

  5. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  6. (转)@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  7. 转载:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    转载自:https://blog.csdn.net/walkerjong/article/details/7946109#commentBox   因为写的很好很全,所以转载过来 引言:接上一篇文章, ...

  8. Spring @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    背景 昨天一个人瞎倒腾spring boot,然后遇到了一点问题,所以把这个问题总结一下. 主要讲解request 数据到handler method 参数数据的绑定,所用到的注解和什么情形下使用. ...

  9. @PathVariable @RequestParam @RequestBody等参数绑定注解详解

    一.分类 handler method 参数绑定常用的注解,我们根据他们处理的Request的内容不同分为四类: 处理request uri 部分的注解:   @PathVariable;(这里指ur ...

随机推荐

  1. PHP遍历目录和文件及子目录和文件

    正常直接使用opendir方法,就可以读到所有的目录和文件 文件可以直接记录下来,目录则需要再进一步获取里边的文件信息 也就是,如果当前读出来是目录,则需要再次调用函数本身(递归),直到没有目录 循环 ...

  2. 剑指offer(61)序列化二叉树

    题目描述 请实现两个函数,分别用来序列化和反序列化二叉树 题目分析 首先拿到题目时候,我先想到的是什么是序列化二叉树?序列化主要就是在前后端交互时候需要转换下,毕竟网络传输的是流式数据(二进制或者文本 ...

  3. 破解微信防盗链&微信公众号文章爬取方案

    破解微信图文防盗链:https://www.cnblogs.com/xsxshmily/p/8000043.html 图片解除防盗链:https://blog.csdn.net/show_ljw/ar ...

  4. 深度学习标注工具 LabelMe 的使用教程(Windows 版本)

    深度学习标注工具 LabelMe 的使用教程(Windows 版本) 2018-11-21 20:12:53 精灵标注助手:http://www.jinglingbiaozhu.com/ LabelM ...

  5. Linux配置中文输入法(搜狗输入法)

    一.基础知识 在原生ubuntu14.04英文环境系统中只有IBus拼音,真的好难用.由于搜狗输入法确实比Linux系统下其它的中文输入法都要好用得多,所以我决定在我的Ubuntu 14.04系统中安 ...

  6. 3、zabbix配置入门

    Zabbix模板 zabbix组件:    zabbix-server    zabbix-database    zabbix-web    zabbix-agent    zabbix-proxy ...

  7. js同步、异步、回调的执行顺序以及闭包的理解

    首先,记住同步第一.异步第二.回调最末的口诀 公式表达:同步=>异步=>回调 看一道经典的面试题: for (var i = 0; i < 5; i++) { setTimeout( ...

  8. vue-update-表单形式复写方法上传图片

    handleSave() { const formData = new FormData(); /* eslint-disable */ for (let key in this.dataInfo) ...

  9. RandomAccessFile多线程下载

    public class DownloadServer { ; private static String fileUrl = "https://dldir1.qq.com/qqtv/mac ...

  10. linux安装nginx,遇坑解决

    1.nginx官网下载tar包,解压linux下: 2.进入解压文件夹,执行./configure: 3.报错,原因没有安装nginx相关依赖,如gcc环境,PCRE依赖库 ,zlib 依赖库 ,Op ...