托管Bean未构建 [英] Managed bean is not constructed

查看:86
本文介绍了托管Bean未构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击页面时,没有构建我的JSF托管bean.

My JSF managed bean is not constructed when I hit the page.

这是我的面孔:

            <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>

这是我的ProductsBean:

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);
            }

        }
....

当我使用#{productsBean.producten}"时,我的JavaBean应该初始化了,但是没有初始化. 当我调试代码时,我没有到达构造函数.

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.

推荐答案

我仍然看到原始的JSF源代码.

您的HTTP请求根本没有命中FacesServlet.它是负责执行所有JSF工作(例如创建托管bean和生成HTML)的人.

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.

您应该确保您的HTTP请求URL与在webapp的web.xml中配置的FacesServlet<url-pattern>相匹配.例如,如果是*.jsf,则应通过/products.jsf而不是/products.xhtml打开页面.

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.

或者,您也可以将FacesServlet<url-pattern>更改为*.xhtml,这样您就无需摆弄虚拟URL.以前,在JSF 1.x中,它经常以一个无限循环结束,每次都调用它,但是由于JSF 2.x,这种情况不再发生,应该可以正常工作.

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>

另请参见:

  • 我们的JSF Wiki页面-包含一个Hello World和指向明智教程的几个链接
  • JSF Facelets:有时我看到URL是.jsf,有时是.xhtml.为什么?
  • 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?
    • 这篇关于托管Bean未构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆