我遇到了DataTable的问题。 我有一个 SelectOneMenu 组件,有3个选项。每个选项都应使用不同的信息呈现 DataTable 。
我的 xhtml 页面正在运行(我使用简单的代码对其进行了测试),但似乎我的支持bean 无法按照我的预期运行。当我运行页面时抛出一条错误消息。
我的xhtml看起来像
<h:form>
<h:outputLabel for="reporte" value="Tipo de reporte: " />
<h:selectOneMenu id="reporte"
value="#{reportController.type}">
<f:selectItem itemLabel="Seleccione tipo de reporte..." itemValue="null" />
<f:selectItems value="#{reportController.reportType}" />
<f:ajax execute="@form" render="@form" />
</h:selectOneMenu>
<h:panelGrid id="porSistema"
rendered="#{reportController.type == 'Por sistema'}">
<h:dataTable value="#{reportController.list}" var="item"
border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Nombre del archivo"/>
</f:facet>
<h:outputText value="#{item.fileName}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Registros correctos"/>
</f:facet>
<h:outputText value="#{item.registersOk}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Registros incorrectos"/>
</f:facet>
<h:outputText value="#{item.registersNok}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Registros procesados"/>
</f:facet>
<h:outputText value="#{item.registers}"/>
</h:column>
</h:dataTable>
</h:panelGrid>
</h:form>
豆是
@ManagedBean
@ViewScoped
public class ReportController {
List<Object> list = new ArrayList<Object>();
String type;
String[] reportType = new String[]{"Por sistema",
"Por programa y componente",
"Por estatus de pago"};
@PostConstruct
public void init() {
if (type.equals("Por sistema")) {
list.add(new ReporteSistema("A", "B", "C", "D"));
} else if (type.equals("Por programa y componente")) {
list.add(new ReporteProgramaComponenteA("E", "F", "G", "H", "I", "J"));
} else if (type.equals("Por estatus de pago")) {
list.add(new ReporteProgramaComponenteB("K", "L", "M", "N"));
}
}
public String[] getReportType() {
return reportType;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
}
并且堆栈跟踪是
com.sun.faces.mgbean.ManagedBeanCreationException: Se ha producido un error al realizar la inyección de recurso en el bean administrado reportController
at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:229)
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:105)
at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:71)
at org.apache.el.parser.AstValue.getValue(AstValue.java:147)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
at com.sun.faces.renderkit.html_basic.MenuRenderer.getCurrentSelectedValues(MenuRenderer.java:648)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:749)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:844)
at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:298)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
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:594)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at mx.gob.sagarpa.NoCacheFilter.doFilter(NoCacheFilter.java:29)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:349)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: com.sun.faces.spi.InjectionProviderException
at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:119)
at com.sun.faces.vendor.WebContainerInjectionProvider.invokePostConstruct(WebContainerInjectionProvider.java:99)
at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:223)
... 56 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor85.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:117)
... 58 more
Caused by: java.lang.NullPointerException
at mx.gob.sagarpa.beans.ReportController.init(ReportController.java:32)
... 62 more
答案 0 :(得分:1)
你的问题显然在stacktrace中说明了。您的init
方法投放了NullPointerException
。确切地说,您的type
变量没有给出任何值。你无法用它来调用equals
函数。
答案 1 :(得分:1)
您似乎误解了@PostConstruct
的目的。此注释用于指定在构造bean之后直接执行的方法。此注释允许您在注入@ManagedProperty
指定的托管属性以及@Inject
或@EJB
等所有其他依赖项后执行代码。那些注入的属性和依赖项在bean的构造函数中是不可用的(当然,在完成所有注入之前,必须首先获得完全构造的bean实例)。当您在其上发送ajax请求时,不会调用视图范围bean的@PostConstruct
。它只在您最初在浏览器中打开页面时调用。
实际上,您希望在触发ajax事件时调用该方法。您需要更改视图和bean,如下所示,以使其工作。
<h:selectOneMenu id="reporte" value="#{reportController.type}">
<f:selectItem itemLabel="Seleccione tipo de reporte..." itemValue="null" />
<f:selectItems value="#{reportController.reportType}" />
<f:ajax execute="@form" listener="#{reportController.changeType}" render="@form" />
</h:selectOneMenu>
(请注意新的listener
属性)
和
public void changeType() { // No, do not put @PostConstruct on it!
if ("Por sistema".equals(type)) {
list.add(new ReporteSistema("A", "B", "C", "D"));
} else if ("Por programa y componente".equals(type)) {
list.add(new ReporteProgramaComponenteA("E", "F", "G", "H", "I", "J"));
} else if ("Por estatus de pago".equals(type)) {
list.add(new ReporteProgramaComponenteB("K", "L", "M", "N"));
}
}
另请注意,我交换了equals()
的两面,以便您可以保证它永远不会抛出NPE。如果type
恰好是null
,那么它将被简单地比较为不相等。当您在equals()
的某些内容上实际调用null
时,您将确保获得一个NPE(这就是整个例外代表的内容!另请参阅its javadoc)。