在加载时执行支持bean操作? [英] Execute backing bean action on load?

查看:138
本文介绍了在加载时执行支持bean操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为报告导出页面构建一个结果页面。此结果页面必须显示导出的状态并提供此导出的下载。

I would like to build a results page for a report export page. This results page must display the status of the export and offer the download of this export.

导出是在操作方法中完成的。我可以通过 commandButton 执行它,但必须在加载时自动执行。

The export is done in an action method. I can execute it via a commandButton but it must be executed automatically on load.

我该如何完成?

JSF:

<h:commandButton value="Download report" action="#{resultsView.downloadReport}"/>

支持bean:

  public String downloadReport() {
    ...
    FileDownloadUtil.downloadContent(tmpReport, REPORT_FILENAME);
    // Stay on this page
    return null;
  }

澄清:a4j是否可行?我想到一个解决方案,一个Ajax请求触发我的 downloadReport 操作,其请求是文件下载。

Clarification: Is this feasible with a4j? I thought of a solution that an Ajax request triggers my downloadReport action and its request is the file download.

推荐答案

您还可以在JSF 2.0中使用组件系统事件,特别是PreRenderViewEvent来解决此问题。

You can also solve this in JSF 2.0 using component system events, specifically the PreRenderViewEvent.

只需创建一个下载视图(/ download)。 xhtml)在渲染之前触发一个下载监听器。

Just create a download view (/download.xhtml) that fires a download listener before render.

<?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>

然后,在报表bean(使用JSR-299定义)中,将文件推送到响应完成。

Then, in your report bean (defined using JSR-299), you push the file and mark the response as complete.

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元标记在启动页面上重定向到。

You can either link to the download page (/download.jsf), or use a HTML meta tag to redirect to it on a splash page.

这篇关于在加载时执行支持bean操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆