Primefaces DataTable SortBy不起作用

时间:2017-09-05 11:23:24

标签: java jsf primefaces

我需要你的帮助。我的项目中有一个奇怪的错误。我的xhtml中有一个primefaces dataTable,它的列的sortBy属性不起作用。调试代码时,sortBy在BeanPropertyComparator类中似乎为null。 tomcat的错误日志写在下面;

Apache Tomcat 7.0.52日志

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:109)
    at com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError(AjaxExceptionHandlerImpl.java:202)
    at com.sun.faces.context.AjaxExceptionHandlerImpl.handle(AjaxExceptionHandlerImpl.java:126)
    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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

    


Apache Tomcat 7.0.52

javax.faces.FacesException: java.lang.NullPointerException
    at org.primefaces.model.BeanPropertyComparator.compare(BeanPropertyComparator.java:92)
    at java.util.TimSort.countRunAndMakeAscending(TimSort.java:324)
    at java.util.TimSort.sort(TimSort.java:189)
    at java.util.TimSort.sort(TimSort.java:173)
    at java.util.Arrays.sort(Arrays.java:659)
    at java.util.Collections.sort(Collections.java:217)
    at org.primefaces.component.datatable.feature.SortFeature.singleSort(SortFeature.java:148)
    at org.primefaces.component.datatable.feature.SortFeature.encode(SortFeature.java:106)
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:80)
    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:827)
    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:425)
    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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.NullPointerException
    at org.primefaces.model.BeanPropertyComparator.compare(BeanPropertyComparator.java:58)
    ... 48 more



当我调试代码时,它是BeanPropertyComparator的'compare'方法。参数sortBy似乎是'null'。 该类及其方法是这样的;

public class BeanPropertyComparator implements Comparator {

    private ValueExpression sortBy;
    private boolean asc;
    private String var;
    private MethodExpression sortFunction;
    private boolean caseSensitive = false;
    private Locale locale;
    private Collator collator;
    private int nullSortOrder;

    public BeanPropertyComparator(ValueExpression sortBy, String var, SortOrder sortOrder, MethodExpression sortFunction, boolean caseSensitive, Locale locale, int nullSortOrder) {
        this.sortBy = sortBy;
        this.var = var;
        this.asc = sortOrder.equals(SortOrder.ASCENDING);
        this.sortFunction = sortFunction;
        this.caseSensitive = caseSensitive;
        this.locale = locale;
        this.collator = Collator.getInstance(locale);
        this.nullSortOrder = nullSortOrder;
    }

    @SuppressWarnings("unchecked")
    public int compare(Object obj1, Object obj2) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();

            context.getExternalContext().getRequestMap().put(var, obj1);
            Object value1 = sortBy.getValue(context.getELContext());
            context.getExternalContext().getRequestMap().put(var, obj2);
            Object value2 = sortBy.getValue(context.getELContext());

错误: ValueExpression sortBy变为null

我的 xhtml 页面如下;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:b="http://bootsfaces.net/ui"
      xmlns:f="http://java.sun.com/jsf/core">


    <h:head>


    </h:head>

    <h:body>

        <h:form id="formmm">

            <p:dataTable id="dt"
                         var="kullanici"
                         value="#{deneView.ortakKurumKullanicilar}"
                         rowKey="#{kullanici.id}">

                <f:facet name="header">
                    #{etiketler.baslikKullanicilar}
                </f:facet>

                <p:column headerText="#{etiketler.tcKimlikNo}"
                          sortBy="#{kullanici.tckn}">
                    <h:outputText value="#{kullanici.tckn}" />
                </p:column>

                <p:column headerText="#{etiketler.adi}"
                          sortBy="#{kullanici.adi}" >
                    <h:outputText value="#{kullanici.adi}" />
                </p:column>

                <p:column headerText="#{etiketler.soyadi}"
                          sortBy="#{kullanici.soyadi}" >
                    <h:outputText value="#{kullanici.soyadi}" />
                </p:column>

            </p:dataTable>


        </h:form>



    </h:body>

</html>


我的xhtml的查看;

@ManagedBean
@ViewScoped
public class DeneView implements Serializable{

    private List<DeneDomain> ortakKurumKullanicilar;

    @PostConstruct
    public void init() {

        DeneDomain kullanici1 = new DeneDomain();
        kullanici1.setId(1L);
        kullanici1.setAdi("Ahmet");
        kullanici1.setSoyadi("Candemir");
        kullanici1.setTckn("11111111111");
        DeneDomain kullanici2 = new DeneDomain();
        kullanici2.setId(2L);
        kullanici2.setAdi("Mehmet");
        kullanici2.setSoyadi("Zahir");
        kullanici2.setTckn("34738946323");
        DeneDomain kullanici3 = new DeneDomain();
        kullanici3.setId(3L);
        kullanici3.setAdi("Cem");
        kullanici3.setSoyadi("Anil");
        kullanici3.setTckn("23567890546");
        DeneDomain kullanici4 = new DeneDomain();
        kullanici4.setId(4L);
        kullanici4.setAdi("Kazim");
        kullanici4.setSoyadi("Izit");
        kullanici4.setTckn("43256789000");
        DeneDomain kullanici5 = new DeneDomain();
        kullanici5.setId(5L);
        kullanici5.setAdi("Nami");
        kullanici5.setSoyadi("Cagan");
        kullanici5.setTckn("52546546556");
        DeneDomain kullanici6 = new DeneDomain();
        kullanici6.setId(6L);
        kullanici6.setAdi("Zeki");
        kullanici6.setSoyadi("Under");
        kullanici6.setTckn("14324324233");
        DeneDomain kullanici7 = new DeneDomain();
        kullanici7.setId(7L);
        kullanici7.setAdi("Deniz");
        kullanici7.setSoyadi("Fazli");
        kullanici7.setTckn("34454545455");

        ortakKurumKullanicilar = new ArrayList<>();
        ortakKurumKullanicilar.add(kullanici1);
        ortakKurumKullanicilar.add(kullanici2);
        ortakKurumKullanicilar.add(kullanici3);
        ortakKurumKullanicilar.add(kullanici4);
        ortakKurumKullanicilar.add(kullanici5);
        ortakKurumKullanicilar.add(kullanici6);
        ortakKurumKullanicilar.add(kullanici7);
    }

    public List<DeneDomain> getOrtakKurumKullanicilar() {
        return ortakKurumKullanicilar;
    }

    public void setOrtakKurumKullanicilar(List<DeneDomain> ortakKurumKullanicilar) {
        this.ortakKurumKullanicilar = ortakKurumKullanicilar;
    }

}

被修改

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 Primefaces dataTable的属性无法正常工作。问题在于将ValueExpression sortBy的值从xhtml发送到&#39; 比较&#39; BeanPropertyComparator类的primefaces方法。 SortBy值为null。我明白,问题不是关于primefaces,而是关于jsf 2.2。 javax.faces2.2.jar jar由maven存储库添加但是它已经崩溃了。当我右键单击由maven(pom.xml)创建的依赖项时,选择手动安装工件选项并更改由于另一个正常(非maven)项目而下载的jar。现在它对我来说很好,但解决后出现了另一个问题。在filterBy之后,如果我没有删除用于过滤的条目,我无法再次对表进行排序,所有数据都消失了,直到我删除该条目。我了解到它是abput Serializable接口。我将带有列表的域类放到dataTable的value属性中,必须实现Serializable接口。在我的项目中,还有一个WebService部分,它有域类。我应该在Web项目中使用这些类,但它不能将域视为Serializable,尽管它们是Serializable。