java.lang.ClassCastException:javax.faces.model.ListDataModel无法转换为org.primefaces.model.LazyDataModel

问题描述 投票:0回答:3

目前我正在使用 JSF,我有一个数据表,当搜索过滤器完成时将加载值,接下来我有一个导出按钮,将数据导出为 xls 格式。在这种情况下,数据将被导出。

但是,当没有完成搜索并且数据表为空且只有列标题时,然后单击导出图标。我收到以下异常,这是堆栈跟踪和附加图像以供参考

 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)

这是我使用过的JSF代码

 <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>

我使用 LazyDataModel 将所有数据添加到列表中

请指导,如有错误请指正。

enter image description here

这是使用 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;
        }
    }

}
jsf primefaces
3个回答
3
投票

我认为例外情况很清楚,不是吗?您在某处使用

javax.faces.model.ListDataModel
,Primefaces 尝试将其转换为
org.primefaces.model.LazyDataModel
方法内的
clearLazyCache

LazyDataModel
可以转换为
DataModel
(向上转换,总是可能的),因此如果运行时的实际类型是
LazyDataModel
之前,您可能能够隐式地将其转换回来(向下转换)。

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

但是

ListDataModel
是另一片树叶的一部分,因此此转换失败。

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

也许您应该添加您的 bean 代码,而不仅仅是页面的标记。


2
投票

如果您在数据表上使用lazy =“true”,则必须使用LazyDataModel作为数据表中的值。如果你想使用普通的 Java List,就把它设置为 false 即可。


0
投票

LeaverJoinerReportLazyModel 必须在控制器 init 中初始化

© www.soinside.com 2019 - 2024. All rights reserved.