无法过滤以在PrimeFaces DataTable上工作

时间:2014-07-17 19:20:49

标签: java jsf primefaces

DataTable不使用primefaces 4.0过滤我,我想过滤掉Estado字段。

<?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <h:form>
        <p:dataTable var="persona" value="#{filtroBean.lstPersona}" widgetVar="lstPersona"
                     emptyMessage="No cars found with given criteria" filteredValue="#{filtroBean.lstPersonaFiltro}">

            <f:facet name="header">
                <p:outputPanel>
                    <h:outputText value="Search all fields:" />
                    <p:inputText id="globalFilter" onkeyup="PF('lstPersona').filter()" style="width:150px" placeholder="Enter keyword"/>
                </p:outputPanel>
            </f:facet>

            <p:column filterBy="#{filtroBean.getEstado(persona.estado)}" headerText="Estado" footerText="contains" filterMatchMode="contains">
                <h:outputText value="#{filtroBean.getEstado(persona.estado)}" />
            </p:column>               
        </p:dataTable>

    </h:form>
    <br />
</h:body>
</html>

状态的Bean方法和列出表的对应信息

package com.msp.controladores;

import com.msp.modelo.Persona;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author admin
 */
@ManagedBean
@SessionScoped
public class FiltroBean {

    private Persona persona;
    private List<Persona> lstPersonaFiltro;
    private List<Persona> lstPersona;

    public FiltroBean() {
        persona = new Persona();
        lstPersona = new ArrayList<>();
        lstPersona.add(new Persona(1, "Edisson", "Llinin", 28, 0));
        lstPersona.add(new Persona(2, "Juan", "Valverde", 29, 1));
        lstPersona.add(new Persona(3, "Alex", "Torres", 30, 2));
        lstPersona.add(new Persona(4, "Karla", "Malonado", 31, 3));
        lstPersona.add(new Persona(5, "Jhonatan", "Oviedo", 32, 4));
    }

    public Persona getPersona() {
        return persona;
    }

    public void setPersona(Persona persona) {
        this.persona = persona;
    }

    public List<Persona> getLstPersona() {
        return lstPersona;
    }

    public void setLstPersona(List<Persona> lstPersona) {
        this.lstPersona = lstPersona;
    }

    public List<Persona> getLstPersonaFiltro() {
        return lstPersonaFiltro;
    }

    public void setLstPersonaFiltro(List<Persona> lstPersonaFiltro) {
        this.lstPersonaFiltro = lstPersonaFiltro;
    }

    public String getEstado(int estado) {
        if (estado == 0) {
            return "Ninguno";
        } else if (estado == 1) {
            return "Finalizado";
        } else if (estado == 2) {
            return "Iniciado";
        } else if (estado == 3) {
            return "En proceso";
        } else if (estado == 4) {
            return "Intemedio";
        }
        return null;
    }
}

过滤状态字段时出错

SEVERE:   Error Rendering View[/index.xhtml]
javax.el.PropertyNotFoundException: The class 'com.msp.modelo.Persona' does not have the property 'Ninguno'.
   at javax.el.BeanELResolver.getBeanProperty(BeanELResolver.java:730)
   at javax.el.BeanELResolver.getValue(BeanELResolver.java:351)
   at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
   at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
   at com.sun.el.parser.AstValue.getValue(AstValue.java:140)
   at com.sun.el.parser.AstValue.getValue(AstValue.java:204)
   at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
   at org.primefaces.component.datatable.feature.FilterFeature.filter(FilterFeature.java:129)
   at org.primefaces.component.datatable.feature.FilterFeature.encode(FilterFeature.java:89)
   at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:76)
   at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
   at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
   at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:559)
   at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
   at org.primefaces.component.api.UIData.visitTree(UIData.java:675)
   at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
   at javax.faces.component.UIForm.visitTree(UIForm.java:371)
   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:399)
   at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:319)
   at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:57)
   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:417)
   at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
   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.StandardWrapper.service(StandardWrapper.java:1682)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
   at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
   at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
   at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
   at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
   at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
   at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
   at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
   at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
   at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
   at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
   at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
   at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
   at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
   at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
   at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
   at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
   at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
   at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
   at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
   at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
   at java.lang.Thread.run(Thread.java:722)

WARNING:   StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalStateException: CDATA tags may not nest
   at com.sun.faces.renderkit.html_basic.HtmlResponseWriter.startCDATA(HtmlResponseWriter.java:681)
   at javax.faces.context.ResponseWriterWrapper.startCDATA(ResponseWriterWrapper.java:179)
   at javax.faces.context.PartialResponseWriter.startError(PartialResponseWriter.java:341)
   at org.primefaces.context.PrimePartialResponseWriter.startError(PrimePartialResponseWriter.java:156)
   at com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError(AjaxExceptionHandlerImpl.java:200)
   at com.sun.faces.context.AjaxExceptionHandlerImpl.handle(AjaxExceptionHandlerImpl.java:124)
   at javax.faces.context.ExceptionHandlerWrapper.handle(ExceptionHandlerWrapper.java:100)
   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
   at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
   at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
   at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
   at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
   at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
   at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
   at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
   at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
   at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
   at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
   at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
   at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
   at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
   at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
   at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
   at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
   at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
   at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
   at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
   at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
   at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
   at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
   at java.lang.Thread.run(Thread.java:722)

请指导我实现目标的正确方法。

1 个答案:

答案 0 :(得分:1)

   <p:column filterBy="#{filtroBean.getEstado(persona.estado)}" headerText="Estado" footerText="contains" filterMatchMode="contains">

filterBy需要一个bean属性进行过滤,如

public class foo {
 private String filterMe;

 //getter and setter
}

在getEstado中,您返回字符串“Ninguno”,而Ninguno不是属性。

<p:column filterBy="#{persona.estado}" .....> 

为了将int映射到String你需要写一个转换器,在PF5中你可以在p:column中使用filterFunction