0

I have successfully implemented the solution suggested by BalusC for rendering dynamic images inside a datatable (code posted further down here). Problem I am facing is this:

Issue

I am using pagination in datatable and when I move to the page which I have never visited, the images render fine. But when I come back to the page I had visited earlier, the images don't show up even though the StreamedContent is returned for that image.

Code

// ImageBean - SessionScoped        
// Get image for compound
public StreamedContent scaledImageById() {
    FacesContext context = FacesContext.getCurrentInstance();

    if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
        return new DefaultStreamedContent();
    } else {
        String idStr = context.getExternalContext().
                                       getRequestParameterMap().get("id");
        StreamedContent image = scaledCompoundImageMap.
                                              get(Integer.valueOf(idStr));
        return image;
    }
}


// Controller - ViewScoped
<p:dataTable id="cTable" value="#{controller.compoundList}" var="compound">
       <p:column headerText="Structure" style="text-align:center">
          <p:graphicImage id="scaledImage" 
                          value="#{imageBean.scaledImageById()}"     
                          cache="false">
                <f:param name="id" value="#{compound.id}" />
          </p:graphicImage>
       </p:column>   
  ...

So when I visit a new page in the paginator, the images display fine. But when I come back to an already visited page, the images are not shown (even though the scaledImageById is called twice and the StreamedContent is returned fine).

Please let me know if you need any other code here and any help would be much appreciated. Thanks.

Abdul

Basith
  • 831
  • 1
  • 7
  • 14
  • What requests are generated by the browser? You should trace your problem under firebug in network tab. – Danubian Sailor Sep 24 '13 at 11:45
  • Thanks Lukasz. Yes I checked the Network tab and I see Primefaces generating a dynamic url for each image. Something like this: 'dynamiccontent.properties.jsf?ln=primefaces&pfdrid=xxxxx&id=13. And when I click on that the first time I visit the paginator, it opens up the image fine but when I revisit the page and open up such a link, there is no image. Not sure why that dynamic link doesn't work the next time I visit the same page. – Basith Sep 24 '13 at 12:17

1 Answers1

0

From the HTML side, you have img element:

<img src="some_url?id=xyz"/>

If the images are stored in the database, and are never changed (uploading new image creates new image id in your entity), you can achieve more flexibility and performance creating custom servlet for images.

1) Create the servlet and bind it to 'some_url'

2) In the servlet, read the image from database (or anything else)

3) Set the headers, most important content type and content length, and cache headers

4) Write the content to the output stream

The plus is, if you have constant url for the image and good caching policy, the browser would fetch the image from the cache - the bean doesn't have to be called each time.

Danubian Sailor
  • 1
  • 38
  • 145
  • 223
  • Thanks for that solution Lukasz. I shall try that out now. It's sometimes frustrating to know that certain things are not so straightforward. Obviously Primefaces is not a one-technology-fits-all solution. Having come from a Flex background, I feel I need to do lots of workarounds (mostly to do with understanding build/view/render & lifecycle phases) to achieve the desired result which is a bit of burden on the developer. I shall keep hunting for new packages on the block and will keep exploring. Thanks again. – Basith Sep 25 '13 at 10:34