How to link dynamic content using <p: media>?
1 answer
As in <p:graphicImage> , the value attribute can point to a bean property that returns a StreamedContent . This requires only a special getter method for reasons that are explained in detail in the following answer on using <p:graphicImage> with a dynamic resource from the database: Display a dynamic image from the database using p: graphicImage and StreamedContent .
In your specific example, it will look like this:
<p:media value="#{mediaManager.stream}" width="100%" height="300px" player="pdf"> <f:param name="id" value="#{bean.mediaId}" /> </p:media> FROM
@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())); } } } +9