gpt4 book ai didi

jsf - 如何将 JSF 2 托管 bean 的多个实例添加到 Java 集合?

转载 作者:行者123 更新时间:2023-12-04 14:38:35 25 4
gpt4 key购买 nike

我正在努力思考一些基本的 JSF 2 概念。例如,如果我有一个托管 bean,Bean1:

@ManagedBean
public class Bean1 {
private String foo;
private String bar;
}

foobar 的值是从 JSF 网络表单中获取的。在每次提交 Web 表单时,我想将 Bean1 的实例存储在另一个 bean 的 Java 集合中:

@ManagedBean
public class Bean2 {
private List<Bean1> beanList;
}

实现此目标的正确方法是什么?谢谢。

最佳答案

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>

关于jsf - 如何将 JSF 2 托管 bean 的多个实例添加到 Java 集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7096983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com