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

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

问题描述

我使用<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?

推荐答案

<p:graphicImage>中一样,value属性可以指向返回StreamedContent的bean属性.出于以下原因,这仅需要一种特殊的getter方法,有关将<p:graphicImage>与数据库中的动态资源一起使用的以下答案中对此进行了详细说明:

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天全站免登陆