Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 如何使用<;绑定动态内容;p:媒体>;?_Jsf_Jsf 2_Primefaces_Media_Dynamic Content - Fatal编程技术网

Jsf 如何使用<;绑定动态内容;p:媒体>;?

Jsf 如何使用<;绑定动态内容;p:媒体>;?,jsf,jsf-2,primefaces,media,dynamic-content,Jsf,Jsf 2,Primefaces,Media,Dynamic Content,我使用显示静态PDF内容 <p:media value="/resource/test.pdf" width="100%" height="300px" player="pdf"> </p:media> 如何将其更改为显示动态内容?与中一样,value属性可以指向返回流内容的bean属性。这只需要一个特殊的getter方法,其原因将在以下关于使用数据库中的动态资源的回答中详细解释: 在您的特定示例中,它将如下所示: <p:media v

我使用
显示静态PDF内容

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

如何将其更改为显示动态内容?

中一样,
value
属性可以指向返回
流内容的bean属性。这只需要一个特殊的getter方法,其原因将在以下关于使用数据库中的动态资源的回答中详细解释:

在您的特定示例中,它将如下所示:

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

如果我将ManagedBean保持在@ViewScope中会怎么样?它将失败:始终使用带有
的无状态bean来服务
流内容
@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()));
        }
    }

}