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

查看:20
本文介绍了在加载时执行支持 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.

我怎样才能做到这一点?

How can I accomplish this?

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天全站免登陆