【问题标题】:How to bind dynamic content using <p:media>?如何使用 <p:media> 绑定动态内容?
【发布时间】:2012-12-23 07:55:37
【问题描述】:

我使用&lt;p:media&gt; 来显示静态 PDF 内容。

<p:media value="/resource/test.pdf" 
         width="100%" height="300px" player="pdf">  
</p:media>

如何更改它以显示动态内容?

【问题讨论】:

    标签: jsf jsf-2 primefaces media dynamic-content


    【解决方案1】:

    &lt;p:graphicImage&gt; 一样,value 属性可以指向返回StreamedContent 的bean 属性。这只需要一个特殊的 getter 方法,原因在以下关于将&lt;p:graphicImage&gt; 与数据库中的动态资源一起使用的答案中进行了详细说明:Display dynamic image from database with p:graphicImage and StreamedContent

    在您的特定示例中,它看起来像这样:

    <p:media value="#{mediaManager.stream}" width="100%" height="300px" player="pdf">
        <f:param name="id" value="#{bean.mediaId}" />
    </p:media>
    

    @ManagedBean
    @ApplicationScoped
    public class MediaManager {
    
        @EJB
        private MediaService service;
    
        public StreamedContent getStream() throws IOException {
            FacesContext context = FacesContext.getCurrentInstance();
    
            if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
                // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
                return new DefaultStreamedContent();
            } else {
                // So, browser is requesting the media. Return a real StreamedContent with the media bytes.
                String id = context.getExternalContext().getRequestParameterMap().get("id");
                Media media = service.find(Long.valueOf(id));
                return new DefaultStreamedContent(new ByteArrayInputStream(media.getBytes()));
            }
        }
    
    }
    

    【讨论】:

    • 如果我将 ManagedBean 保留在 @ViewScoped 中会怎样?
    • 它将失败:stackoverflow.com/q/18994288 始终使用带有&lt;f:param&gt; 的无状态bean 来服务StreamedContent
    猜你喜欢
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2012-06-21
    • 1970-01-01
    • 2013-04-02
    相关资源
    最近更新 更多