且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在JSF 2.0中创建托管Bean的多个实例的正确方法是什么?

更新时间:2023-10-06 18:07:16

您不能.从技术上讲,它也没有多大意义.您可能正在为特定功能需求寻找错误方向的解决方案.

You can't. It technically also doesn't make much sense. You're probably looking for a solution in the wrong direction for the particular functional requirement.

您***的选择是拥有一个父bean,并将那些多个bean"作为孩子.

Your best bet is to have a parent bean and have those "multiple beans" as children.

@ManagedBean
@RequestScoped
public class Parent {
    private Child child1;
    private Child child2;
    // ...
}

,以便您可以通过#{parent.child1}#{parent.child2}访问它.当然,您也可以使用List<Child>属性,甚至使用Map<String, Child>来提高灵活性.

so that you can access it by #{parent.child1} and #{parent.child2}. You can of course also use a List<Child> property or even Map<String, Child> instead to be more flexible.

使用faces-config.xml可以定义多个具有不同名称的bean类.尽管如此,我仍然看不到它的用处.

With the faces-config.xml it's however possible to define multiple bean classes with a different name. Still then, I don't see how that's useful.