I am trying to show the embedded PDF that opens in a new browser window. I have the following script:
Now, on this page my PDF is not generated. In the log I see two lines:
org.primefaces.application.PrimeResourceHandler handleResourceRequest SEVERE: Error in streaming dynamic resource. Expression cannot be null
I began to debug and learn a few things.
First, I added a breakpoint to the @PostConstruct method of my RequestScoped bean. What is interesting is that the breakpoint is reached twice, and to my great surprise, after the PDF is displayed perfectly ?!
After some debugging through PrimeResourceHandler I find out that in some cases ValueExpression not calculated, it actually throws a NullPointerException , and again during debugging I saw that two requests are sent and the second request fails because dynamicContentId is deleted in the first request. and the second call to handleResourceRequest does not make sense.
Through Firebug, I see two requests, firstly, it is good with PDF data, and secondly, it is also with the content type / pdf, but empty, with a size of 0.
xhtml page:
<html> <h:head></h:head> <h:body> <p:media value="#{reportBean.streamedContent}" player="pdf" width="500" height="500"/> </h:body> </html>
bean support:
@RequestScoped public class StampaListeBackingBean implements Serializable { private static final long serialVersionUID = 1L; private StreamedContent streamedContent; @PostConstruct public void init() { Map<String, Object> session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap(); byte[] b = (byte[]) session.get("reportBytes"); if (b != null) { streamedContent = new DefaultStreamedContent(new ByteArrayInputStream(b), "application/pdf"); } } public StreamedContent getStreamedContent() { if (FacesContext.getCurrentInstance().getRenderResponse()) { return new DefaultStreamedContent(); } else { return streamedContent; } } public void setStreamedContent(StreamedContent streamedContent) { this.streamedContent = streamedContent; } }
I need to understand why two requests are sent to a page with the p:media tag and figure out how to do this. The backup bean is the request area, it creates the StreamedContent method in @PostConstruct and has getter and setter fields for this. The private version is 3.4.2, with Mojarra 2.1.14.
ADDED:
Easy to reproduce my problem. If the code in the init method is replaced with the following:
FileInputStream fis = new FileInputStream(new File("C:\\samplexxx.pdf")); streamedContent = new DefaultStreamedContent(fis, "application/pdf");
The problem may be reproduced.