Whats the correct way to create multiple instances of managed beans in JSF 2.0

后端 未结 3 2043
别跟我提以往
别跟我提以往 2020-12-18 03:20

If I want to create more than one instance of managed bean in JSF 2.0, under different names in the same scope, how should I proceed? Ideally, I want the equivilant to (for

3条回答
  •  感动是毒
    2020-12-18 04:00

    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.

    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;
        // ...
    }
    

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

    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.

提交回复
热议问题