Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

jsf - Managed bean is not constructed

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...