Primefaces, p: fileDownload ajax="false" nullPointerException

2

Hello, I'm new here, I already searched for Google's life and here on the Stack something similar but did not find. I am not as experienced as DEV so who can help ...

The problem is p:DownloadFile . At least I think it's there. It gives a 500 error of nullpointerException and the log apparently says nothing. I use the primefaces framework. In xhtml I have the datatable and the button column:

    <p:dataTable value="#{lancamentoRCCadastroMB.arquivos}" var="arquivo" styleClass="small"   
                                scrollable="true"  scrollHeight="150" id="gridNotaFiscalDigiRC"
                                emptyMessage="Nenhum registro encontrado."
                                rowSelectMode="click" rowHover="true">

            <p:column headerText="Arquivo" styleClass="align-center" >
                    <p:commandButton id="btnDownload2RC" ajax="false" actionListener="#{lancamentoRCCadastroMB.downloadAnexoLancamento(arquivo.codigo)}"
                                            icon="fa fa-download state-primary" title="Download" value="#{arquivo.nome}"
                                            styleClass="rowClick" immediate="true" process="gridNotaFiscalDigiRC" rendered="#{lancamentoRCCadastroMB.analistaOrcamentario}">
                        <p:fileDownload value="#{lancamentoRCCadastroMB.streamedContentAnexoLancamento}" />                     
                    </p:commandButton>              
            </p:column>
    </p:dataTable>

No bean:

public void downloadAnexoLancamento(Integer pCodigo) {

    ArquivoLancamentoVO lArquivo = new ArquivoLancamentoVO();
    if(arquivos != null && !arquivos.isEmpty()) {

        for(ArquivoLancamentoVO lArquivoLoop : arquivos) {
            if(pCodigo.equals(lArquivoLoop.getCodigo())) {
                lArquivo = lArquivoLoop;
                break;
            }
        }

        if(lArquivo.getCodigo() != null) {
            ByteArrayInputStream stream = new ByteArrayInputStream(lArquivo.getArquivo());
            streamedContentAnexoLancamento = new DefaultStreamedContent(stream, "application/x-download", lArquivo.getNome());
        }           
    }
}

It turns out that when you click the id="btnDownload2RC" button, you should call the bean method downloadAnexoLancamento and prepare the file for download. Debugging method is not invoked when ajax="false" . If set to true it even calls the method but does not download the file. I've heard that these prime processes and updates may be messing me up, but I've already checked the code and can not find fault.

    
asked by anonymous 21.08.2018 / 16:47

1 answer

0

It may be that the listener is being called after running getStreamedContentAnexoLancamento() , so do not create the streamed content. Try to make these changes:

In XHTML:

<p:commandButton
    id="btnDownload2RC"
    icon="fa fa-download state-primary"
    title="Download"
    value="#{arquivo.nome}"
    ajax="false"
    styleClass="rowClick"
    immediate="true"
    process="gridNotaFiscalDigiRC"
    rendered="#{lancamentoRCCadastroMB.analistaOrcamentario}"
    >
    <p:fileDownload value="#{lancamentoRCCadastroMB.downloadAnexoLancamento(arquivo.codigo)}" />
</p:commandButton>

No Bean:

public StreamedContent downloadAnexoLancamento(Integer pCodigo) {

    ArquivoLancamentoVO lArquivo = new ArquivoLancamentoVO();

    if(arquivos != null && !arquivos.isEmpty()) {

        for (ArquivoLancamentoVO lArquivoLoop : arquivos) {
            if(pCodigo.equals(lArquivoLoop.getCodigo())) {
                lArquivo = lArquivoLoop;
                break;
            }
        }

        if(lArquivo.getCodigo() != null) {
            ByteArrayInputStream stream = new ByteArrayInputStream(lArquivo.getArquivo());
            streamedContentAnexoLancamento = new DefaultStreamedContent(stream, "application/x-download", lArquivo.getNome());
        }           
    }

    return streamedContentAnexoLancamento;
}
    
31.08.2018 / 18:36