Angular.js前台传list数组由后台spring MVC接收数组示例代码
程序员文章站
2022-09-08 23:08:59
前言
本文主要给大家介绍了关于angular.js前台传list数组由后台spring mvc接收数组的相关内容,分享出来供大家参考学习,下面话不多说,来一起看看详细的介...
前言
本文主要给大家介绍了关于angular.js前台传list数组由后台spring mvc接收数组的相关内容,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍吧。
在开发中有时候需要在前台自定义对象,然后把对象封装在list中,在传送到后台,这样的思想也比较合理,直接来看示例代码:
1. 前台代码
$scope.savescore = function () { $scope.userscorelist = new array();//自定义数组 angular.foreach ($scope.records, function (record, index) { if (record.score != null) { $scope.userscoremodel = {'useranswerid': null,'score': null};//自定义对象结构 $scope.userscoremodel.useranswerid = record.useranswerid;//赋值 $scope.userscoremodel.score = record.score; $scope.userscorelist.push($scope.userscoremodel);//把对象封装在集合中 debugger; } }); if ($scope.userscorelist != null && $scope.userscorelist.length > 0) { var fd = new formdata();// 使用angularjs的formdata封装要传送的数据 var userscorerecords = angular.tojson($scope.userscorelist);//把对象(集合)转换为json串 fd.append('userscorerecords', userscorerecords);//参数放入formdata中 debugger;//使用 debugger模式查看传值情况 $http.post('/reviewprocess/save', fd, { //使用post方法 传送formdata对象 transformrequest: angular.identity, //使用angular传参认证 headers: { 'content-type': undefined //设置请求头 } }) .success(function (data){ toastr.success("success"); }) .error(function (data) { toastr.success("failed"); }); } };
2. 后台接收
@responsebody @requestmapping(value = "/reviewprocess/save", method = requestmethod.post) public void saveuserscore (@requestparam("userscorerecords") string userscorerecords) { //使用requestparam接收前台传送的json串 system.out.println(userscorerecords); objectmapper mapper = new objectmapper(); // 使用fastjson的objectmapper反序列化json串为对象 userscoremodel record = null; try { jsonarray jsonarray = new jsonarray (userscorerecords); //在后台把json串转换为json数组 for (int i =0; i < jsonarray.length(); i++) { record = mapper.readvalue(jsonarray.getjsonobject(i).tostring(), userscoremodel.class); //获取json数组的json对象并且反序列化为对应的对象 system.out.println(record); // 得到对象后后台即可操作 } } catch (exception e) { logger.error(e.getmessage(), e); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持