Refresh page after download

2

I'm working on a JSF page and need to download a .pdf. When downloading the object is changed and I would need to update it because I can not allow the same download more than once. But if I use <f:ajax> the download does not work.

   <h:commandLink actionListener="#{repositorioCobrancaAddMB.clear}" 
                  title="${messages['label.print']}" 
                  rendered="#{repositorioCobrancaAddMB.podeGeraBoleto(bean)}"
                  class="btn btn-white">
             <span class="fa fa-file-pdf-o"/>
             <f:ajax event="click" execute="@this" render="@all" listener="#{repositorioCobrancaAddMB.geraBoleto(bean)}"/>
     </h:commandLink> 

Without using ajax the download happens, but does not update my page!

     <h:commandLink action="#{repositorioCobrancaAddMB.geraBoleto(bean)}" 
                    actionListener="#{repositorioCobrancaAddMB.clear}" 
                    title="${messages['label.print']}" 
                    rendered="#{repositorioCobrancaAddMB.podeGeraBoleto(bean)}"
                    class="btn btn-white">
           <span class="fa fa-file-pdf-o"/>
      </h:commandLink> 

Method that defines whether or not the user can generate the ticket and the ticket generation methods.

   public boolean podeGeraBoleto(Titulo bean) {
        return tituloBC.podeGeraBoleto(bean);
    }

    public String geraBoleto(Titulo bean) {
//        return "titulo_list.jsf";
        TituloTO tituloTO = tituloBC.geraTituloTO(bean);
        if (bean.getNossoNumero() == null || bean.getNossoNumero().isEmpty()) {
            tituloBC.geraNossoNumero(bean, tituloTO);
        } else {
            tituloTO.buildNossoNumeroTO();
        }
        if (tituloTO.getNossoNumeroTO() == null) {
            getMessageContext().add(getResourceBundle().getString("banco.msg.null"), SeverityType.WARN);
            return null;
        }

        byte[] pdf = GeraBoleto.gera(tituloTO);

        this.geraPDF(pdf, bean);

        return null;
    }

    public void geraPDF(byte[] pdf, Titulo bean) {
        if (pdf == null) {
            return;
        }

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=" + bean.getNumero() + " - " + bean.getParcela() + ".pdf");

        try {
            OutputStream out = response.getOutputStream();
            out.write(pdf);
            out.flush();
            out.close();
        } catch (IOException ex) {
            CriareLog.log(ex);
        }
    }

TituloBC

public boolean podeGeraBoleto(Titulo bean) {
        if (bean.getSituacao().isLiquidado()) {
            return false;
        }
        return bean.getPortador().getBanco().isCobranca() && bean.getPortador().getBloquetoEmissao().isCliente();
    }

Would anyone know how to download and, at the end of it, refresh the page?

    
asked by anonymous 24.06.2015 / 18:41

3 answers

0

It is not possible to give 2 answers, one being the file download and the other redirecting. Another thing with ajax will not work because it does not respond with the download of the file.

A gambirra is to create a condition in your .jsf to download after updated.

In your managedBean create a new variable

private boolean downloadPronto = false;

In your method geraBoleto(Titulo bean)

public String geraBoleto(Titulo bean)
{
    TituloTO tituloTO = tituloBC.geraTituloTO(bean);
    if (bean.getNossoNumero() == null || bean.getNossoNumero().isEmpty()) {
        tituloBC.geraNossoNumero(bean, tituloTO);
    } else {
        tituloTO.buildNossoNumeroTO();
    }
    if (tituloTO.getNossoNumeroTO() == null) {
        getMessageContext().add(getResourceBundle().getString("banco.msg.null"), SeverityType.WARN);
        return null;
    }

    // IMPORTATE NÃO GERAR O DOWNLOAD AINDA, SE NÃO SEU ATUALIZAR QUEBRA
    //byte[] pdf = GeraBoleto.gera(tituloTO);

    //this.geraPDF(pdf, bean);

    // CRIE ESSE ATRIBUTO INICILIZANDO COM 'false' 
    // E ATUALIZE PRA SABER QUANDO ESTÁ PRONTO
    this.downloadPronto = true;

    return null;
}

Once you've done this, create a condition in your% of%:

<c:if test="#{seuManagedBean.downloadPronto}">
     <script>
          window.onload = function() {
               document.getElementById('formDownload:link').onclick();
          }
     </script>

     /* pode deixar escondido visualmente, serve apenas pra disparar o download */
     <h:form id="formDownload">
          <h:commandLink id="link" action="#{seuManagedBean.downloadDireto()}"/>
     </h:form>
</c:if>

And in your code, create the .jsf

public String downloadDireto() {
    TituloTO tituloTO = tituloBC.geraTituloTO(bean);

    byte[] pdf = GeraBoleto.gera(tituloTO);
    this.geraPDF(pdf, bean);

    this.downloadPronto = false; // reseta variavel

    return null;
}

Basically how it works, when you click the button you already have it will update the values and update the new variable downloadDireto to true, when you return to the page it will enter the downloadPronto condition and will call the c:if of the button for there to download without having to update the values.

    
26.06.2015 / 21:08
0

Replace with this:

public void geraBoleto(Titulo bean) {

    TituloTO tituloTO = tituloBC.geraTituloTO(bean);
    if (bean.getNossoNumero() == null || bean.getNossoNumero().isEmpty()) {
        tituloBC.geraNossoNumero(bean, tituloTO);
    } else {
        tituloTO.buildNossoNumeroTO();
    }
    if (tituloTO.getNossoNumeroTO() == null) {
        getMessageContext().add(getResourceBundle().getString("banco.msg.null"), SeverityType.WARN);
        return null;
    }

    byte[] pdf = GeraBoleto.gera(tituloTO);

    this.geraPDF(pdf, bean);

    FacesContext.getCurrentInstance().getExternalContext().
    redirect("nomeDaSuaPagina.jsf");


}
    
26.06.2015 / 16:58
-1

Try to use update :

<h:commandLink action="#{repositorioCobrancaAddMB.geraBoleto(bean)}" 
                    actionListener="#{repositorioCobrancaAddMB.clear}" 
                    title="${messages['label.print']}" 
                    rendered="#{repositorioCobrancaAddMB.podeGeraBoleto(bean)}"
                    class="btn btn-white"
                    update=":SEU_FORM">
           <span class="fa fa-file-pdf-o"/>
      </h:commandLink> 
    
26.06.2015 / 21:08