We transfer the binary file to our users in accordance with the procedure described in the SO question. How to ensure that the file is downloaded from the JSF bean database?
In the general case, the workflow works as intended, but recoverable errors may occur during the generation of the export file, and we want to display them as a warning to the user. In this case, the file itself must be generated. Therefore, we want the export to continue and display messages from individuals.
Just to emphasize this: yes, there is something wrong with the data, but our users want the export to continue and get this damaged file. Then they want to look at the file, contact their supplier and send him an error message.
Therefore, I need the export to be completed anyway.
But this will not work as we want it. I created a simplified example to illustrate our approach.
As an alternative, we consider a Bean that will store messages and display them after export. But perhaps there is a way with JSF built-in mechanisms to achieve this.
controller
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.OutputStream; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import org.apache.tomcat.util.http.fileupload.util.Streams; @ManagedBean @RequestScoped public class ExportController { public void export() { FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); byte[] exportContent = "Hy Buddys, thanks for the help!".getBytes();
JSF Page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"> <f:view contentType="text/html"> <h:body> <h:form prependId="false"> <h:messages id="messages" /> <h:commandButton id="download" value="Download" actionListener="#{exportController.export()}" /> </h:form> </h:body> </f:view> </html>
source share