将复杂数据从angularjs控制器传递给Spring MVC控制器

时间:2017-01-07 04:35:20

标签: java angularjs spring-mvc model-view-controller

我有一个包含以下部分的表格(以下示例是为了理解目的)

GeneralInformation - 它是一个具有Cityname(String)和population(int)

的对象

位置信息:它是locationCode(int)和最近的HospitalName(String)

的对象

公司:它是公司详细信息的对象。有动态添加的公司列表            以公司为对象。基本上列出

医院:它就像List

// generalInfo - 从表单

填充

// locationInfo - 从表单

填充

// companiesArr [] //这是动态人口(每个对象的每一行)公司数组

// hospitalArr [] // //这是动态人口(每个对象每行)医院阵列

// Angular代码开始.. 控制器(' 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);


        });

// Angular代码结束..

     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控制器?

3 个答案:

答案 0 :(得分:0)

请分享从浏览器发送的请求以发表评论

当然看起来你正在发送DataInfo对象但是正在接收 列出控制器中的dataInfo。存在不匹配。

更改处理程序方法的签名

to public @ResponseBody String addGeneralData(@RequestBody DataInfo dataInfo)

答案 1 :(得分:0)

你有任何例外吗?
由于List接口作为参数传递给控制器​​,因此很可能会出现序列化异常。 Spring无法初始化List的新实例。尝试使用数组而不是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对象中的接口。 希望它有所帮助

答案 2 :(得分:0)

感谢您的回复。当我更改为数组而不是列表时,它已经工作了。我已将Data对象内的所有列表也更改为数组。除此之外,确保从输入传递的所有数据都是按照具体对象中提到的类型。例如,任何提到int的数据,请确保它只传递int。如果它是复杂的形式,并且在输入验证之前我们正在将前端与后端集成,请确保我们传递的所有数据与映射对象中提到的类型完全一致。在MVC控制器中使用数组作为参数是不错的做法?