Pick up object by clicking on a commandLink in the Datatable

Asked

Viewed 641 times

1

I’m trying to catch an object by clicking on a commandLink that is in each column of Datatable, but always from the nullPointerException error. Can anyone help?

I’m doing it this way:

<p:commandLink id="ajax"  ajax="false" process="@this"
    actionListener="#{controleAuditoriaBean.prepDownload}" value="#{controleAuditoria.caminhoArquivo}">
            <f:setPropertyActionListener
                target="#{controleAuditoriaBean.solicitacoesBD}"
                            value="#{controleAuditoria}" />                         
                    <p:fileDownload value="#{controleAuditoriaBean.download}" />    
</p:commandLink>

Method:

public void prepDownload() throws Exception {
        System.out.println("Caminho "+solicitacoesBD.getCaminhoArquivo());
        File file = new File(solicitacoesBD.getCaminhoArquivo());

        InputStream input = new FileInputStream(file);
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
        System.out.println("PREP = " + download.getName());
    }

1 answer

0


Techies, your problem is using the actionListener whereas the correct would be just action.

The problem of actionListener is that he executes before the action.

Your code would stand:

<p:commandLink id="ajax"  ajax="false" process="@this"
    action="#{controleAuditoriaBean.prepDownload}" value="#{controleAuditoria.caminhoArquivo}">
            <f:setPropertyActionListener
                target="#{controleAuditoriaBean.solicitacoesBD}"
                            value="#{controleAuditoria}" />                         
                    <p:fileDownload value="#{controleAuditoriaBean.download}" />    
</p:commandLink>
  • It worked, but I have to click 2x on the link to start the download of the file. Your click 1x it takes the right object but the download does not start.

  • @Techies You can go back to actionListener and tries to pass the value by parameter instead of f:setPropertyActionListener, experiment!

  • To pass by parameter do not need to use the f:setPropertyActionListener?

  • It depends on the version of your jsf, which is? :)

  • I believe it’s 2.2

  • You can do it actionListener="#{controleAuditoriaBean.prepDownload(controleAuditoria)}"

  • I went back to the actionListener, you just need to change your method prepDownload to receive this type of parameter

  • to receive this parameter in my method I use requestContext?

  • or just put the object in the method to receive this parameter? for example prepDownload(Auditoria a)

  • @Techies only put the object in the method, like its last example

  • 1

    Thank you again Maicon. You have helped me a lot.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.