---
title: "struts2前台对象数组传递"
author: "chenchi1985"
date: "2010-05-18T19:53:42Z"
tags: []
categories: ["企业架构"]
slug: "struts2"
draft: false
lastmod: "2010-05-18T19:54:25Z"
id: 669275
url: "https://chenchi1985.iteye.com/blog/669275"
---

我知道的关于action接收前台的对象数组的方式有两种

1.在input标签中利name属性进行设置

```
<input name="studentList[0].name">
```

2.通过ajax来传递

 

```
            var billList = [];
            $('#data_tab tr').each(function(index, tr) {
                tr = $(tr);

                billList.push({
                    channelId : $('#channel option:selected').val(),
                    serviceId : tr.find('td:nth-child(1) a').attr('service_id'),
                    serviceName : tr.find('td:nth-child(1) a').html().trim(),
                    playTime : $('#playTime').val() + ' ' + tr.find('td:nth-child(2)').html() ,
                    duration : tr.find('td:nth-child(3)').html()
                });
            });

            var dataPara = { channel : $('#channel option:selected').val(),
                playTime:$('#playTime').val(),
                billList:billList
            };
            //            console.log(dataPara)
            $.ajax({type:'post',
                url:'playBillAction!save.action',
                data:dataPara,
                dataType:'json',
                success:function(rtn) {
                    //                    console.log(rtn);
                    if (rtn.opResult.isSuccessed) {
                        alert('操作已成功！');
                    } else {
                        alert('操作失败，请稍后再试！');
                    }
                }
            });
```

这里用的ajax库是jquery-1.4.2，但是在实际运行的时候，发现后台action的billList属性并没有被正确赋值。跟踪后发现，前台发到服务器的参数形式是这样的：billList\[0]\[channnelId]=1，这种方式struts2好像不支持，必须是billList\[0].channelId=1样式才行。

跟踪jquery的源代码后发现，这一步是在function buildParams( prefix, obj )中完成的，把对应部分改成：

 

```
else if ( !traditional && obj != null && typeof obj === "object" ) {
				// Serialize object item.
				jQuery.each( obj, function( k, v ) {
					buildParams( prefix + "." + k, v );
				});
					
			}
```

 然后就可以正常接收了。

附件是修改完成的jquery代码。