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?
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
属性可以指向返回的 bean 属性StreamedContent
。这只需要一个特殊的 getter 方法,原因在以下关于使用<p:graphicImage>
数据库中的动态资源的答案中有详细说明:使用 p:graphicImage 和 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()));
}
}
}