SpingMvc复杂参数传收总结

8次阅读

共计 2874 个字符,预计需要花费 8 分钟才能阅读完成。

上一篇文章 [javaWeb 传收参数方式总结] 总结了简单传收参数, 这一篇讲如何传收复杂参数, 比如 Long[]、User(bean 里面包含 List)、User[]、List<User><user style=”margin: 0px; padding: 0px; max-width: 100%; overflow-wrap: break-word !important; box-sizing: border-box;”>、List<Map<String,Object> 等几种复杂参数 </user>。
一. 简单数组集合类
比如 Long[],String[],List<User><long style=”margin: 0px; padding: 0px; max-width: 100%; overflow-wrap: break-word !important; box-sizing: border-box;”> 等 </long>
前端:
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<User><user style=”margin: 0px; padding: 0px; max-width: 100%; overflow-wrap: break-word !important; box-sizing: border-box;”>、List<Map<String,Object> 等, 此种类型均使用 Json 提交 </user>
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){
}

 THANDKS
End –
一个立志成大腿而每天努力奋斗的年轻人
伴学习伴成长,成长之路你并不孤单!

正文完
 0