簡體   English   中英

將復雜的數據從angularjs控制器傳遞到Spring MVC控制器

[英]pass complex data from angularjs controller to Spring MVC controller

我有一個包含以下各節的表格(以下示例用於理解)

GeneralInformation-它是具有城市名稱(字符串)和人口(int)的對象

位置信息:這是一個具有locationCode(int)和Nearest HospitalName(String)的對象

公司:這是帶有公司詳細信息的對象。 有以Company為對象動態添加的公司列表。 基本上清單

醫院:就像清單

// generalInfo-從表單填充

// locationInfo-從表單填充

// companiesArr [] //這是動態填充的(每個對象每一行)companies數組

// hospitalsArr [] // //這是動態填充的(每個對象每一行)Hospitals數組

//開始角代碼。.controller('addGeneralController',function($ scope,close,Service){

        $scope.companiesArr = [];
        $scope.comapnyName='';
        $scope.companyType='';


        $scope.hospitalsArr = [];
        $scope.hospitalName='';
        $scope.locationCode='';



        $scope.generalInfo = {};
        $scope.locationInfo = {};
        $scope.companies = {};
        $scope.hospitals = {};

        $scope.dataInfo = {};//this is to carry entire objects and arrays

       //Following method calls after populating data from form and submit.
       //companiesArr,hospitalsArr are populated from front end and passing as submission parameters

        $scope.saveGeneral = functio(generalInfo,locationInfo,companiesArr,hospitalsArr){ 

            $scope.companies = companiesArr;

            $scope.hospitals = hospitalsArr;

            //Create an empty array
            //$scope.dataInfo = [];

            $scope.dataInfo.push({'generalInfo' : generalInfo, 'locationInfo' : locationInfo,'companies' : $scope.companies,'hospitals' : $scope.hospitals});

    $http.post("/addGeneralData",$scope.dataInfo);


        });

//角度代碼結束

     It's not reaching to the following Spring MVC method:

    @RequestMapping(value = "/addGeneralData", method = RequestMethod.POST)
        public @ResponseBody String addGeneralData(@RequestBody List<Data> dataInfo){

           // not reaching here.With simple parametrs it's reaching here, so no other mapping issue apart from this complex data
          // Data - is an object with  generalInfo as object,locationInfo as object,                //companies List ,hospitals List as it's attributes.

        Data data  = dataInfo.get(0);
            GeneralInfo generalInfo = data.getgeneralInfo();
            LocationInfo locationInfo = data.getLocationInfo();
            List<Company> companies = data.getCompanies();
            List<Hospital> hospitals = data.getHospitals();


    }

基本上我想知道如何將這種復雜的數據從角度控制器傳輸到Spring MVC控制器?

請分享瀏覽器發送的請求以發表評論

當然,您似乎正在發送DataInfo對象,但在控制器中接收到List dataInfo。 不匹配。

更改處理程序方法的簽名

公開@ResponseBody字符串addGeneralData(@RequestBody DataInfo dataInfo)

你有例外嗎?
由於將List接口作為參數傳遞給控制器​​,很可能會出現序列化異常。 Spring不能初始化List新實例。 嘗試使用數組而不是列表。
例如

 @RequestMapping(value = "/addGeneralData", method = RequestMethod.POST)
    public @ResponseBody String addGeneralData(@RequestBody Data[] dataInfo){
        Data data  = dataInfo[0];
        GeneralInfo generalInfo = data.getgeneralInfo();
        LocationInfo locationInfo = data.getLocationInfo();
        Company[] companies = data.getCompanies();
        Hospital[] hospitals = data.getHospitals();


}

確保使用具體的實現,而不是Data對象中的接口。 希望能幫助到你

感謝您的回復。當我更改為數組而不是列表時,此方法有效。 我已經將Data對象內的所有列表也更改為數組。 除此之外,還要確保從輸入傳遞的所有數據都與具體對象中提到的類型相同。 例如,任何提及為int的數據,請確保僅傳遞int。 如果是復雜形式,並且在輸入驗證之前將前端與后端集成在一起,請確保傳遞的所有數據與映射對象中提到的類型完全相同。在MVC控制器中使用數組作為參數是否是一種好習慣?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM