您还可以使用组件系统事件(特别是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>
然后,在你的报告豆,你把文件和标记响应,完整(使用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();
}
}
这就是它的全部!
您可以链接到下载页面(/download.jsf),也可以使用HTML元标记在启动页面上重定向到它。
我试过这个解决方案。它只有在页面没有其他控制的情况下才有效。如果页面具有其他控件(如下拉框选择),并且页面来回多次,则“download()”将继续调用并且该值将永久重置。因此,有没有办法永远保护它的执行只有一次? – huahsin68 2012-08-30 02:33:48