gpt4 book ai didi

java - 在 JSF 中使用 Bean(View Scope)显示数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:51 30 4
gpt4 key购买 nike

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

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

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

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 .

关于java - 在 JSF 中使用 Bean(View Scope)显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7077047/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com