如何发布复杂的对象struts2的列表复杂、对象、列表

2023-09-10 15:49:12 作者:╯別致旳柔弱

我想送User对象转化为行动的清单,而用户字段的名称和地址在这里。

I want to send a list of User object into action, while user has field name and address here.

有些code这样的:网页是动态的jQuery操纵。有样本$ C $网页℃。

Some code like this: The webpage is dynamic manipulated by jquery. there is sample code of webpage.

<form id="create">
  <tr id="1"> <td> <input type='text' name='name'/></td><td><input type='text' name='address'/></td></tr>
  <tr id="3"> <td> <input type='text' name='name'/></td><td><input type='text' name='address'/></td></tr>
</form>
<input type='submit' onclick="submit()"/>


<script>
  function submit() {
    var results = [];
    $("#create tr").each(function(index, tr) {
      var user = {
        name: tr.find('input[name="name"]').val(),
        address: tr.find('input[name="address"]').val()
      }
      results.push(user);
    });

    var param = {users:results};
    $.ajax({
      url: "save.action",
      data: param,
      type: 'post',
      success: function() {
        alert('success');  
      },
      error: function() {
        alert('error');  
      }
    });
  }
</script>

行动code是这样的:

Action code like this:

@ParentPackage('json-default')
public class UserAction extends ActionSupport {
  private List<User> users;
  public List<User> getUsers(){
     return users;
  }
  public void setUsers(List<User> users){
     this.users = users;
  }

@Action(name="save", results={@Result{name="success",location="/webpage/addUser.jsp"}})
public String execute(){
 for(User user: Users){
    System.out.println(user.getName()+" address: "+ user.getAddress());
 }
 return SUCCESS; 
}
}

我的问题是为什么动作不能接收数据? 我从萤火虫采集数据和数据已经公布。 因此,任何提示或什么事吗? 我两天时间被搞糊涂了,请大家帮忙。

My question is why the action can't receive the data? I capture the data from firebug and the data has been posted. So any hint or something wrong? I was confused here two days, please help.

推荐答案

如果您使用jQuery 1.4你应该考虑有一个关于参数序列化的变化。 jQuery提供了一个新的标志$就来覆盖默认行为(和回用参数序列化的老方法):在$就传统的选项,所以,你应该尝试的超过阿贾克斯JQuery的发送数据:

If you use jQuery 1.4 you should consider that there is a change about param serialization. JQuery provides a new flag in $.ajax to override the default behavior (and go back to use the old way of param serialization): the "traditional" option in $.ajax So you should try that to send data over Ajax at JQuery:

....
$.ajax({
    url:"save.action",
    data:param,
    type:'post',
    traditional: true,
....