0
I am implementing a page where I need to display a datagrid with the values of a table in the database. Each table row has a blob image.
But when trying to display the image with the primefaces by the tag : it does not appear.
below has the part of the code of the entity in which has the property of the image and the function that puts it in the Defaultstreamedcontent format that is supported by the tag of the first faces.
@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 code part 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, along with the name of each of them.
I don’t know the context of your application, but it’s not a good practice to record images directly in the database. You could save these images to the server where the application is creating a folder/subfolder structure if necessary. In the database you would only save the path or name of the image, in a common varchar field.
– electus