Dynamic Images with StreamedContent

I implemented two StreamedContent beans for dynamically loading a graphic image, after finding a solution here

1) When you first display images in contentFlow (ALL 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) In the second case, I try to display images using 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 does not work. Check ( context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) is always true.

If I change the scope of the Bean to RequestScopedwhat 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");
        }
      }
    }
+4
source share
1 answer

Since you are using simple fonts, try using p:repeat

Repeat - JSF PrimeFaces.

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

0

Source: https://habr.com/ru/post/1673492/


All Articles