springboot前端向后端传递复杂对象如list set的接收问题
转自:https://blog.csdn.net/qq_27093465/article/details/52094112
https://www.cnblogs.com/yfzhou/p/9661994.html
spring MVC后台处理数组对象。
list类型的参数,接收前台的数组值,实验了一下,结果还真可以。
不用绑定到对象里面。
当然我这个是前台传递了个包含的是string的数组到后台,然后,后台用list来接收。
具体如下:
前台代码:
//发送请求到后台,带数组形式的数据。
function testList() {
var data = getTreeViewCheckedData();
$.ajax({
url: APP_NAME + "xxxx/testList",
data: {
list:data
},
dataType: "json",
success: function (data) {
}
});
}
//获得数据---以数组的形式存储
function getTreeViewCheckedData() {
var checkedData = [];
$('#shareSetting').find('ol.bonsai input:checkbox:checked').each(function () {
checkedData.push($(this).val())
}
);
return checkedData;
}
后台代码:
//方式一,用list接收前台的数组参数。
@RequestMapping(value = "/testList")
@ResponseBody
public JsonResult testList(@RequestParam(required = false, value = "list[]") List<String> list){
LOG.debug("---------------XxxxController: testList---------------");
LOG.debug("---------------list:\t" + list);
return new JsonResult(true,"ok ",null);
}
//方式二,用数组接收前台的数组参数。然后转化成list,虽然没必要。
@RequestMapping(value = "/testList")
@ResponseBody
public JsonResult testList(@RequestParam(required = false, value = "list[]") String[] list){
LOG.debug("---------------XxxxController: testList---------------");
LOG.debug("---------------list:\t" + list);
List<String> newList = Lists.newArrayList(Arrays.asList(list));
return new JsonResult(true,"ok ",null);
}
注意的地方:
value = "list[],这个好像不能省,
另外你要是修改方法上的参数的话,就得重启一下,不然得不到值
这个list要和前台的数据一模一样。
————————————————
版权声明:本文为CSDN博主「李学凯」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_27093465/article/details/52094112
前端JS
1 var taskList = ["123","456"]; 2 var params = { 3 taskList: taskList 4 }; 5 6 $.ajax({ 7 type: "PUT", 8 dataType: "json", 9 url: "/client/update", 10 data: params, 11 success: function (msg) { 12 } 13 });
后端接收:
1 @RequestMapping(value = "/update", method = RequestMethod.PUT) 2 @ResponseBody 3 public JSONResult updateClient(Client client, @RequestParam(value = "taskList[]") List<String> taskList) { 4 logger.debug("Yufan taskList={}", taskList); 5 return JSONResult.ok(); 6 }
参考文献:spring MVC 前台传数组类型,后台用list类型接收也是可以的
相关文章
- Springboot分模块开发详解(2):建立子工程
- springboot整合shiro的认证与授权流程
- springboot之HttpSessionListener的用法创建,销毁
- spring cloud 自定义ribbon实现负载均衡(二) 解决自定义ribbon和springboot启动类放在同一包下
- 精品基于springboot+mybatis的在线基金管理平台vue
- 第二十四章 springboot注入servlet
- springboot配置swagger-rest文档
- springboot整合freemarker(转)
- mybatis-plus springboot配置xml文件不在resources文件夹下的解决方法--Invalid bound statement (not found): com.xx..mapper.xxMapper.xx
- Springboot LIST进行分割方法
- 将图片上传到linux服务器(通过Springboot)
- SpringBoot 集成 WebSocket,实现后台向前端推送信息
- SpringBoot 之Spring Boot Starter依赖包及作用
- SpringBoot系列六:SpringBoot整合Tomcat
- springboot 整合使用redis发布订阅功能
- SpringBoot基础之MockMvc单元测试
- 在SpringBoot中使用FluentValidator验证插件
- SpringBoot+Thyemleaf开发环境正常,打包jar发到服务器就报错Template might not exist or might not be accessible