Check if there is a file in a certain folder rendered jsf

2

Personal I'm saving image to a particular project folder and I need to check if this file exists in the folder. I was using a command as below:

rendered="Fotos/#{consultaFuncionariosBean.pessoaModel.codigo != null}.png"

But it did not work. Does anyone know how I can resolve this?

Resolved as Below.

Method.

public boolean existeArquivo(String file) {
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    String nomeSaida = externalContext.getRealPath("") + "resources" + File.separator + "Fotos" + File.separator + file + ".png";
    File Arqs = new File(nomeSaida);
    boolean success = Arqs.exists();
    return success;
}

Form.

    <p:graphicImage rendered="#{photoCamBean.existeArquivo(consultaFuncionariosBean.pessoaModel.codigo)==false}" library="Fotos" name="Modelo.jpg" cache="false"/>
    <p:graphicImage rendered="#{photoCamBean.existeArquivo(consultaFuncionariosBean.pessoaModel.codigo)==true}" name="Fotos/#{consultaFuncionariosBean.pessoaModel.codigo}.png" cache="false"/>
    
asked by anonymous 05.07.2017 / 15:50

1 answer

1

The rendered attribute is Boolean.

Do something like this:

@ManagedBean
public class TesteMB {

    private File arquivo;
    private boolean arquivoExiste;

    public boolean getArquivoExiste() {
        return arquivo != null;
    }
}

And then:

rendered="#{testeMB.arquivoExiste}"

Or to make it easier, you can do the verification directly in the view:

@ManagedBean
public class TesteMB {

    private File arquivo;

    public File getArquivo() {
        return arquivo;
    }
}

And then:

rendered="#{testeMB.arquivo != null}"
    
05.07.2017 / 16:21