ITeye博客

struts2前台对象数组传递

chenchi1985 · 2010-05-18 19:53 · 企业架构 · Markdown

我知道的关于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代码。

评论(2)

  1. #2 楼 · happyboyfwh · 2011-10-26 09:49

    正好需要这个功能,非常感谢!

  2. #1 楼 · shanjing · 2011-08-20 13:40

    不评论对不起良心,真的,找了好久,多谢

评论功能维护中,当前仅展示历史评论。