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

后端 未结 3 2024
别跟我提以往
别跟我提以往 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 03:53

    One possibility is to make your class abstract and subclass it into as many named instances as you need, which you may leave empty. This will also help you separate future managed bean functionality which really only concerns one of the cases.

    You will have to move the @ManagedBean (and scope) annotation to all the subclasses, regrettably, even though it is @Inherited. For the current version of Mojarra atleast, others I don't know.

    0 讨论(0)
  • 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<Child> property or even Map<String, Child> 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.

    0 讨论(0)
  • 2020-12-18 04:02

    In your case you should make use of the faces-config.xml. Implment your bean without the ManagedBean and RequestScope annotation. So your bean will not become a managed bean per default. You can than instance as much managedBeans as you need with different names, different scopes and at lease differnent properties. For example:

        <managed-bean>
        <managed-bean-name>MyManagedBean1</managed-bean-name>
        <managed-bean-class>org.MyManagedBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
            <property-name>value1</property-name>
            <property-class>int</property-class>
            <value>5</value>
        </managed-property>
        <managed-property>
            <property-name>value2</property-name>
            <property-class>int</property-class>
            <value>2</value>
        </managed-property>
    </managed-bean>
    
    <managed-bean>
        <managed-bean-name>MyManagedBean2</managed-bean-name>
        <managed-bean-class>org.MyManagedBean</managed-bean-class>
        <managed-bean-scope>view</managed-bean-scope>
        <managed-property>
            <property-name>value1</property-name>
            <property-class>int</property-class>
            <value>30</value>
        </managed-property>
        <managed-property>
            <property-name>value2</property-name>
            <property-class>java.lang.String</property-class>
            <value>project</value>
        </managed-property>
    </managed-bean>
    

    Don't think that descriptors are evil and annotations are the only way to implement your code.

    0 讨论(0)
提交回复
热议问题