4

I have implemented two StreamedContent beans to dynamic load a graphic image, following the solution found here

1) With the first one I display images in a contentFlow (EVERYTHING OK)

<p:contentFlow value="#{imageBean.images}" var="image">
           <p:graphicImage value="#{imageStreamer.fileContent}" styleClass="content" cache="false">
                  <f:param name="index" value="#{image.index}"/>
            </p:graphicImage>
</p:contentFlow>

2) With the second one I try to display images using a foreach (or repeat):

 <c:forEach items="#{imageBean.images}" var="item" varStatus="varStatus">
          <p:graphicImage value="#{imageStreamer.fileContent}" cache="false">
                            <f:param name="index" value="#{varStatus.index}" />
          </p:graphicImage>
</c:forEach>

This doesn't work. The check (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) is always true.

If I change the scope of the Bean to RequestScoped than it works! I'm confused... can someone help me?

Managed bean:

@ManagedBean
@ApplicationScoped
public class ImageStreamer{

      public ImageStreamer(){}

      public StreamedContent getFileContent(){
        List<Link> links = this.getLinkItems();
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

        FacesContext fc = FacesContext.getCurrentInstance();
        String linkIndex = externalContext.getRequestParameterMap().get("index");

        if(fc.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE){
          return new DefaultStreamedContent();
        }else{

          int parsedIndex = Integer.parseInt(linkIndex);
          Link ql = links.get(parsedIndex);
          return new DefaultStreamedContent(new ByteArrayInputStream(ql.getBytes()), "image/png");
        }
      }
    }
Community
  • 1
  • 1
Neo
  • 1,171
  • 3
  • 17
  • 42

1 Answers1

0

Since you are using primefaces, try using p:repeat

Repeat is an extension to standard repeat component to provide interoperability between JSF implementations and PrimeFaces components.

https://www.primefaces.org/showcase/ui/data/repeat.xhtml

David Florez
  • 1,370
  • 2
  • 9
  • 25
  • Sorry, I had something wrong... now it's working... I really don't know why... I have used c:foreach – Neo Apr 04 '17 at 07:59