在JSF中使用Bean(视图范围)来显示数据



我正在创建一个web应用程序,使用JSF (2.0)。它有"ViewProducts.xhtml"来查看产品与页面。每次加载此页面时,如果参数有一些东西(例如:page=1 (ViewProduct.xhtml?page=1)),它将自动将id设置为该Bean中的setPage属性。但是,我一直得到这个错误:

无法创建托管bean 类别。发现了以下问题:—Bean或属性类Bean。无法找到托管bean类别的类别。

下面是我的代码(类别就像一个产品容器):

faces-config.xml:

<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<navigation-rule>
    <from-view-id>/template/header.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>ViewCategories</from-outcome>
        <to-view-id>/ViewCategories.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>
<managed-bean>
    <managed-bean-name>categories</managed-bean-name>
    <managed-bean-class>bean.Categories</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
</managed-bean>

ViewProducts.xhtml

xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"

<f:metadata>
    <f:viewParam name="page" value="#{categories.page}"/>
</f:metadata>
<h:dataTable value="#{categories.listProduct}" var="cus">
    <h:column>
        <f:facet name="header">Name</f:facet>
        <h:outputText value ="#{cus.name}"></h:outputText>
    </h:column>
    <h:column>
        <f:facet name="header">Price</f:facet>
        <h:outputText value ="#{cus.price}"></h:outputText>
    </h:column>
</h:dataTable>

Categories.java (ManagedBean)

public class Categories implements Serializable {    
    /** Creates a new instance of categories */
    public Categories() {
    }
    private int page = 0;
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    public List<Product> listProduct = null;
    public List<Product> getListProduct() {
        if (listProduct != null) {
            return listProduct;
        } else {
            listProduct = dataAccess.DataAccess.getCategories(this.page);
            return listProduct;
        }
    }
    public void setListProduct(List<Product> listProduct) {
        this.listProduct = listProduct;
    }
}

堆栈跟踪:

com.sun.faces.mgbean.ManagedBeanCreationException: Unable to create managed bean categories.  The following problems were found:
     - Bean or property class bean.Categories for managed bean categories cannot be found.
     - Bean or property class bean.Categories for managed bean categories cannot be found.
     - Bean or property class bean.Categories for managed bean categories cannot be found.
     - Bean or property class bean.Categories for managed bean categories cannot be found.
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:263)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:86)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
    at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:69)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:112)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
    at javax.faces.component.UIData.getValue(UIData.java:554)
    at javax.faces.component.UIData.getDataModel(UIData.java:1248)
    at javax.faces.component.UIData.setRowIndex(UIData.java:447)
    at com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
    at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:823)
    at javax.faces.component.UIData.encodeBegin(UIData.java:937)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1611)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

至少有3个问题:

  1. bean类bean.Categories不在类路径中

  2. 托管属性的作用域不能小于托管bean。

  3. 您正在与<f:viewParam>复制托管属性。

确保bean类位于类路径中,并且您没有键入托管bean类。你还需要摆脱<managed-property>,如果你已经在使用<f:viewParam>,你不需要它。

与问题无关,但由于您已经在JSF2上,我还建议使用注释而不是faces-config.xml

相关内容

  • 没有找到相关文章

最新更新