从javaBean获取属性

时间:2013-01-03 13:28:44

标签: java jsf jsf-2 javabeans

我有一个applicationScoped bean,我初始化我的商店。

@ManagedBean(name="applicationBean", eager=true)
    @ApplicationScoped
    public class applicationBean implements Serializable {

     private Store store;

        public applicationBean() {
            store = new Store();
        }

        public Store getStore() {
            return store;
        }  

    }

现在我想把我的商店放在我的另一个Bean中。

@ManagedBean(name="productsBean")
@RequestScoped
public class ProductsBean implements Serializable {

    private List <Product> products;

    public ProductsBean(){

    }

}

如何从applicationScoped Bean调用getter?

1 个答案:

答案 0 :(得分:0)

将您的@ApplicationScoped bean作为托管属性注入ProductsBean。然后您就可以访问Store属性。

@ManagedBean(name="productsBean")
@RequestScoped
public class ProductsBean implements Serializable {

private List <Product> products;

@ManagedProperty(value="#{applicationBean}")
private applicationBean appBean;

public ProductsBean(){

}

}

请记住为该属性编写一个setter,以便JSF能够设置它。并且只是告诉你在Java语言中不鼓励使用以lowercases(applicationBean)开头的类名。