1
votes

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.

2
Rightclick page in browser and do View Source. What do you see? Do you see JSF-generated HTML code, or do you still see raw JSF source code?BalusC
I see still raw JSF source code.user1939400
I posted an answer. I've also edited your overly generic term "javabean" in the question to be the more JSF-specific "managed bean". There are a lot of different kinds of javabeans in the average Java EE web application.BalusC

2 Answers

1
votes

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:

0
votes

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.