springmvc 传递对象数组参数 property path is neither an array nor a List nor a Map

Spring MVC 3: Property referenced in indexed property path is neither an array nor a List nor a Map

 
JQuery's $.ajax does an excellent job mapping a json object to parameters, but when you start getting into more complex objects Spring MVC doesn't know how to interpret it sometimes.

For example if you have a json object like:

{
   "beans":[
      {
         "agreemendId":1,
         "answerId":2
      }
   ]
}

springmvc中的接收参数如下:

@ResponseBody

public Result update(@ModelAttribute Role dataList) {。。。}

 

 

其中,Role,Bean的定义:

public class Role{

 

  List<Bean> beans;

 

  public List<Bean> getBeans(){ return beans;}

  public void setBeans(List<Bean> beans){this.beans=beans;}  

}

 

public class Bean{

  private Integer agreemendId;

  private Integer answerId;

  

  set....

  get....

 

}

注意到,ajax中提交的参数beans与Role类中的属性beans必需一致。

JQuery will map your parameters like 

beans[0][agreementId]:1
beans[0][answerId]:2

The problem is that Spring MVC is expecting a parameter format like 

beans[0].agreementId:1
beans[0].answerId:2

In order to get that you can do it in 1 of two ways. You can do the quick and dirty way, which changes the way you're building your json object. Or, the other way is to extend the jQuery plugin to build parameters differently. 

To change the javascript code was pretty simple and looked something like this 

var answers = {};
answers['beans[' + index +'].agreementId'] = agreementId;
answers['beans[' + index +'].answerId'] = value;
 

$.ajax({

            url:url,

            data: answers ,

            dataType:'json',

            type:'post',

            success:function(data){

                console.log(data)

            }

        })

 
To modify the jquery plugin I would suggest taking a  look here.

And for reference here are the pojos I was mapping to.

转载于:https://www.cnblogs.com/pautcher/p/6075422.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值