日期:2014-05-18  浏览次数:20693 次

Struts2 前后台传数据
Action中定义一个List 里面放了十个对象。


public class testAction  extends ActionSupport{
public List<User>  testList=  new ArrayList<User>();

public String execute(){
for(int i = 0;i<10;i++){
testList.add(new User());
}
return "SUCCESS";
}

public List<User> getTestList() {
return testList;
}

public void setTestList(List<User> testList) {
this.testList = testList;
}

}




jsp中定义一个表格,想将textfield 于Action中的List绑定。当页面输入值时提交表单后在后台Action中可以接收到一个有值(界面输入的值)的List。但是jsp中像这样写:[name = "info.login"]不能绑定到list中请教应该怎么写。


<table class="Checkbox_table_2">
<tr>
<td >Name</td>
<td>Password</td>
</tr>
<s:iterator value = "testList" id = "info">
<tr>
<td><s:textfield  name = "info.login"></s:textfield  ></td>
<td><s:textfield name = "info.password"></s:textfield ></td>
</tr>
</s:iterator>


</table>


界面点OK希望后台Action的List可以获取输入的值



------解决方案--------------------
ognl集合数组的赋值方式

<s:iterator value = "testList" status="st">
    <tr>
      <td><s:textfield name = "testList[#st.index].name"></s:textfield></td>
      <td><s:textfield name = "testList[#st.index].password"></s:textfield ></td> 
    </tr>
</s:iterator>