Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 example):

@ManagedBeans({name="myManagedBean1",name="myManagedBean2"})
@RequestScoped
public class MyManagedBean {

}

Thanks ..

like image 502
Dave Avatar asked Sep 14 '10 14:09

Dave


People also ask

What is managed bean in JSF?

Managed Bean is a regular Java Bean class registered with JSF. In other words, Managed Beans is a Java bean managed by JSF framework. Managed bean contains the getter and setter methods, business logic, or even a backing bean (a bean contains all the HTML form value). Managed beans works as Model for UI component.

Which scope is used on managed beans which is created for every session?

Using Managed Bean Scopes. You can use annotations to define the scope in which the bean will be stored. You can specify one of the following scopes for a bean class: Application (@ApplicationScoped): Application scope persists across all users' interactions with a web application.


2 Answers

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.

like image 152
BalusC Avatar answered Oct 14 '22 10:10

BalusC


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.

like image 42
Ralph Avatar answered Oct 14 '22 08:10

Ralph