gue*_*rda 9 jsf richfaces event-handling
我想为报告导出页面构建结果页面.此结果页面必须显示导出的状态并提供此导出的下载.
导出以动作方法完成.我可以通过a执行它commandButton
但必须在加载时自动执行.
我怎么能做到这一点?
JSF:
<h:commandButton value="Download report" action="#{resultsView.downloadReport}"/>
Run Code Online (Sandbox Code Playgroud)
支持豆:
public String downloadReport() {
...
FileDownloadUtil.downloadContent(tmpReport, REPORT_FILENAME);
// Stay on this page
return null;
}
Run Code Online (Sandbox Code Playgroud)
澄清:a4j这是可行的吗?我想到了一个Ajax请求触发我的downloadReport
操作的解决方案,它的请求是文件下载.
Dan*_*len 15
您还可以使用组件系统事件(特别是PreRenderViewEvent)在JSF 2.0中解决此问题.
只需创建一个下载视图(/download.xhtml),在渲染之前触发下载侦听器.
<?xml version="1.0" encoding="UTF-8"?>
<f:view
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core">
<f:event type="preRenderView" listener="#{reportBean.download}"/>
</f:view>
Run Code Online (Sandbox Code Playgroud)
然后,在报表bean(使用JSR-299定义)中,推送文件并将响应标记为完成.
public @Named @RequestScoped class ReportBean {
public void download() throws Exception {
FacesContext ctx = FacesContext.getCurrentInstance();
pushFile(
ctx.getExternalContext(),
"/path/to/a/pdf/file.pdf",
"file.pdf"
);
ctx.responseComplete();
}
private void pushFile(ExternalContext extCtx,
String fileName, String displayName) throws IOException {
File f = new File(fileName);
int length = 0;
OutputStream os = extCtx.getResponseOutputStream();
String mimetype = extCtx.getMimeType(fileName);
extCtx.setResponseContentType(
(mimetype != null) ? mimetype : "application/octet-stream");
extCtx.setResponseContentLength((int) f.length());
extCtx.setResponseHeader("Content-Disposition",
"attachment; filename=\"" + displayName + "\"");
// Stream to the requester.
byte[] bbuf = new byte[1024];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1)) {
os.write(bbuf, 0, length);
}
in.close();
}
}
Run Code Online (Sandbox Code Playgroud)
这里的所有都是它的!
您可以链接到下载页面(/download.jsf),也可以使用HTML元标记在启动页面上重定向到它.
之前的答案将提交表单,也许会更改导航.
使用<rich:jsFunction action="#{bean.action}" name="loadFunction" />
然后window.onload = loadFunction;