Playing with PrimeFaces, I successfully produced dynamically generated image and displayed it in p:graphicImage, but a selectOneListbox is not displaying the images, generated by the same code.
JSF:
<p:selectOneListbox id="imgListBox" value="#{Bean.selectedimg}" var="img" style="height:600px; width: 400px">
<f:selectItems value="#{Bean.availableimgs}" var="img" itemLabel="#{img.imgName}" itemValue="#{img}" />
<p:column>
<p:graphicImage value="#{img.picture}" alt="#{img.imgFileName}"/>
</p:column>
<p:column>
<p:outputLabel id="imgNameLabel" value="#{img.imgName}"/>
</p:column>
</p:selectOneListbox>
Bean:
private StreamedContent Picture;
public StreamedContent getPicture() throws IOException {
if (Picture == null) {
...
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ImageIO.write(im, "png", byteStream);
// ImageIO.write(im, "png", new File(String.format("/home/user/Desktop/IMG/%s.png", imgFile.getName())));
this.Picture = new DefaultStreamedContent(new ByteArrayInputStream(byteStream.toByteArray()), "image/png");
return this.Picture;
}
}
All that's there is alt text. Dumping images into a file in a commented out line works fine. Dumping returned StreamedContent object into a label works fine too and displays its string representation:
org.primefaces.model.DefaultStreamedContent@47afcfac
org.primefaces.model.DefaultStreamedContent@1b43fbba
and so on. Image src tags also seem to be populated similarly to the working single image above:
/testJSF/faces/javax.faces.resource/dynamiccontent.properties?ln=primefaces&pfdrid=pfdrid_d905ac48-0fb4-4d2c-8282-9c84a714a92c
but no images in any of the browsers I've access to (Epiphany, IE9 32/64bit, FF13 Linux, FF21 Win7)
Trying such and such to get list to work, I noticed that NetBeans always 'Fixed imports' for @SessionScoped bean annotation by adding import javax.enterprise.context.SessionScoped and the page worked (except graphics). The beans in PrimeFaces demo examples are injected by way of faces-config.xml as request scoped. If I used @RequestScoped annotation with import javax.faces.bean.RequestScoped; then nothing related to the bean would work. Also if I changed import for @SessionScoped annotation to javax.faces.bean.SessionScoped then nothing would work too. Can anyone explain what annotations should be used on the beans backing PrimeFaces JSF and with what imports, as this is too confusing for a noob like me?
Worst part is that simple graphicsImage works only once with @SessionScoped bean importing javax.enterprise.context.SessionScoped. If I refreshed the page, than images would be gone (actual URLs would change and nothing would be at the old URLs) until clean/build is repeated, and the method calls would not be re-entered in debugger during the refresh.
I am obviously doing something wrong.