Form集合的参数是_SpingMvc复杂参数传收总结

a714f44651e926099fc4edb416669cbd.png

上一篇文章[javaWeb传收参数方式总结]总结了简单传收参数,这一篇讲如何传收复杂参数,比如Long[] 、User(bean里面包含List)、User[]、List、List等几种复杂参数。

一.简单数组集合类

比如Long[],String[],List等

前端:

1.重复单个参数

//(1)普通
http://localhost:8080/ajaxGet?id=1&id=2&id=3
//(2)Ajaxget方式 发送请求时等于(1)方式
    $.ajax({
        type: "GET",
        url: "http://localhost:8080/ajaxGet?id=1&id=2&id=3"
    });
//(3)Form表单GET方式 发送请求时等于(1)方式
<form id="fromGet" action="fromGet" method="GET">
    <input type="text"name="id" value="1">
    <input type="text"name="id" value="2">
    <input type="text"name="id" value="3">
</form>
//(4)Form表单POST方式 
//发送请求参数会被拼接成 id=1&id=2&id=3 存储在请求体中
<form id="fromGet" action="fromGet" method="POST">
    <input type="text"name="id" value="1">
    <input type="text"name="id" value="2">
    <input type="text"name="id" value="3">
</form>

后端SpringMvc:

//数组
public void ajaxGet(Long[] id){
}
//List集合
public void ajaxGet(@RequestParam("id") List<Long> id){
}

2.数组参数

前端:

//(1)普通url
http://localhost:8080/ajaxGet?id[]=1&id[]=2&id[]=3
//2.Form GET方式(Ajax异步表单提交) 发送请求时等于(1)方式
$.ajax({
        type: "GET",
        url: "http://localhost:8080/ajaxGet",
        data: {"id":[1,2,3]},
        contentType:'application/x-www-form-urlencoded'
    });
//(3)Form POST方式(Ajax异步表单提交)
//发送请求参数会被拼接成 id[]=1&id[]=2&id[]=3 存储在请求体中
$.ajax({
        type: "POST",
        url: "http://localhost:8080/ajaxPost",
        data: {"id":[1,2,3]},
        contentType:'application/x-www-form-urlencoded'
    });

后端SpringMvc:

//数组
public void ajaxGet(@RequestParam("id[]") Long[] id){
}
//List集合
public void ajaxGet(@RequestParam("id[]") List<Long> id){
}

其实以上两种都是一个道理,主要是看发送请求时 参数是id还是id[](可使用浏览器的F12开发者工具查看network请求),来决定后端使不使用@RequestParam("id[]")进行数据绑定

二.复杂实体类与集合

比如User(bean里面包含List)、User[]、List、List等,此种类型均使用Json提交

1.复杂实体类User

User实体类

//User实体类
public class User {  
    private String name;   
    private String pwd;  
    private List<User> customers;//属于用户的客户群 
    //省略getter/setter  
}

前端:

//用户
var user = {};
user.name = "李刚";
user.pwd = "888";
//客户
var customerArray = new Array();
customerArray.push({name: "李四",pwd: "123"});
customerArray.push({name: "张三",pwd: "332"});
user. customers = customerArray;

$.ajax({
        type: "POST",
        url: "http://localhost:8080/ajaxPost",
        data: JSON.stringify(user),
        contentType:'application/json;charset=utf-8'
    });

后端SpringMvc:

public void ajaxPost(@ResponBody User user){
 }

前端:

//用户
var userList = new Array();  
userList.push({name: "李四",pwd: "123"});   
userList.push({name: "张三",pwd: "332"});   
$.ajax({
        type: "POST",
        url: "http://localhost:8080/ajaxPost",
        data: JSON.stringify(userList),
        contentType:'application/json;charset=utf-8'
    });

后端SpringMvc:

public void ajaxPost(@ResponBody User[] user){
 }
public void ajaxPost(@ResponBody List<User> user){
 }
public void ajaxPost(@ResponBody List<Map<String,Object>> userMap){
 }

c0ef11eaba4d3784f1aff3acf5345ddd.png

1e2e5f38306249257259c2131f6ed967.gif

THANDKS

  • End -

一个立志成大腿而每天努力奋斗的年轻人

伴学习伴成长,成长之路你并不孤单!

d62bdfcd3ffd93011f8b77f7aff7882e.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值