如何将 JSF 2 托管 bean 的多个实例添加到 Java Collection?
我正在尝试了解一些基本的 JSF 2 概念。例如,如果我有一个托管 bean Bean1:,
@ManagedBean
public class Bean1 {
private String foo;
private String bar;
}
则 foo
和 bar
的值是从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
BalusC 是 100%正确的,但(正如他警告的那样)他的答案将毫无用处。这里重要的一点是您根本不需要也不希望管理第二个 bean。这是你的模型,而不是你的 GUI。您可能想要类似的东西:
现在是一个表格:
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:
and now a form:
将
Bean2
设为Bean1
的托管属性,以便您可以访问其beanList
属性。(请注意,这种方式仅存储引用,而不是
Bean1
状态或其他内容的克隆!)不用说,这是一种设计味道。可能有更好的方法来实现您在提出问题时想到的具体功能要求,但没有透露任何内容。以后尝试问如何解决功能需求,而不是如何实现解决方案(这可能不是正确的解决方案)。
Make
Bean2
a managed property ofBean1
so that you have access to itsbeanList
property.(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).