PrimeFaces文件下载处理错误
基本上我的问题是:当要下载的文件发生失败时,如何在页面上显示错误消息并取消下载。 PrimeFaces 7。我愿意更新到11(打破了我的某些代码..),如果我看到一种在11中实现这一目标的方法,
<p:commandButton id="downloadButtonAllCSV" widgetVar="downloadButtonAllCSV" value="Download alle CSV" onclick="PrimeFaces.monitorDownload(start, stop)" icon="ui-icon-arrowthick-1-s" styleClass="button" update="@form">
<p:fileDownload value="#{reportingController.CSVAllGCG}" />
</p:commandButton>
<p:growl></p:growl>
<p:dialog widgetVar="csvError" header="Fehler bei CSV Erzeugung">
<h:outputText value="Fehler bei CSV Erzeugung"></h:outputText>
</p:dialog>
我的bean代码基本上会检查选定的样本的数量是否小于某些辅助值的值避免使用浪费。我的问题是,在其他块中的网页上显示一些错误的尝试不起作用。咆哮和对话框都没有出现。
public StreamedContent getCSVAllGCG() {
InputStream targetStream = null;
String csv = "";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(GCGNachrichtDTO.REP_CSV_HEADER);
if (getAnzahl().intValue() < this.maxDownloads) {
List<GCGNachrichtDTO> allDtos = this.reportMessageDAO.findGCGNachichten(0, getAnzahl().intValue(), filter, true);
String dtoString = allDtos.stream().map(d -> d.getMonCSVString()).collect(Collectors.joining());
stringBuilder.append(dtoString);
csv = stringBuilder.toString();
targetStream = new ByteArrayInputStream(csv.getBytes());
} else {
String message = "Mehr als " + maxDownloads + " Eintraege koennen nicht verarbeitet werden";
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, message);
FacesContext.getCurrentInstance().addMessage(null, facesMessage);
message += " Konfiguration mit gcg.monitoring.max.downloads. Ein zu hoher Wert kann zu einer OutOfMemoryError fuehren.";
log.info(message);
PrimeFaces.current().executeScript("PF('csvError').show()");
targetStream = new ByteArrayInputStream(message.getBytes());
}
return new DefaultStreamedContent(targetStream, "text/csv", "reporting.csv");
}
@adam:
<p:commandButton id="downloadButtonAllCSV" widgetVar="downloadButtonAllCSV" value="Download alle CSV" icon="ui-icon-arrowthick-1-s" styleClass="button" update="@form" actionListener="#{reportingController.actionCSVAllGCG()}">
<p:fileDownload value="#{reportingController.stream}" />
</p:commandButton>
public void actionCSVAllGCG() {
InputStream targetStream = null;
String csv = "";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(GCGNachrichtDTO.REP_CSV_HEADER);
if (getAnzahl().intValue() <= this.maxDownloads) {
List<GCGNachrichtDTO> allDtos = this.reportMessageDAO.findGCGNachichten(0, getAnzahl().intValue(), filter, true);
String dtoString = allDtos.stream().map(d -> d.getMonCSVString()).collect(Collectors.joining());
stringBuilder.append(dtoString);
csv = stringBuilder.toString();
targetStream = new ByteArrayInputStream(csv.getBytes());
} else {
String message = "Mehr als " + maxDownloads + " Eintraege koennen nicht verarbeitet werden";
log.info(message);
targetStream = new ByteArrayInputStream(message.getBytes());
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, message);
FacesContext.getCurrentInstance().addMessage(null, facesMessage);
message += " Konfiguration mit gcg.monitoring.max.downloads. Ein zu hoher Wert kann zu einer OutOfMemoryError fuehren.";
log.info("------- ******** " + message);
PrimeFaces.current().executeScript("PF('csvError').show()");
}
this.stream = new DefaultStreamedContent(targetStream, "text/csv", "reporting.csv");
}
这也不起作用,什么也没发生...
Basically my problem is: How do I display an error message on my page and cancel a download, when the generation of the file to download has failed.
Primefaces 7. I would be willing to update to 11 (which breaks some of my code ..) if I would see a method which achieves this in 11
<p:commandButton id="downloadButtonAllCSV" widgetVar="downloadButtonAllCSV" value="Download alle CSV" onclick="PrimeFaces.monitorDownload(start, stop)" icon="ui-icon-arrowthick-1-s" styleClass="button" update="@form">
<p:fileDownload value="#{reportingController.CSVAllGCG}" />
</p:commandButton>
<p:growl></p:growl>
<p:dialog widgetVar="csvError" header="Fehler bei CSV Erzeugung">
<h:outputText value="Fehler bei CSV Erzeugung"></h:outputText>
</p:dialog>
My bean code basically checks if the number of selected samples is smaller than some cofigured value to avoid an OutOfMemory. My problem is that the attempts to show some error on the web page in the else block do not work. Neither the growl, nor the dialog show up.
public StreamedContent getCSVAllGCG() {
InputStream targetStream = null;
String csv = "";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(GCGNachrichtDTO.REP_CSV_HEADER);
if (getAnzahl().intValue() < this.maxDownloads) {
List<GCGNachrichtDTO> allDtos = this.reportMessageDAO.findGCGNachichten(0, getAnzahl().intValue(), filter, true);
String dtoString = allDtos.stream().map(d -> d.getMonCSVString()).collect(Collectors.joining());
stringBuilder.append(dtoString);
csv = stringBuilder.toString();
targetStream = new ByteArrayInputStream(csv.getBytes());
} else {
String message = "Mehr als " + maxDownloads + " Eintraege koennen nicht verarbeitet werden";
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, message);
FacesContext.getCurrentInstance().addMessage(null, facesMessage);
message += " Konfiguration mit gcg.monitoring.max.downloads. Ein zu hoher Wert kann zu einer OutOfMemoryError fuehren.";
log.info(message);
PrimeFaces.current().executeScript("PF('csvError').show()");
targetStream = new ByteArrayInputStream(message.getBytes());
}
return new DefaultStreamedContent(targetStream, "text/csv", "reporting.csv");
}
@adam:
<p:commandButton id="downloadButtonAllCSV" widgetVar="downloadButtonAllCSV" value="Download alle CSV" icon="ui-icon-arrowthick-1-s" styleClass="button" update="@form" actionListener="#{reportingController.actionCSVAllGCG()}">
<p:fileDownload value="#{reportingController.stream}" />
</p:commandButton>
public void actionCSVAllGCG() {
InputStream targetStream = null;
String csv = "";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(GCGNachrichtDTO.REP_CSV_HEADER);
if (getAnzahl().intValue() <= this.maxDownloads) {
List<GCGNachrichtDTO> allDtos = this.reportMessageDAO.findGCGNachichten(0, getAnzahl().intValue(), filter, true);
String dtoString = allDtos.stream().map(d -> d.getMonCSVString()).collect(Collectors.joining());
stringBuilder.append(dtoString);
csv = stringBuilder.toString();
targetStream = new ByteArrayInputStream(csv.getBytes());
} else {
String message = "Mehr als " + maxDownloads + " Eintraege koennen nicht verarbeitet werden";
log.info(message);
targetStream = new ByteArrayInputStream(message.getBytes());
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, message);
FacesContext.getCurrentInstance().addMessage(null, facesMessage);
message += " Konfiguration mit gcg.monitoring.max.downloads. Ein zu hoher Wert kann zu einer OutOfMemoryError fuehren.";
log.info("------- ******** " + message);
PrimeFaces.current().executeScript("PF('csvError').show()");
}
this.stream = new DefaultStreamedContent(targetStream, "text/csv", "reporting.csv");
}
This does not work either, nothing happens...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论