0

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&amp;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.

ajeh
  • 2,372
  • 2
  • 28
  • 52
  • The class declaration of your `Bean` is missing. Is it `Serializable`? It should be. About the `@SessionScoped`, did you give the name `Bean` to your bean? – cheffe Sep 16 '13 at 04:58
  • The code of your `Bean` does not compile when trying to copy'n'paste it. The return statement needs to be moved out of the if block. – cheffe Sep 16 '13 at 05:02
  • 1
    This problem has the same grounds as already answered here: http://stackoverflow.com/questions/8207325/display-image-from-database-with-pgraphicimage/12452144#12452144 In a nutshell, **do not** have a `StreamedContent` as instance variable. Instead, create it in the getter based on an identifier as request parameter. – BalusC Sep 16 '13 at 13:25
  • @BalusC: Implementing the linked has fixed 'single use images' issue with the individual graphicImage tags, but not the graphicImages within the list box unfortunately :( My beans are annotated Named, ManagedBean, SessionScoped. Tried with and without the 1st two and it did not make any difference. Now that the code is fixed, it actually works with both imports I mentioned earlier for SessionScoped. Any tips on how to get list view's images working would be greatly appreciated! – ajeh Sep 16 '13 at 18:00
  • @BalusC: It's working! The parameter name was supposed to be **"id"** in the call to **getRequestParameterMap().get("id");** Guess that's how PrimeFaces works. You are a gem! – ajeh Sep 16 '13 at 19:00
  • You can just choose any parameter name you want, as long as it's the same on both sides. So if you send a `` then you should obviously use `get("foo")`. – BalusC Sep 16 '13 at 20:07
  • @BalusC: Yes, what I mean is that in the linked answer param name="id" did not match get("stidentId") and it did not work until I replaced it with get("id"). They have to match in the param element and get call. – ajeh Sep 17 '13 at 00:20

0 Answers0