Database image displayed in primefaces does not appear

0

I'm implementing a page where I need to display a datagrid with the values of a table in the database. Each row in the table has a blob-like image.

But when you try to display the image with the primefaces by the:: tag, it does not appear.

Below is the part of the entity code where you have the image property and the function that places it in the DefaultStreamedContent format that is supported by the primefaces tag.

@Lob
@Basic(fetch= FetchType.LAZY)
private byte[] imagem;

public Projetista() {

}

public StreamedContent mostrarImagemDeByte(){
    InputStream in = null;
    StreamedContent sc;
    if(this.imagem != null){
        in = new ByteArrayInputStream(this.imagem);
    }

    if( in != null){
        sc  = new DefaultStreamedContent(in);
    }else{
        sc = null; 
    }
         return sc;
}

below the part of the code of the page where I use to display the image

<div id="envolveTexto">
                <div id="texto">

                    <p:dataGrid columns="3" value="#{projetistaBean.listaDeProjetistas}" var="projetista" >

                        <p:panelGrid columns="1">
                            <p:graphicImage value="#{projetista.mostrarImagemDeByte()}" width="70" height="70" />
                            <h:outputText value="#{projetista.nome}" />
                        </p:panelGrid>

                    </p:dataGrid>

                </div>
             </div>

It was to appear a datagrid with the images that are in the database, together with the name of each one of them.

    
asked by anonymous 21.07.2014 / 21:14

2 answers

2

Solved !!

The graphicImage does not work if it is used inside a set tag such as the datagrid if the images are saved in a database.

If you need to display them within a datagrid, you should save the images to temporary folders inside the application server and then reference them by id.

I did this and it worked out.

    
22.07.2014 / 22:09
0

Instead of:

sc  = new DefaultStreamedContent(in);

Try:

sc  = new DefaultStreamedContent(in, "image/png");

It's just a detail, but often that kind of detail is the "x" of the question.

Another thing, change the ShowByteType method name to getByteByte picture.

    
21.07.2014 / 21:24