问题
My JSF managed bean is not constructed when I hit the page.
This is my facelet:
<h:dataTable value="#{productsBean.producten}" var="product">
<h:column>#{product.description}</h:column>
<h:column>#{product.price}</h:column>
<h:column>#{product.categoryName}</h:column>
<h:column>
<h:link value="Edit" outcome="/products/edit">
<f:param name="id" value="#{product.product_id}"/>
</h:link>
</h:column>
</h:dataTable>
This is my ProductsBean:
@ManagedBean(eager=true)
@RequestScoped
public class ProductsBean implements Serializable{
private List<ProductBean> producten; //+getter
@ManagedProperty(value = "#{applicationBean}")
private ApplicationBean applicationBean;
public ProductsBean() {
Store store = applicationBean.getStore();
for (String c : store.getCategories()) {
for(be.kdg.shop.model.stock.Product p : store.getProductsOfCategory(c)){
ProductBean product = new ProductBean();
product.setProduct_id(p.getProduct_id());
product.setDescription(p.getDescription());
product.setCategoryName(p.getCategoryName());
product.setPrice(p.getPrice());
producten.add(product);
}
}
....
When I use "#{productsBean.producten}" my JavaBean should my initialized but it doesn't. When I debug my code i doesn't reach the constructor.
回答1:
I see still raw JSF source code.
Your HTTP request did not hit the FacesServlet
at all. It's the one responsible for performing all the JSF works such as creating managed beans and generating HTML.
You should make sure that your HTTP request URL matches the <url-pattern>
of the FacesServlet
as configured in webapp's web.xml
. If it is for example *.jsf
, then you should open the page by /products.jsf
instead of /products.xhtml
.
Alternatively, you can also just change the <url-pattern>
of the FacesServlet
to *.xhtml
, so that you never need to fiddle with virtual URLs. Previously in JSF 1.x this used to end up in an infinite loop calling itself everytime, but since JSF 2.x this does not occur anymore and should work fine.
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
See also:
- Our JSF wiki page - contains a Hello World and several links to sane tutorials
- JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?
回答2:
First of all, (eager=true) only works with @ApplicationScoped
managed beans and means that bean will be created when application initializes, so in this case you should remove it.
I suggest you to check that producten attribute has a getter method, because you don't specify that in your code. Also you can try with other scope like @ViewScoped
.
来源:https://stackoverflow.com/questions/14154813/managed-bean-is-not-constructed