1

I have an application that calls a service that returns a Json string with about ten images as byte array. I loop through the Json string and pull out the images and convert them to StreamedContent and place them into an object then populate a List with the object that has the image and name of that image. I am trying to display those image in a JSF page with the PrimeFaces tag, but the images are not displaying. My code follows:

ImageBackingBean

    private String sku;
private StreamedContent image;
private List<ImageBackingBean> imageList;

public String getSku(){
    return sku;
}
public void setSku(String sku){
    this.sku = sku;
}
public StreamedContent getImage() {
    FacesContext context = FacesContext.getCurrentInstance();
    if(context.getRenderResponse()){
        return new DefaultStreamedContent();
    }else{
        String id = context.getExternalContext().getRequestParameterMap().get("sku");
        List<ImageBackingBean> list = getImageList();
        for(ImageBackingBean bean : list){
            if(bean.getSku().equalsIgnoreCase(id)){
                return bean.getImage();
            }
        }
    }
    return image;
}
public void setImage(StreamedContent image) {
    this.image = image;
}

public List<ImageBackingBean> getImageList(){
    return imageList;
}

public void setImageList(List<ImageBackingBean> imageList){
    this.imageList = imageList;
}

public String getImageFromDatabase() throws IOException{
    setImageList(DBImages.getImages());
    if(getImageList() == null){
        return "error";
    }
    return "success";
}

My JSF page is simple and is as follows:

                <h:dataTable var="items" value="#{imageBackingBean.imageList}">
                <h:column>
                    <p:graphicImage value="#{items.image}">
                        <f:param name="sku" value="#{items.sku}" />
                    </p:graphicImage>
                </h:column>
            </h:dataTable>

Everything I've read online tells me this is how I need to display dynamic images. Should I not store the images as type StreamedContent in the list?

stirling
  • 143
  • 2
  • 3
  • 8
  • No, this is not the right way. You should not have *any* `StreamedContent` property at all and the `` must be `#{imageBackingBean.image}` and not `#{items.image}` (and preferably be a completely separate application scoped bean). Duplicate of http://stackoverflow.com/questions/8304967/how-to-use-pgraphicimage-with-streamedcontent-within-pdatatable/8305079#8305079 and http://stackoverflow.com/questions/10944673/how-to-use-pgraphicimage-with-defaultstreamedcontent-in-an-uirepeat/10951201#10951201 – BalusC Nov 15 '12 at 20:22
  • @BalusC Thank you for the post, I've updated my code as per the other posts. But the images are still not rendering, I'm getting an error: No mime type could be found for file dynamiccontent. To resolve this, add a mime-type mapping to the applications web.xml. I've added the mime-mapping in the web.xml and still nothing. Any suggestions? – stirling Nov 16 '12 at 14:50
  • This error suggests that you're still doing it the wrong way and didn't understood my previous comment. I said: you should have **no** `StreamedContent` property. The whole `private StreamedContent image` line in your code should be removed altogether and the logic should be altered on that in order to perform the job on a per-request basis. See also the in the previous comment linked answers. Here's another one, hopefully making it a bit more clear: http://stackoverflow.com/a/10161878 Without seeing your updated code it's hard to point out your actual mistake. – BalusC Nov 16 '12 at 14:59

0 Answers0