网站首页 > java教程 正文
前后端发送接收数组参数
两种在url之中请求的方式
1.http://localhost:9992/test/array?titles=123&titles=hello
2.http://localhost:9992/test/array?titles=123,hello,info,test
// 测试一下这样可以接收数组吗
// http://localhost:9992/test/array?titles=123&titles=hello
// http://localhost:9992/test/array?titles=123,hello,info,test
@RequestMapping("/test/array")public String testArray(String[] titles) {
String info = "";
for (String s : titles) {
info += s;
}
return info;
}
前后端发送接收List参数
这种在浏览器之中,必须通过ajax请求来完成请求,或者在postman之中完成请求,普通的方式无法完成这样的请求。
前端的代码可以写成如下的样子,主要其实就是list的模拟,需要把list在发送ajax请求的时候序列化,这点注意一下,其他就没有了
function write_list() {
let fileName = $('#fileName').val();
// 模拟输入的一个list
let students = new Array();
students.push({ name: "李四", pwd: "123" });
students.push({ name: "张三", pwd: "332" });
$.ajax({
type: "post",
url: "/write-list",
data: {
students: JSON.stringify(students), // 将对象序列化成JSON字符串
fileName: fileName,
},
dataType: "JSON",
success: function (resp) {
let resultCode = resp.code;
if (1 == resultCode) {
console.log("okay")
} else {
console.log("error")
}
},
error: function (error) {
console.log(error);
}
});
}
后端接收的时候,需要使用@RequestBody注解,使用List类型接收即可,参数是放在http请求的body部分的,普通的参数fileName可以放到query参数之中
/**
* 一次性存放多条数据,list导入
*
* @param students
* @param fileName
* @return
* @throws IOException
*/
@PostMapping("write-list")
public String writeList(@RequestBody List<PrimaryStudent> students, String fileName) throws IOException {
if (null == students) {
return "数据无效!";
}
// 存放路径设置
String userPath = System.getProperty("user.dir");
if (!fileName.endsWith(".xls") && !fileName.endsWith(".xlsx")) {
fileName = fileName + ".xlsx";
} else {
fileName = userPath + File.separator + fileName;
}
try {
writeExcelService.writeList(students, fileName);
} catch (Exception e) {
return e.getMessage();
}
return "导出完成!";
}
猜你喜欢
- 2024-10-05 List的用法和实例详解——Java进阶知识讲义系列(四)
- 2024-10-05 从Collection到List:Java集合转换的艺术
- 2024-10-05 小心!"数组"转"集合"的这几个隐藏"bug"
- 2024-10-05 JAVA脱水学习-java数组解析及常用操作
- 2024-10-05 《极简Java新手编程之道》10.2 List集合
- 2024-10-05 字符串拆分数组(字符串拆成列表)
- 2024-10-05 Java中的ArrayList与LinkedList(java linklist和arraylist的区别)
- 2024-10-05 小白学JAVA之——List接口的实现类——ArrayList
- 2024-10-05 「漫步计算机系统」之数据结构与算法(5):Array、List和Map等
- 2024-10-05 每日分享- java 编程中 ArrayList 集合怎么扩容
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)