Primefaces从Java导出会导致IllegalStateException

时间:2015-06-18 09:03:42

标签: java primefaces export illegalstateexception

我目前正在尝试使用primefaces导出器类将内容导出到csv / xls文件。我正在从javacode生成一个数据表组件并将其传递给exporter类。它工作正常,但我常常遇到以下异常:

  

java.lang.IllegalStateException:已为此响应调用了getOutputStream()

我理解异常,但不是为什么抛出异常。以下消息由primefaces命令按钮作为actionlistener调用:

public void export(MyBeanBeanClass bean, MyTable myTable, String format) {
    FacesContext context = FacesContext.getCurrentInstance();
    Exporter exporter = ExporterFactory.getExporterForType(format);
    DataTable table = new DataTable();
    String beanName = this.lookupManagedBeanName(bean);
    List<Object[]> exportList = new ArrayList<Object[]>();
    String outputFileName = "export";       
    boolean isPageOnly = false;       
    boolean isSelectionOnly = false;
    String encodingType = "iso-8859-1"; 
    MethodExpression preProcessor = null;
    MethodExpression postProcessor = null;

    // creating <p:datatable> component from java code -> see balusc's blog
    // http://balusc.blogspot.de/2006/06/using-datatables.html#PopulateDynamicDatatable

    // prepare datasource for export
    for (MyRow row : myTable) {
        Object[] rowContent = new Object[row.columnCount()];

        for (int i = 0; i < rowContent.length; i++) {
            rowContent[i] = row.get(i);
        }

        exportList.add(rowContent);
    }
    bean.setExportList(exportList);

    // create primefaces datatable <p:datatable>
    table.setValueExpression("value", createValueExpression("#{" + beanName + ".exportList}", List.class));
    table.setVar("row");


    // create columns and rows
    for (int i = 0; i < myTable.getColumnCount(); i++) {

        // Create <p:column>.
        Column column = new Column();
        table.getChildren().add(column);

        // Create <h:outputText value="dynamicHeaders[i]"> for <f:facet name="header"> of column.
        HtmlOutputText header = new HtmlOutputText();
        header.setValue(this.getColumnTitle(this.getRenderedColumnNames().get(i)));
        column.setHeader(header);

        // Create <h:outputText value="#{dynamicItem[" + i + "]}"> for the body of column.
        HtmlOutputText output = new HtmlOutputText();
        output.setValueExpression("value", createValueExpression("#{row[" + i + "]}", String.class));
        column.getChildren().add(output);
    }

    // export       
    try {
        exporter.export(context, table, outputFileName, isPageOnly, isSelectionOnly, encodingType, preProcessor, postProcessor);
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这是命令按钮的xhtml:

<h:form>
    <p:commandButton value="#{cc.attrs.exportXLS}" style="margin-top: 5px; width: 100%"
                 actionListener="#{cc.attrs.view.export(cc.attrs.bean, cc.attrs.view.tableOfData, &quot;xls&quot;)}"
                 ajax="false" />
</h:form>

一些信息:bean是一个简单的视图结构bean,它存储生成的列表(在函数中)以便导出它。 myTable包含应该导出的数据(将被转换为函数中的对象数组列表) 格式包含“csv”或“xls”。

这是stacktrace:

java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:678)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
    at com.sun.faces.context.ExternalContextImpl.getResponseOutputWriter(ExternalContextImpl.java:837)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.createResponseWriter(FaceletViewHandlingStrategy.java:1197)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:408)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    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.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:612)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    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:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    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)

任何人都知道我为什么会受到这种刺激?

亲切的问候 亚历

1 个答案:

答案 0 :(得分:0)

我通过在导出调用后添加以下行来解决问题:

context.responseComplete();