问题描述
我正在尝试为一个数据表创建过滤器.我希望该过滤器适用于数据表中包含的所有关键字.
I'm trying create a filter to one dataTable. I want that filter works with all keywords contained in datatable.
我正在关注以下示例:http://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml 但我不能让它工作
I am following examples at: http://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml but I can't make it work
问题是,当我输入任何关键字时,dataTable 会清楚地显示消息Nenhum Equipmento encontrado",如果我删除关键字,所有结果都不会返回并且消息Nenhum Equipmento encontrado"会保留.
The problem is when I enter with any keywords the dataTable is clear showing message "Nenhum equipamento encontrado", if I delete keyword all results doesn't returns and message "Nenhum equipamento encontrado" keeps.
我该如何解决这个问题?
How can I solve this problem ?
我查找了类似的问题,我发现列上可能缺少 filterBy="#{}",然后我添加了所有内容,但仍然不起作用
I looked up for similar problems and all i find was the possible lack of filterBy="#{}" on the columns, i added then all, but still doesn't work
XHTML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Equipamentos</title>
</h:head>
<h:body>
<p:dataTable id="equipamentos"
var="equipamento"
value="#{equipamentoBean.equipamentos}"
widgetVar="equipamentoTable"
filteredValue="#{equipamentoBean.equipamentosFiltrados}"
paginator="true"
rows="15"
emptyMessage="Nenhum equipamento encontrado"
resizableColumns="true">
<f:facet name="header">
Lista de equipamentos
<!-- DESLIGAR COLUNAS -->
<p:commandButton id="toggler"
type="button"
value="Colunas"
style="float:right"
icon="ui-icon-calculator" />
<p:columnToggler datasource="equipamentos" trigger="toggler" />
<!-- BUSCA -->
<p:outputPanel>
<h:outputText value="Busca em todos os campos:"/>
<p:inputText id="globalFilter"
onkeyup="PF('equipamentoTable').filter()" style="width:250px"
placeholder="Palavra-chave para busca" />
</p:outputPanel>
</f:facet>
<p:column filterBy="#{equipamento.nome}"
sortBy="#{equipamento.nome}"
headerText="Nome">
<h:outputText value="#{equipamento.nome}"></h:outputText>
</p:column>
<p:column filterBy="#{equipamento.equipamentoTipoId}"
sortBy="#{equipamento.equipamentoTipoId}"
headerText="Tipo">
<h:outputText value="#{equipamento.equipamentoTipoId}"></h:outputText>
</p:column>
<p:column filterBy="#{equipamento.equipamentoTipoId.hardwareEquipamentoId}"
headerText="Hardware">
<h:outputText
value="#{equipamento.equipamentoTipoId.hardwareEquipamentoId}">
</h:outputText>
</p:column>
<p:column filterBy="#{equipamento.equipamentoVersaoSoftwareId}"
headerText="Versao Software">
<h:outputText
value="#{equipamento.equipamentoVersaoSoftwareId}">
</h:outputText>
</p:column>
<p:column filterBy="#{equipamento.equipamentoTipoId.tecnologiaEquipamentoId}"
headerText="Tecnologia">
<h:outputText
value="#{equipamento.equipamentoTipoId.tecnologiaEquipamentoId}"
</h:outputText>
</p:column>
<p:column filterBy="#{equipamento.regiaoId}" headerText="Regional">
<h:outputText value="#{equipamento.regiaoId}"></h:outputText>
</p:column>
<p:column filterBy="#{equipamento.equipamentoTipoId.areaId}"
headerText="Area">
<h:outputText
value="#{equipamento.equipamentoTipoId.areaId}">
</h:outputText>
</p:column>
<p:column filterBy="#{equipamento.ativo}" headerText="Ativo">
<h:outputText value="#{equipamento.ativo}"></h:outputText>
</p:column>
</p:dataTable>
</h:body>
</html>
ManagedBean
package br.com.timbrasil.network.bean;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
import br.com.timbrasil.network.dao.EquipamentoDao;
import br.com.timbrasil.network.modelo.Equipamento;
@ManagedBean
@ViewScoped
public class EquipamentoBean {
private List<Equipamento> equipamentos;
private List<Equipamento> equipamentosFiltrados;
public EquipamentoBean(){
this.equipamentos = new EquipamentoDao().listaEquipamentos();
Equipamento eqp = new Equipamento();
}
public List<Equipamento> getEquipamentos() {
return equipamentos;
}
public void setEquipamentos(List<Equipamento> equipamentos) {
this.equipamentos = equipamentos;
}
public List<Equipamento> getEquipamentosFiltrados() {
return equipamentosFiltrados;
}
public void setEquipamentosFiltrados(List<Equipamento> equipamentosFiltrados) {
this.equipamentosFiltrados = equipamentosFiltrados;
}
}
错误
set 24, 2014 7:00:54 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
SEVERE: Error Rendering View[/equipamento.xhtml]
java.lang.NullPointerException
at org.primefaces.component.datatable.feature.FilterFeature.filter(FilterFeature.java:136)
at org.primefaces.component.datatable.feature.FilterFeature.encode(FilterFeature.java:105)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:77)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:582)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
at org.primefaces.component.api.UIData.visitTree(UIData.java:692)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:403)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:322)
at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:1004)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:430)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
set 24, 2014 7:00:54 PM com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError
SEVERE: java.lang.NullPointerException
at org.primefaces.component.datatable.feature.FilterFeature.filter(FilterFeature.java:136)
at org.primefaces.component.datatable.feature.FilterFeature.encode(FilterFeature.java:105)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:77)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:582)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
at org.primefaces.component.api.UIData.visitTree(UIData.java:692)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:403)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:322)
at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:1004)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:430)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
推荐答案
你必须像这样把你的 p:datatable
放在 h:form
里面.
You must put your p:datatable
inside h:form
like this.
<h:form id="form-list-equipamentos">
<p:dataTable id="equipamentos"
var="equipamento"
value="#{equipamentoBean.equipamentos}"
widgetVar="equipamentoTable"
filteredValue="#{equipamentoBean.equipamentosFiltrados}"
emptyMessage="Nenhum equipamento encontrado"
resizableColumns="true">
<f:facet name="header">
Lista de equipamentos
<!-- DESLIGAR COLUNAS -->
<p:commandButton id="toggler"
type="button"
value="Colunas"
style="float:right"
icon="ui-icon-calculator" />
<p:columnToggler datasource="equipamentos"
trigger="toggler" />
<!-- BUSCA -->
<p:outputPanel>
<h:outputText value="Busca em todos os campos:"/>
<p:inputText id="globalFilter"
onkeyup="PF('equipamentoTable').filter()"
style="width:250px"
placeholder="Palavra-chave para busca" />
</p:outputPanel>
</f:facet>
<p:column filterBy="#{equipamento.nome}"
sortBy="#{equipamento.nome}"
headerText="Nome" filterMatchMode="contains">
<h:outputText value="#{equipamento.nome}"></h:outputText>
</p:column>
</p:dataTable>
</h:form>
还要注意您的 equipamentoBean
在适当的范围内(我的:javax.faces.view.ViewScoped
).
Also watchout that your equipamentoBean
is in proper scope (mine: javax.faces.view.ViewScoped
).
OT:我建议您识别"f:form
标记,因为这使您可以在创建"表单(在此之外)之后更新它表单!)添加了一条记录:
OT: I would recommend you to "id" the f:form
tag as this gives you the possibility to update it after a "create" form (outside this form!) has added a record:
<h:form>
....
<p:commandButton
type="submit"
value="Add something"
action="#{someBackingBean.addSomething()}"
update=":master:form-list-equipamentos:equipamentos"
/>
</h:form>
id="master"
位于我的主"模板中,所有其他视图均来自:
The id="master"
is in my "master" template where all other views derive from:
<h:body>
<pm:page id="master">
...
</pm:page>
</h:body>
这篇关于org.primefaces.component.datatable.feature.FilterFeature.filter 处的 java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!