如何使用 <p:media> 绑定动态内容? [英] How to bind dynamic content using <p:media>?

查看:24
本文介绍了如何使用 <p:media> 绑定动态内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 来显示静态 PDF 内容.

I use the <p:media> to display static PDF content.

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

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

How can I change it to display dynamic content?

推荐答案

一样,value 属性可以指向一个 bean 属性返回 StreamedContent.这只需要一个特殊的 getter 方法,原因在以下关于将 <p:graphicImage> 与数据库中的动态资源一起使用的答案中详细解释:使用 p:graphicImage 和 StreamedContent 显示数据库中的动态图像.

Like as in <p:graphicImage>, the value attribute can point to a bean property returning StreamedContent. This only requires a special getter method for the reasons which is explained in detail in the following answer on using <p:graphicImage> with a dynamic resource from a database: Display dynamic image from database with p:graphicImage and StreamedContent.

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

In your particular example, it would look like this:

<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()));
        }
    }

}

这篇关于如何使用 &lt;p:media&gt; 绑定动态内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆