【问题标题】:Dynamic images with StreamedContent带有 StreamedContent 的动态图像
【发布时间】:2017-03-29 11:21:24
【问题描述】:

按照找到的解决方案,我已经实现了两个 StreamedContent bean 来动态加载图形图像 here

1) 第一个我在 contentFlow 中显示图像(一切正常)

<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) 对于第二个,我尝试使用 foreach(或重复)显示图像:

 <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>

这不起作用。检查 (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) 始终为真。

如果我将 Bean 的范围更改为 RequestScoped,它就会起作用!我很困惑...有人可以帮助我吗?

托管 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");
        }
      }
    }

【问题讨论】:

  • 您能提供您的托管 bean 代码吗?
  • 我已将其添加到问题中
  • If I change the scope of the Bean to RequestScoped than it works 。您的意思是在此处更改ImageStreamer 的范围吗?这对您的应用程序来说根本不是问题。
  • 对不起,我出了点问题...现在它正在工作...我真的不知道为什么...

标签: primefaces jsf-2 graphicimage


【解决方案1】:

由于您使用的是 primefaces,请尝试使用 p:repeat

Repeat 是标准重复组件的扩展,用于提供 JSF 实现和 PrimeFaces 组件之间的互操作性。

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

【讨论】:

  • 抱歉,我出了点问题……现在它可以工作了……我真的不知道为什么……我用过 c:foreach
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 2017-02-03
相关资源
最近更新 更多