jsfprimefaces

java.lang.ClassCastException: javax.faces.model.ListDataModel cannot be cast to org.primefaces.model.LazyDataModel


Currently I am playing with JSF, I have an data-table, values will be loaded when ever search filter has done, Moving forward I have an export button, exports the data to xls format. In this scenario data is exported.

But when there is no search done and the data-table is empty which has only column headers, and by clicking on the export icon. I am getting the following exception, Here is the stack trace and Image attached for reference

 Apr 21, 2017 4:37:24 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: javax.faces.model.ListDataModel cannot be cast to org.primefaces.model.LazyDataModel
java.lang.ClassCastException: javax.faces.model.ListDataModel cannot be cast to org.primefaces.model.LazyDataModel
    at org.primefaces.component.datatable.DataTable.clearLazyCache(DataTable.java:964)
    at org.primefaces.component.export.Exporter.exportAll(Exporter.java:224)
    at org.primefaces.component.export.ExcelExporter.export(ExcelExporter.java:59)
    at org.primefaces.component.export.DataExporter.processAction(DataExporter.java:97)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:105)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    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 com.ppts.setting.SessionFilter.doFilter(SessionFilter.java:38)
    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:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1527)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1484)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

Apr 21, 2017 4:37:24 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/GEO_DM] threw exception [javax.faces.model.ListDataModel cannot be cast to org.primefaces.model.LazyDataModel] with root cause
java.lang.ClassCastException: javax.faces.model.ListDataModel cannot be cast to org.primefaces.model.LazyDataModel
    at org.primefaces.component.datatable.DataTable.clearLazyCache(DataTable.java:964)
    at org.primefaces.component.export.Exporter.exportAll(Exporter.java:224)
    at org.primefaces.component.export.ExcelExporter.export(ExcelExporter.java:59)
    at org.primefaces.component.export.DataExporter.processAction(DataExporter.java:97)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:105)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    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 com.ppts.setting.SessionFilter.doFilter(SessionFilter.java:38)
    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:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1527)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1484)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

Here is the JSF Code which I have Used

 <label>Export</label> <span class="info-box-title"> <h:panelGrid
columns="2" border="0" cellpadding="3" cellspacing="5">
<h:commandLink>
    <img src="images/excel.png" class="img-responsive" />
    <p:dataExporter type="xls" target="removetagtbl"
        fileName="LeaverJoinerDataxls" 
        postProcessor="#{leaverJoinerTagReportBean.postProcessXLS}"/>

</h:commandLink>

<h:commandLink style="padding-right: 20px;">
    <img src="images/csv.png" />
    <p:dataExporter type="csv" target="removetagtbl"
        fileName="LeaverJoinerDatacsv" />
</h:commandLink>

I have used LazyDataModel to add all the datas into a list

Please guide me, and correct me if anything is wrong.

enter image description here

Here is the code of preparing list using LazyDataModel

public class LeaverJoinerReportLazyModel extends LazyDataModel<LeaverJoinerTagReportDTO> {

    private static final long serialVersionUID = 1L;

    private List<LeaverJoinerTagReportDTO> datasource;

    public LeaverJoinerReportLazyModel(List<LeaverJoinerTagReportDTO> datasource) {
        this.datasource = datasource;
    }

    @Override
    public LeaverJoinerTagReportDTO getRowData(String rowKey) {
        for (LeaverJoinerTagReportDTO leaverJoinerTagReportDTO : datasource) {
            if (leaverJoinerTagReportDTO.getLineId().equals(rowKey))
                return leaverJoinerTagReportDTO;
        }
        return null;
    }

    @Override
    public Object getRowKey(LeaverJoinerTagReportDTO leaverJoinerTagReportDTO) {
        return leaverJoinerTagReportDTO.getLineId();
    }

    @Override
    public List<LeaverJoinerTagReportDTO> load(int first, int pageSize,
            String sortField, SortOrder sortOrder, Map<String, Object> filters) {

        List<LeaverJoinerTagReportDTO> dataList = new ArrayList<LeaverJoinerTagReportDTO>();

        // filter
        for (LeaverJoinerTagReportDTO leaverJoinerTagReportDTO : datasource) {
            boolean match = true;

            if (filters != null) {
                for (Iterator<String> iterator = filters.keySet().iterator();iterator.hasNext();) {
                    try {
                        String sFilterProperty = iterator.next();
                        Object sFilterValue = filters.get(sFilterProperty);
                        String sFieldValue = String.valueOf(leaverJoinerTagReportDTO.getClass().getField(sFilterProperty).get(leaverJoinerTagReportDTO));
                        if (sFilterValue == null    || sFieldValue.startsWith(sFilterValue.toString())) {
                            match = true;
                        } else {
                            match = false;
                            break;
                        }
                    } catch (Exception e) {
                        match = false;
                    }
                }
            }else{
                //Do Nothing
            }

            if (match) {
                dataList.add(leaverJoinerTagReportDTO);
            }
        }

        // rowCount
        int dataSize = dataList.size();
        this.setRowCount(dataSize);

        // paginate
        if (dataSize > pageSize) {
            try {
                return dataList.subList(first, first + pageSize);
            } catch (IndexOutOfBoundsException e) {
                return dataList.subList(first, first + (dataSize % pageSize));
            }
        } else {
            return dataList;
        }
    }

}

Solution

  • I think the exception is pretty clear, isn't it? You are using javax.faces.model.ListDataModel somewhere, and Primefaces tries to cast this to org.primefaces.model.LazyDataModel inside the clearLazyCache-Method.

    LazyDataModel can be casted to DataModel (upcast, always possible), therefore you might be able to implicit cast it back (downcast), if the actual Type during runtime was LazyDataModel before.

    java.lang.Object
      javax.faces.model.DataModel<T>
        org.primefaces.model.LazyDataModel<T>
    

    https://www.primefaces.org/docs/api/5.0/org/primefaces/model/LazyDataModel.html

    But ListDataModel is part of another tree-leaf, therefore this cast fails.

    java.lang.Object
      javax.faces.model.DataModel<E>
        javax.faces.model.ListDataModel<E>
    

    http://docs.oracle.com/javaee/6/api/javax/faces/model/ListDataModel.html

    Maybe you should add your bean-code, not only the markup of the page.