开发者

Using Bean (View Scope) in JSF to show data

开发者 https://www.devze.com 2023-03-28 22:27 出处:网络
I\'m creating an web application, using JSF (2.0). It has \"ViewProducts.xhtml\" to view Product with page. Each time this page loaded, if parameter has some thing (Eg: page=1 (ViewProduct.xhtml?page=

I'm creating an web application, using JSF (2.0). It has "ViewProducts.xhtml" to view Product with page. Each time this page loaded, if parameter has some thing (Eg: page=1 (ViewProduct.xhtml?page=1)), it's will automatically set the id to setPage property in that Bean. But, i'm keeping getting this error:

Unable to create managed bean categories. The following problems were found: - Bean or property class bean.Categories for managed bean categories cannot be found.

Here is my code (Categories act like a product container):

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;
    }开发者_JAVA百科
}

Stack trace:

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)


There are at least 3 problems:

  1. The bean class bean.Categories is not in the classpath.

  2. You cannot have a managed property which is of a narrower scope than the managed bean.

  3. You're duplicating the managed property with <f:viewParam>.

Ensure that the bean class is in the classpath and that you didn't typo'ed the managed bean class. You also need to get rid of the <managed-property>, you don't need it if you're already using <f:viewParam>.

Not related to the problem, but as you're already on JSF2, I'd also suggest to use annotations instead of the faces-config.xml.

0

精彩评论

暂无评论...
验证码 换一张
取 消