SpringMVC后台接收list类型的数据的实现方式
一、背景
最近在做一些东西的时候,遇到一个需要Springmvc后台接收list类型数据的需求,几经辗转才完美解决了这个问题,今天记下来方便以后使用,也分享给需要的小伙伴们~
二、实现方式
实现方式一
前端页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
</head>
<body>
<input type="button" name="request" value="请求后台" style="width:200px;height:50px;background-color:red;margin-bottom:20px;">
<div name="rs"></div>
<input type="checkbox" name="se" value="1">hafiz.zhang<br/>
<input type="checkbox" name="se" value="2">jack.chen<br/>
<input type="checkbox" name="se" value="3">lili.wang<br/>
<script type="text/javascript"> $("input[name='request']").click(function(){
var data = [];
$("input[name='se']").each(function(){
if($(this).prop("checked")) {
data.push($(this).val());
}
});
var json_data = JSON.stringify(data);
$.ajax({
type:"post",
url:$.wap.url + "/test/index",
contentType:"application/json",
data:json_data ,
dataType:"json",
success:function(data){
var str="";
for(var i = 0; i < data.length; i++) {
str += ";name=" + data[i];
}
$("div[name='rs']").html(str);
},
error:function(){
alert("出错啦");
}
});
});
</script>
</body>
</html>
后台接收
package com.hafiz.www.controller; import java.util.ArrayList;
import java.util.List; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/index", method = RequestMethod.POST)
@ResponseBody
public List<Integer> test(@RequestBody ArrayList<Integer> ids){
System.out.println("List==" + ids);
return ids;
}
}
注意:这种方法只适用于POST方法提交,(上面代码中标红的是必不可少的代码)如果使用get方法会出现如下图所示的错误

这是因为get方式的参数中的双引号会被编码,导致传到后台的不再是json串格式,所以解析出错。
实现方式二
前端页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
</head>
<body>
<input type="button" name="request" value="请求后台" style="width:200px;height:50px;background-color:red;margin-bottom:20px;">
<div name="rs"></div>
<input type="checkbox" name="se" value="1">hafiz.zhang<br/>
<input type="checkbox" name="se" value="2">jack.chen<br/>
<input type="checkbox" name="se" value="3">lili.wang<br/>
<script type="text/javascript"> $("input[name='request']").click(function(){
var data = [];
$("input[name='se']").each(function(){
if($(this).prop("checked")) {
data.push($(this).val());
}
});
$.ajax({
type:"get",
url:$.wap.url + "/test/index",
data:{"datas":data},//或者data:{"datas[]":data}
dataType:"json",
success:function(data){
var str="";
for(var i = 0; i < data.length; i++) {
str += ";name=" + data[i];
}
$("div[name='rs']").html(str);
},
error:function(){
alert("出错啦");
}
});
});
</script>
</body>
</html>
后台接收,指定参数名必须以数组方式,如:@RequestParam("datas[]")
1).通过ArrayList接收
package com.hafiz.www.controller; import java.util.ArrayList;
import java.util.List; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
@ResponseBody
public List test(@RequestParam("datas[]") ArrayList<Integer> ids){
System.out.println("List==" + ids);
return ids;
}
}
2).通过数组进行接收
package com.hafiz.www.controller; import java.util.ArrayList;
import java.util.List; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/index", method = RequestMethod.POST)
@ResponseBody
public Integer[] test(@RequestParam("datas[]") Integer[] ids){
System.out.println("ids==" + ids);
return ids;
}
}
注意:
1.这种方式对于get和post方式的请求同样都适用....
2.以上两种实现方式传到后台的数据不能为null,否则会报Http 400错误。
实现方式三
前端页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
</head>
<body>
<input type="button" name="request" value="请求后台"
style="width:200px;height:50px;background-color:red;margin-bottom:20px;">
<div name="rs"></div>
<input type="checkbox" name="se" value="1">hafiz.zhang<br/>
<input type="checkbox" name="se" value="2">jack.chen<br/>
<input type="checkbox" name="se" value="3">lili.wang<br/>
<script type="application/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript"> $("input[name='request']").click(function () {
var data = [];
$("input[name='se']").each(function () {
if ($(this).prop("checked")) {
data.push($(this).val());
}
});
$.ajax({
type: "post",
url: "/test/index",
data: {"datas": data.join()}
dataType: "json",
success: function (data) {
var str = "";
for (var i = 0; i < data.length; i++) {
str += ";name=" + data[i];
}
$("div[name='rs']").html(str);
},
error: function () {
alert("出错啦");
}
});
});
</script>
</body>
</html>
后端代码
1)通过数组接收
package com.hafiz.www.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList;
import java.util.List; /**
* Desc:测试控制器
* Created by hafiz.zhang on 2017/7/2.
*/
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/index", method = RequestMethod.POST)
@ResponseBody
public Integer[] test(@RequestParam("datas") Integer[] ids) {
System.out.println("ids=" + ids);
return ids;
}
}
2).通过List接收
package com.hafiz.www.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List; /**
* Desc:测试控制器
* Created by hafiz.zhang on 2017/7/2.
*/
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/index", method = RequestMethod.POST)
@ResponseBody
public List test(@RequestParam("datas") List<Integer> ids) {
System.out.println("ids=" + ids);
return ids;
}
}
这种方式即使没有选中任何复选框进行提交,也不会报错!
对于想要前端传自定义对象数组到后端,以上的方式就不适用了,那么解决办法是什么呢?
ajax请求中设置contentType:"application/json;charset=utf-8"
ajax请求中设置data:JSON.stringify(dataList)
后端Controller种用@RequestBody YourObject[] data进行接收,并且只能用数组接收.
如果你有更好的实现方式,希望可以拿来分享。。。。
三、总结
1.实现方式一只对post方法有效,且比较繁琐,不推荐!
2.实现方式二要求后端接收的时候必须声明参数为数组,但可以使用数组或者list进行接收参数,如:@RequestParam("datas[]"),前端使用data:{"datas":data}或data:{"datas[]":data}都可以!且post和get方法都适用。但是不能传空数组,限制也比较多,也不太推荐。
3.实现方式三只需要前端传值的时候使用数组的join()方法,为空数组也不会报错,配置简单,要求少,且支持使用数组和list进行接收参数,比较推荐!
关于传递自定义对象的集合,可以参考这篇文章:https://blog.csdn.net/sweetgirl520/article/details/79127223
SpringMVC后台接收list类型的数据的实现方式的更多相关文章
- .net mvc前台如何接收和解析后台的字典类型的数据 二分搜索算法 window.onunload中使用HTTP请求 网页关闭 OpenCvSharp尝试 简单爬虫
.net mvc前台如何接收和解析后台的字典类型的数据 很久没有写博客了,最近做了一个公司门户网站的小项目,其中接触到了一些我不会的知识点,今日事情少,便记录一下,当时想在网上搜索相关的内容,但是 ...
- SpringBoot后台接收前台的字符串数据
需求 将前台传入的字符串数据转为int类型. 操作 在pom.xml中添加引用. <dependency> <groupId>org.apache.commons</gr ...
- springMVC怎么接收日期类型的参数?
springMVC怎么接收日期类型的参数? springMVC的controller中用实体接受页面传递的参数,并且实体中的属性类型为日期类型,怎么接收呢?如果接收不到会进不到controller中. ...
- js进阶 14-3 如何接收load函数从后台接收到的返回数据
js进阶 14-3 如何接收load函数从后台接收到的返回数据 一.总结 一句话总结:load方法的回调函数的参数即可接收从后台的返回数据. 1.load方法的回调函数的参数是什么? 语法:load( ...
- springMVC参数绑定JSON类型的数据
需求就是: 现在保存一个Student,并且保存Student的friend,一个student会有多个朋友,这里要传递到后台的参数是: var friends = new Array(); var ...
- angular的post请求,SpringMVC后台接收不到参数值的解决方案
这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...
- 解决angular的post请求后SpringMVC后台接收不到参数值问题的方法
这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...
- .net mvc前台如何接收和解析后台的字典类型的数据
很久没有写博客了,最近做了一个公司门户网站的小项目,其中接触到了一些我不会的知识点,今日事情少,便记录一下,当时想在网上搜索相关的内容,但是没有找到. 今天想记录一下这样一个小的需求的做法.先说一下我 ...
- springmvc接收JSON类型的数据
1.在使用AJAX传递JSON数据的时候要将contentType的类型设置为"application/json",否则的话会提示415错误 2.传递的data需要时JSON类型的 ...
随机推荐
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- 用rose画UML图(用例图,活动图)
用rose画UML图(用例图,活动图) 首先,安装rose2003,电脑从win8升到win10以后,发现win10并不支持rose2003的安装,换了rose2007以后,发现也不可以. 解决途径: ...
- coreseek操作
开启服务$ /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf 重新索引: /usr/local/coresee ...
- mysql复习相关
Mysql相关 mysql增删改查 我们需要修改数据表名或者修改数据表字段时,就需要使用到Mysql Alter命令 删除,添加或修改表字段 alter table student drop regi ...
- VBA学习
1. Range / Cells / Columns / Rows 2. 绝对引用 $F$13 / 相对引用 F13 公式所在单元格的被复制到其他位置时,绝对引用不变 3. VLookup / NLo ...
- php内部函数
strpos函数 /** haystack:被比较字串首地址(指向被比较字符串) needle:源字串首地址(指向源字符串) needle_len:源字符串长度 end:指向最后一个字符地址的下一个内 ...
- 第一章 Java多线程技能
1.初步了解"进程"."线程"."多线程" 说到多线程,大多都会联系到"进程"和"线程".那么这两者 ...
- window.open打开新窗口被浏览器拦截的处理方法
一般我们在打开页面的时候, 最常用的就是用<a>标签,如果是新窗口打开就价格target="_blank"属性就可以了, 如果只是刷新当前页面就用window.loca ...
- 浏览器内部工作原理--作者:Tali Garsiel
本篇内容为转载,主要用于个人学习使用,作者:Tali Garsiel 一.介绍 浏览器可以被认为是使用最广泛的软件,本文将介绍浏览器的工作原理,我们将看到,从你在地址栏输入google.com到你看到 ...
- Excel——MATCH函数
使用 MATCH 函数在范围单元格中搜索特定的项,然后返回该项在此区域中的相对位置. 1.参数说明: MATCH(lookup_value, lookup_array, [match_type]) l ...