當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
angularJS前台传list数组,后台springMVC接收数组
生活随笔
收集整理的這篇文章主要介紹了
angularJS前台传list数组,后台springMVC接收数组
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有時候需要在前臺自定義對象,然后把對象封裝在list中,在傳送到后臺,這樣的思想也比較合理
1. 前臺代碼
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | $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. 后臺接收
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @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); ??????} ??} |
轉載于:https://www.cnblogs.com/Jeremy2001/p/6824923.html
總結
以上是生活随笔為你收集整理的angularJS前台传list数组,后台springMVC接收数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 科目二倒车入库不论怎么都能入进去的方法
- 下一篇: Android开发简易教程