2013-01-03 94 views
1

我有一個applicationScoped bean,用於初始化我的Store。從javaBean獲取屬性

@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?

回答

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語言不鼓勵以lowercase(applicationBean)開頭的類名。