BalusC 是 100% 正确的,但是(正如他警告的那样)他的回答将毫无用处。这里重要的一点是,您根本不需要也不希望管理第二个 bean。这是您的模型,而不是您的 GUI。你可能想要这样的东西:
@ManagedBean
@ViewScoped
class PeopleHolder {
private List<Person> people = new ArrayList<Person>();
// not managed at all:
private Person currentPerson;
// just the getter, no need for a setter
public Person getCurrentPerson() { return currentPerson; }
@PostConstruct
public init(){ currentPerson = new Person(); }
public void addCurrentPersonToList() {
people.add(currentPerson);
init();
}
// just for test:
public List<People> getPeople() { return people; }
}
现在是一个表格:
<h:form>
<h:inputText value="#{peopleHolder.currentPerson.name}" />
<h:inputText value="#{peopleHolder.currentPerson.lastName}" />
<h:commandButton action="#{peopleHolder.addCurrentPersonToList}" />
</h:form>