简体   繁体   English

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

[英]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? 如果我想在JSF 2.0中创建多个托管bean实例,在同一范围内使用不同的名称,我该如何处理? Ideally, I want the equivilant to (for example): 理想情况下,我希望等效(例如):

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

}

Thanks .. 谢谢 ..

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. 你最好的选择是拥有一个父bean并将这些“多豆”作为孩子。

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

so that you can access it by #{parent.child1} and #{parent.child2} . 以便您可以通过#{parent.child1}#{parent.child2}访问它。 You can of course also use a List<Child> property or even Map<String, Child> instead to be more flexible. 您当然也可以使用List<Child>属性甚至Map<String, Child>来更灵活。

With the faces-config.xml it's however possible to define multiple bean classes with a different name. 但是使用faces-config.xml ,可以使用不同的名称定义多个bean类。 Still then, I don't see how that's useful. 然而,我还没有看到它是如何有用的。

In your case you should make use of the faces-config.xml. 在您的情况下,您应该使用faces-config.xml。 Implment your bean without the ManagedBean and RequestScope annotation. 在没有ManagedBean和RequestScope批注的情况下实现bean。 So your bean will not become a managed bean per default. 因此,默认情况下,您的bean不会成为托管bean。 You can than instance as much managedBeans as you need with different names, different scopes and at lease differnent properties. 您可以根据需要使用不同的名称,不同的范围和不同的属性来实例尽可能多的managedBeans。 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. 不要认为描述符是邪恶的,注释是实现代码的唯一方法。

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. 这也将帮助您分离未来的托管bean功能,这些功能实际上只涉及其中一种情况。

You will have to move the @ManagedBean (and scope) annotation to all the subclasses, regrettably, even though it is @Inherited. 令人遗憾的是,即使它是@Inherited,您也必须将@ManagedBean(和范围)注释移动到所有子类。 For the current version of Mojarra atleast, others I don't know. 对于目前版本的Mojarra至少,其他人我不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM