防止af:fileDownloadActionListener动作事件

时间:2013-02-01 01:10:30

标签: jsf-2 download oracle-adf phaselistener

背景

使用JDeveloper 11.1.2.3使用fileDownloadActionListener创建报告下载按钮,如下所示:

<af:commandButton text="Run Report" id="submitReport">
  <af:fileDownloadActionListener method="#{reportBean.run}"/>
</af:commandButton>

在这个JSF页面的顶部是以下内容:

<f:view afterPhase="#{validationBean.afterPhase}" ...>
  ...
    <af:form id="f1">
      <f:event listener="#{validationBean.postValidate}" type="postValidate"/>

这个想法是验证Bean可以捕获任何验证问题,如下所示:

public void afterPhase(javax.faces.event.PhaseEvent phaseEvent) {
  if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE) {
    FacesContext context = phaseEvent.getFacesContext();
    FacesMessage.Severity severity = context.getMaximumSeverity();

    if (isSevereError(severity)) {
      context.getExternalContext().getSessionMap().put(ERROR_FLAG_NAME, true);
    }
  }
}

这可以按预期工作。当用户按下按钮但表单有错误时,validationError会话变量设置为true。如果表单参数有错误,这应该允许框架阻止生成报告。

问题

报告bean的run方法使用validationError会话变量,如下所示:

  public void run(FacesContext facesContext, OutputStream outputStream) {
    Object error = facesContext.getExternalContext().getSessionMap().get( ERROR_FLAG_NAME );

    if( error != null && error != Boolean.TRUE ) {
      Report report = null;

      try {
        report = getReport();
        report.setOutputStream(outputStream);
        configure(report.getParameters());
        report.run();
      } catch (Exception e) {
        if (report != null && facesContext != null) {
          report.publish(e);
        }
      }
    }
    else {
      facesContext.getExternalContext().getSessionMap().remove( ERROR_FLAG_NAME );
      facesContext.renderResponse();
    }
  }

如果页面中存在验证错误,则会执行facesContext.renderResponse();代码,但生成的网页为空白。没有记录异常。没有错误产生。

问题

避免这种情况的一种方法是使用隐藏按钮,自定义Java和一些JavaScript,如以下页面所述:

但是,这种机制很复杂。如果页面可以像往常一样呈现,我想到的解决方案将会起作用。

如何在af:fileDownloadActionListener事件被触发后强制页面呈现?

1 个答案:

答案 0 :(得分:1)

Frank Nimphius said

  

使用隐藏按钮是您今天唯一可用的选项。一世   将提交一个ER,它会为fileDownload监听器引发一个事件   (一种预先下载)应该允许你通过调用取消它   渲染响应。如上所述,这还不存在和隐藏按钮   是您可用的选项(请注意文件下载标记是   客户端行为标记而不是完整的UI组件,这就是为什么   还没有办法中断执行。