如何将值注入bean的子属性(没有注释)?

时间:2012-06-28 09:08:39

标签: jsf-2

我想问你是否可以为bean的子属性注入一个值。

这就是我想要做的,但Eclipse IDE抱怨:

在父类paq.paq.BeanA上找不到Bean属性beanB.subprop

<managed-bean>
    <managed-bean-name>beanA</managed-bean-name>
    <managed-bean-class>paq.paq.BeanA</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
            <property-name>beanB.subprop</property-name>
            <value>Hello!</value>
        </managed-property>        
</managed-bean>



public class BeanA implements Serializable {

    public BeanB beanB = null;      

    public Skin getBeanB() {
        if (this.beanB == null) {
            this.beanB = new BeanB();
        }
        return this.beanB;
    }       

    public void setBeanB(BeanB beanB) {     
        this.beanB = beanB;
    }

}


public class BeanB implements Serializable {

    public String subprop = null;       

    public String getSubprop() {
        return this.subprop;
    }       

    public void setSubprop(String subprop) {        
        this.subprop = subprop;
    }

}   

谢谢!

1 个答案:

答案 0 :(得分:0)

每个嵌套bean也需要声明为托管bean。您需要将它注入嵌套bean,然后将嵌套bean本身注入主bean。

<managed-bean>
    <managed-bean-name>beanA</managed-bean-name>
    <managed-bean-class>paq.paq.BeanA</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>beanB</property-name>
        <value>#{beanB}</value>
    </managed-property>        
</managed-bean>
<managed-bean>
    <managed-bean-name>beanB</managed-bean-name>
    <managed-bean-class>paq.paq.BeanB</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>subprop</property-name>
        <value>Hello!</value>
    </managed-property>        
</managed-bean>