I am currently trying to export content to a csv/xls file using primefaces exporter classes. I am generating a datatable component from javacode and pass it to the exporter class. It works fine, but I get very often a the following exception:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
I understand the exception, but not why it is thrown. The following message is called as an actionlistener by a primefaces command button:
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();
}
}
here's the xhtml of the command button:
<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, "xls")}"
ajax="false" />
</h:form>
some information: bean is a simple viewscoped bean which stores the generated list (in function) for exporting it. myTable contains the data which should be exported (will be transfered into an list of object array in function) format contains "csv" or "xls".
here is the 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)
anybody got an idea why I get this excpetion?
kind regards Alex
I solved the problem by adding the following line after the export call: