How to bind dynamic content using

?

后端 未结 1 501
独厮守ぢ
独厮守ぢ 2020-11-27 19:49

I use the to display static PDF content.



        
相关标签:
1条回答
  • 2020-11-27 20:28

    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>
    

    With

    @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()));
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题