struts2提供了从页面直接封装到List的功能,实现起来有两个办法
1.使用泛型定义集合中的对象类型
2.配合配置文件定义集合中的对象类型
User.java

package HelloWorld;

public class User
...{

private String username;

private String password;

private String[] books;

public String[] getBooks()
...{

return books;

}

public void setBooks(String[] books)
...{

this.books = books;

}

public String getUsername()
...{

return username;

}

public void setUsername(String username)
...{

this.username = username;

}

public String getPassword()
...{

return password;

}

public void setPassword(String password)
...{

this.password = password;

}

}
LoginAction.java (不使用泛型,使用配置文件定义集合中对象的类型)
LoginAction_conversion.properties (放在和LoginAction同目录)
Element_users=HelloWorld.User
表示集合中的Element类型是User,如果是Map类型,则还需要定义Key_users=****
index.jsp

<%
...@ page language="java" contentType="text/html; charset=gb2312"

pageEncoding="gb2312"%>

<%
...@ taglib prefix="s" uri="/struts-tags" %>

<html>

<head>

<title></title>

</head>

<body>

<s:form action="Login">

<s:textfield name="users[0].username" key="username"></s:textfield>

<s:textfield name="users[0].password" key="password"></s:textfield>

<s:textfield name="users[1].username" key="username"></s:textfield>

<s:textfield name="users[1].password" key="password"></s:textfield>

<s:submit value="login"></s:submit>

</s:form>

</body>

</html>
如果使用泛型定义List users,则可以省略LoginAction-conversion.properties文件
private List<List> users