我有一个applicationScoped bean,我在其中初始化我的Store。
@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 ?
只需将您的@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语言中不鼓励以小写字母开头的类名(applicationBean)。