I have a big problem with GraphicImage and StreamedContents using PrimeFaces 5.1
My situation is the following:
I have a PNG raster stored in a PostgreSQL DB (version 9.4 with PostGIS 2.1 and GDAL 1.11); I create a bean method to perform a query to DB in order to get the stored image and show it on my UI page.
My bean:
Code: Select all
@ManagedBean(name="mapUrbanBean")
@SessionScoped
public class MapBean implements Serializable{
private StreamedContent image1;
//....
//bean getter
public StreamedContent getImage1() {
FacesContext context = FacesContext.getCurrentInstance();
if(context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE){
return new DefaultStreamedContent();
}else{
return image1;
}
}
}
//...
/**
* Note: dsm is an istance of my Class for DB connection management
*/
public void queryImage(){
//...
InputStream imgReaded;
dsm.setPreparedStatementRef("Select ST_AsPNG(ST_ColorMap(rast,1,colormap)) from my_raster_table order by data desc"); //this query uses PostGIS functions in order to extract the PNG image
dsm.runPreparedQuery();
if(dsm.next()){
imgReaded = dsm.getBStream(1); //getBStream retrieves the BinaryStream of the image
image1 = new DefaultStreamedContent(imgReaded , "image/png"); //save the image in the StreamedContent
}
}
//....
Code: Select all
<p:graphicImage id="rasterout" value="#{mapUrbanBean.image1}" binding="#{rasterout}" cache="false"/>
<p:commandButton title="showImg"
update=":#{rasterout.clientId} "
action="#{mapUrbanBean.queryImage}"
/>
In the tomcat log file I was able to see that getImage1 getter function is called during the page load phase...
Moreover the method that performs the Postgresql query works fine.
My project specs: PrimeFaces 5.1, Tomcat 6, JDK 1.7, JDBC 41 for Postgresql 9.4, Mojarra 2.2.2 (I'm using NetBeans with Maven)
Any idea? Thank you in advance and sorry for my bad english