2013-01-03 72 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)开头的类名。