【问题标题】:pass complex data from angularjs controller to Spring MVC controller将复杂数据从 angularjs 控制器传递到 Spring MVC 控制器
【发布时间】:2017-05-21 22:20:54
【问题描述】:

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

GeneralInformation - 它是一个带有 Cityname (String) 和 population(int) 的对象

位置信息:它是一个带有 locationCode (int) 和 Nearest HospitalName(String) 的对象

Companies:这是一个带有公司详细信息的对象。有动态添加的公司列表 以公司为对象。基本列表

医院:就像列表

// generalInfo - 从表单填充

//locationInfo - 从表单填充

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

// hospitalArr[] // // 这是动态填充的(每行每个对象) Hospitals 数组

//Angular 代码开始.. 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);


        });

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

【问题讨论】:

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


    【解决方案1】:

    请分享浏览器发送的请求以进行更多评论

    看起来您确实在发送 DataInfo 对象但正在接收 在您的控制器中列出 dataInfo。不匹配。

    更改处理程序方法的签名

    公开@ResponseBody String addGeneralData(@RequestBody DataInfo dataInfo)

    【讨论】:

    • 这也是正确的。基本上使用数组而不是 List 接口。确保所有嵌套数据都是数组而不是列表/任何其他接口
    【解决方案2】:

    你有什么例外吗?
    由于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 对象中的接口。 希望对你有帮助

    【讨论】:

    • 在上面的代码中将所有列表更改为数组,然后它将起作用
    【解决方案3】:

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

    【讨论】:

    • 在我看来,数组没有问题。您也可以使用任何其他类型,只要您确定 spring 可以使用非参数构造函数正确初始化它。您使用了无法初始化的接口。后端验证几乎是开箱即用的。 Spring 抛出 BindingException 或 MethodArgumentNotValidException,您可以在控制器建议中捕获它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2019-05-13
    • 2017-03-16
    • 1970-01-01
    相关资源
    最近更新 更多