如何将 JSF 2 托管 bean 的多个实例添加到 Java Collection?

发布于 2024-11-30 08:27:21 字数 407 浏览 0 评论 0原文

我正在尝试了解一些基本的 JSF 2 概念。例如,如果我有一个托管 bean Bean1:,

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

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

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

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

I'm trying to wrap my head around some basic JSF 2 concepts. For instance, if I have a managed bean, Bean1:

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

and the values for foo and bar are obtained from a JSF web form. On each submit of the web form, I want to store an instance of Bean1 in a Java Collection of another bean:

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

What is the correct way to achieve this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风夜微凉 2024-12-07 08:27:21

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>

BalusC is 100% per cent right, but (as he warns) his answer will be useless. The important point here is that you do not need nor want the second bean to be managed at all. It is your model, not your GUI. You probably wanted something like:

@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; }
}

and now a form:

<h:form>
   <h:inputText value="#{peopleHolder.currentPerson.name}" />
   <h:inputText value="#{peopleHolder.currentPerson.lastName}" />

   <h:commandButton action="#{peopleHolder.addCurrentPersonToList}" />
</h:form>
若有似无的小暗淡 2024-12-07 08:27:21

Bean2 设为 Bean1 的托管属性,以便您可以访问其 beanList 属性。

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

    @ManagedProperty("#{bean2}")
    private Bean2 bean2;

    public void submit() {
        bean2.getBeanList().add(this);
        // ...
    }

    // ...
}

(请注意,这种方式仅存储引用,而不是 Bean1 状态或其他内容的克隆!)

不用说,这是一种设计味道。可能有更好的方法来实现您在提出问题时想到的具体功能要求,但没有透露任何内容。以后尝试问如何解决功能需求,而不是如何实现解决方案(这可能不是正确的解决方案)。

Make Bean2 a managed property of Bean1 so that you have access to its beanList property.

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

    @ManagedProperty("#{bean2}")
    private Bean2 bean2;

    public void submit() {
        bean2.getBeanList().add(this);
        // ...
    }

    // ...
}

(please note that this way just the reference is stored, not a clone of the Bean1's state or something!)

Needless to say that this is a design smell. There are likely better ways to achieve the concrete functional requirement which you've had in mind while asking the question but didn't tell anything about. In the future try to ask how to solve the functional requirement instead of how to achieve a solution (which may not be the right solution after all).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文