需求就是:

现在保存一个Student,并且保存Student的friend,一个student会有多个朋友,这里要传递到后台的参数是:

var friends = new Array();
var o1 = {
"family": "大宅门",
"otherName": "lisi",
"desc": "亲密无间的好朋友"
} var o2 = {
"family": "军大院",
"otherName": "wangwu",
"desc": "关系一般"
} friends.push(o1);
friends.push(o2); //要传递的参数:id=1 name=zhangsan age=10是要保存的student信息,并且friends是student的朋友信息;
data:{
"id": 1,
"name": "zhangsan",
"age": 10,
"friends":friends
},

一、包装类型来接收JSON类型的传参:

方法一其实就是 springMVC学习(11)-json数据交互和RESTful支持 中接收json数据;只不过包装了另一个po

前台代码,ajax传递数据:

<input type="button" onclick="saveFriend()" value="保存friend" />
<script type="text/javascript">
/**
* 保存朋友
*/
function saveFriend(){
var friends = new Array();
var o1 = {
"family": "大宅门",
"otherName": "lisi",
"desc": "亲密无间的好朋友"
} var o2 = {
"family": "军大院",
"otherName": "wangwu",
"desc": "关系一般"
} friends.push(o1);
friends.push(o2); //要发送的参数
var params = {
"id": 1,
"name": "zhangsan",
"age": 10,
"friends":friends
}; $.ajax({
type: 'post',
url: '${pageContext.request.contextPath }/saveStudent',
contentType:'application/json;charset=utf-8',
dataType: 'json',
data: JSON.stringify(params),
success: function(data){
console.log(data);
}
});
}
</script>

Controller中接受这个参数:

/**
* 保存朋友
*/
@RequestMapping("/saveStudent")
@ResponseBody
public void saveStudent(@RequestBody StudendAndFriend studendAndFriend){
System.out.println("接收参数friends--->> " + studendAndFriend);
}

接收参数的包装类:

StudendAndFriend.java:

package com.cy.po;

import java.util.List;

/**
* 接收参数包装类
* @author chengyu
*
*/
public class StudendAndFriend {
private int id;
private String name;
private int age;
private List<Friend> friends; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Friend> getFriends() {
return friends;
}
public void setFriends(List<Friend> friends) {
this.friends = friends;
} }

Friend.java:

package com.cy.po;

public class Friend {
private String family;
private String otherName;
private String desc; public String getFamily() {
return family;
}
public void setFamily(String family) {
this.family = family;
}
public String getOtherName() {
return otherName;
}
public void setOtherName(String otherName) {
this.otherName = otherName;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
} @Override
public String toString() {
return "Friend [family=" + family + ", otherName=" + otherName
+ ", desc=" + desc + "]";
} }

后台成功接收参:

注意点:

ajax传参:

contentType:'application/json;charset=utf-8'; //contentType为application/json

data: JSON.stringify(params),           //传的是字符串;

方法二、                                              

传递String类型的json字符串过来,后台接收后,进行JSONArray解析成对应的List<Object>;

这里使用fastjson-1.2.2.jar包来对json字符串转化为javabean类型;

前端发送请求:

<input type="button" onclick="saveFriend2()" value="保存friend2" />
<script type="text/javascript">
/**
* 保存朋友2
*/
function saveFriend2(){
var friends = new Array();
var o1 = {
"family": "大宅门",
"otherName": "lisi",
"desc": "亲密无间的好朋友"
} var o2 = {
"family": "军大院",
"otherName": "wangwu",
"desc": "关系一般"
} friends.push(o1);
friends.push(o2); friends = JSON.stringify(friends); $.ajax({
type: 'post',
url: '${pageContext.request.contextPath }/saveStudent2',
dataType: 'json',
data: {
"id": 1,
"name": "zhangsan",
"age": 10,
"friends":friends
},
success: function(data){
console.log(data);
}
});
}
</script>

Controller接收这个参数:

   //用的是fastjson 

    import com.alibaba.fastjson.JSONArray;
    import com.cy.po.Friend;

  /**
* 保存朋友2
*/
@RequestMapping("/saveStudent2")
@ResponseBody
public void saveStudent2(int id, String name, int age, String friends){
List<Friend> frientList = JSONArray.parseArray(friends, Friend.class);
System.out.println("接收参数friends--->> " + frientList);
}

可以看到前端发送参数:

controller中经过json解析为javabean:

springMVC参数绑定JSON类型的数据的更多相关文章

  1. 利用SpringMVC参数绑定实现动态插入数据

    javabean代码:public class User { private String firstName; private String lastName; public String getF ...

  2. springmvc:请求参数绑定集合类型

    一.请求参数绑定实体类 domain: private String username; private String password; private Double money; private ...

  3. [转载]SpringBoot系列: SpringMVC 参数绑定注解解析

    本文转载自 https://www.cnblogs.com/morethink/p/8028664.html, 作者写得非常好, 致谢! SpringMVC 参数绑定注解解析   本文介绍了用于参数绑 ...

  4. 一篇文章搞定SpringMVC参数绑定

    SpringMVC参数绑定,简单来说就是将客户端请求的key/value数据绑定到controller方法的形参上,然后就可以在controller中使用该参数了 下面通过5个常用的注解演示下如何进行 ...

  5. spring-mvc---Controller参数绑定的类型

    Controller参数绑定的类型 Controller的绑定参数类型6种.没有参数(系统默认类型):简单类型(Intager,Double)等:pojo类(对象):数组:集合:HashMap等. 我 ...

  6. SpringMVC参数绑定,这篇就够了!

    SpringMVC参数绑定,简单来说就是将客户端请求的key/value数据绑定到controller方法的形参上,然后就可以在controller中使用该参数了 下面通过5个常用的注解演示下如何进行 ...

  7. SpringMVC参数绑定(未完待续)

    1. Strut2与SpringMVC接收请求参数的区别 Struts2通过action类的成员变量接收SpringMVC通过controller方法的形参接收 2. SpringMVC参数绑定流程 ...

  8. SpringMvc参数绑定出现乱码解决方法

    在SpringMvc参数绑定过程中出现乱码的解决方法 1.post参数乱码的解决方法 在web.xml中添加过滤器 <!-- 过滤器 处理post乱码 --> <filter> ...

  9. 存在日期类型的JSON数据,进行SpringMVC参数绑定时存在的问题和解决方案

    这篇文章已经过时了. 请参考比较合适的前后端交互方式. 首先是发送AJAX请求的html页面 <!DOCTYPE html> <html> <head> <m ...

随机推荐

  1. 去除 DBGridEh SelectedRows里无效的书签

    数据集处于过滤状态,然后选中几个记录,再修改了这些记录中的某个字段(和过滤条件有关),导致那几个记录不符合过滤条件,不显示了.但是SelectedRows里 还保存着.如果不删除SelectedRow ...

  2. 编译EXE文件的时候pcap编译不进去。 pyinstaller pcap pypcap 报错

    如果生成的exe源码中有import pcap 那么你目标机上就要先装npcap 并勾选winpcap API. 然后就不出这个问题了. 暂时的办法是第一个exe不包含import pcap.自检np ...

  3. Linux内核分析-分析system_call中断处理过程

    姓名:江军 ID:fuchen1994 分析system_call中断处理过程 使用gdb跟踪分析一个系统调用内核函数(您上周选择那一个系统调用),系统调用列表参见http://codelab.shi ...

  4. Javascript的堆和栈的简单理解

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. reboot 后 Docker服务及容器自动启动设置

    https://blog.csdn.net/wxb880114/article/details/82904765 重启reboot操作系统后,发现docker 服务未启动,容器也未启动,天生反骨,怎么 ...

  6. L224 词汇题

    Elaborate 精心的 preparations were being made for the Prime Minister’s official visit to the four forei ...

  7. 逆向路由器固件之解包 Part1

    这个系列的文章以逆向路由器固件的方式来挖掘路由器中存在的漏洞. 本篇文章主要以介绍解包固件工具为主.文中演示用的固件可以在这里下载.由于针对设备的攻击越来越多,很多厂商把不提供固件下载作为一种安全策略 ...

  8. Python 之 numpy 和 tensorflow 中的各种乘法(点乘和矩阵乘)

    点乘和矩阵乘的区别: 1)点乘(即“ * ”) ---- 各个矩阵对应元素做乘法 若 w 为 m* 的矩阵,x 为 m*n 的矩阵,那么通过点乘结果就会得到一个 m*n 的矩阵. 若 w 为 m*n ...

  9. jeecms系统使用介绍——通过二次开发实现对word、pdf、txt等上传附件的全文检索

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76912307 本文出自[我是干勾鱼的博客] 之前在文章<基于Java的门户 ...

  10. 1.1 Linux中的进程 --fork、孤儿进程、僵尸进程、文件共享分析

    操作系统经典的三态如下: 1.就绪态 2.等待(阻塞) 3.运行态 其转换状态如下图所示: 操作系统内核中会维护多个队列,将不同状态的进程加入到不同的队列中,其中撤销是进程运行结束后,由内核收回. 以 ...