我需要显示 in another tab
使用bean方法生成报告(bean.generateReporte())
。并在无法生成报告(未找到数据)时显示一条消息。根据条件)。
我在.xhtml中的第一个代码是:
<p:commandButton id="printDeliveryList" label="Generate Delivery List" styleClass="btn-success"
style="float:left" icon="fa fa-print"
action="#{bean.generateReporte()}"
onclick="this.form.target = '_blank'"
ajax="false"
/>
我正在阅读这个问题How to prevent commandButton from executing action when onclick method return false和这个问题How to call JS function on click of JSF button and block page reload/navigation? 而这篇文章https://www.adictosaltrabajo.com/2011/09/14/primefaces-client-side-ajax-events/ 还有这个文件 https://www.primefaces.org/showcase/ui/misc/requestContext.xhtml
我在参加onsuccess
事件。
稍后再输入我的代码。...
在 bean
中,我有:
public void generateReporte() {
// blabla
if (success) {
addGeneratedToCallback(true);
} else {
addGeneratedToCallback(false);
}
}
private void addGeneratedToCallback(boolean value) {
PrimeFaces.current().ajax().addCallbackParam("generated", value);
}
在 .xhtml
端
<script type="text/javascript">
function handleComplete(xhr, status, args) {
if (args.validationFailed) {
PrimeFaces.debug("Validation Failed");
} else {
if (args.generated) {
// open a new tab in the browser
//window.open(this.form.target = '_blank');
form.target = '_blank';
} else {
// show message
}
}
}
</script>
<p:commandButton id="printListaEntrega" label="Generar Lista de Entrega" styleClass="btn-success"
style="float:left" icon="fa fa-print"
action="#{bean.generateReporte()}"
ajax="false"
oncomplete="handleComplete(xhr, status, args)"
/>
我的代码结果
在无法生成报告的情况下,使用ajax="false"
可以得到此信息。
当我从ajax="false"
中删除p:commandButton
并生成报告时,我得到了。 (未在浏览器中打开新标签,并且无法下载报告)
是否可以设置连续ajax="bean.someMethodOrProperty"
?
答案 0 :(得分:0)
在.xhtml
<p:commandButton id="printDeliveryList" label="Generate Delivery List" styleClass="btn-success"
style="float:left" icon="fa fa-print"
action="#{bean.generateReport()}"
oncomplete="if (args && !args.validationFailed) {PF('download').jq.click();PF('openNewTab').jq.click() }"
/>
<p:commandButton widgetVar="download" styleClass="ui-helper-hidden"
action="#{bean.downloadFile()}" ajax="false" />
<p:commandButton widgetVar="openNewTab" styleClass="ui-helper-hidden"
action="#{bean.openInNewTab()}"
onclick="this.form.target = '_blank'" ajax="false" />
在豆子里
public void generateReport() {
// blabla
if (success) {
bytesToBeDownload = getBytesOfReport();
nameOfFile = getNameOfReport() + ".pdf";
} else {
Faces.validationFailed();
}
}
public void downloadFile() {
try {
// byte[] bytesToBeDownload = ....
org.omnifaces.util.Faces.sendFile(bytesToBeDownload, nameOfFile, true);
} catch (IOException e) {
//Exception handling
}
}
public void openInNewTab() {
try {
byte[] bytes = bytesToBeDownload;
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
baos.write(bytes, 0, bytes.length);
buildResponse(baos, "application/pdf");
} catch (Exception e) {
//Exception handling
}
}
private static void buildResponse(ByteArrayOutputStream out, String contentType) {
ByteArrayInputStream input = null;
BufferedOutputStream output = null;
try {
input = new ByteArrayInputStream(out.toByteArray());
int bufferSize = out.toByteArray().length;
FacesUtils.getHttpServletResponse().reset();
FacesUtils.getHttpServletResponse().setHeader("Content-Type", contentType);
FacesUtils.getHttpServletResponse().setHeader("Content-Length", String.valueOf(bufferSize));
output = new BufferedOutputStream(FacesUtils.getHttpServletResponse().getOutputStream(), bufferSize);
byte[] buffer = new byte[bufferSize];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
} catch (Exception ex) {
//Exception handling
} finally {
try {
output.close();
input.close();
} catch (Exception ex) {
//Exception handling
}
}
FacesUtils.getFacesContext().responseComplete();
}