primefaces update pdf media funktioniert nicht

Ich habe ein Problem im Zusammenhang mit Primefaces4 Meida Typ Rendering PDF-Datei im Browser. Ich habe das Beispiel in Showcase von der primefaces-Website erfolgreich ausprobiert. Jetzt möchte ich eine neue Funktion erhalten, die eine Baumstruktur mit Dokumentknoten im linken Bereich bietet. Der Benutzer kann ein Dokument auswählen, um es im mittleren Bedienfeld anzuzeigen. Dies bedeutet, dass ein PDF-Medienfeld in Backbean generiert wird, sobald der Benutzer ein Dokument in der Struktur auswählt.

Der zugehörige Code wird unten angezeigt:

backbean:

@ManagedBean
@ViewScoped
public class DocumentsBean implements Serializable {

private static final long serialVersionUID = 3560539268513760978L;
private TreeNode root;
private String url;
private TreeNode selectedNode; 
private StreamedContent media;

public DocumentsBean() {
    root = new DefaultTreeNode("Root");
}

public TreeNode getRoot() {
    return root;
}

public TreeNode getSelectedNode() {  
    return selectedNode;  
}  

public void setSelectedNode(TreeNode selectedNode) {  
    this.selectedNode = selectedNode;  
}  

public void onNodeSelect(NodeSelectEvent event) {  
    File file = (File) this.selectedNode.getData();
    generatePDF(file);
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public void explore() {
    root = new DefaultTreeNode(new File(this.url), null);
    constructDir(root);
}

/**
 * construct directory and its sub files.
 * @param parent
 */
private void constructDir(TreeNode parent) {
    File file = (File) parent.getData();
    File[] files = file.listFiles();
    for (File f: files) {
        if (f.isFile()) {
            new DefaultTreeNode("document", f, parent);
        } else {
            TreeNode subParent = new DefaultTreeNode(f, parent);
            constructDir(subParent);
        }
    }

}

private void generatePDF(File file) {
    PDFGenerator generator = new PDFGenerator(file);
    File pdf = generator.transformToPDF();

    if (pdf != null) {
        InputStream stream = null;
        try {
            stream = new FileInputStream(pdf);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        media = new DefaultStreamedContent(stream, "application/pdf");
    }

}

public StreamedContent getMedia() {
    return media;
}

}

Teil meiner Sichtweise:

<p:layoutUnit position="west" size="300" header="Directory Content" resizable="false" collapsible="true">
            <h:form id="docTree_form">
                <p:growl id="messages" showDetail="true" /> 
                <p:tree id="docTree" value="#{documentsBean.root}" var="node" animate="true" selectionMode="single" selection="#{documentsBean.selectedNode}" dynamic="true" cache="true">

                    <p:ajax event="select" update=":pdf_form:media" listener="#{documentsBean.onNodeSelect}" />
                    <p:treeNode expandedIcon="ui-icon-folder-open" collapsedIcon="ui-icon-folder-collapsed">
                        <h:outputText value="#{node.name}" />
                    </p:treeNode>

                    <p:treeNode type="document" icon="ui-icon-document">
                        <h:outputText value="#{node.name}" />
                    </p:treeNode>
                </p:tree>
            </h:form>
        </p:layoutUnit>

        <p:layoutUnit position="center" header="Center" resizable="true">
            <h:form id="pdf_form">
                <p:media id="media" value="#{documentsBean.media}"     player="pdf" width="100%" height="700px">  
                    Your browser can't display pdf 
                </p:media>
            </h:form>
        </p:layoutUnit>

Wenn ich diesen Code ausführe, gibt es keinen Fehler oder eine Ausnahme. In Firefox wird jedoch kein PDF-Viewer generiert. Wirklich seltsam !

eine Folgefrage basierend auf BalusC Kommentaren:

Ich habe diese Ausnahme erhalten, als meine App gestartet wird:

SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path     [/DocumentViewer_JSF] threw exception
java.lang.NullPointerException
at     org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:114)

Ich habe festgestellt, dass diese Zeile diese Ausnahme verursacht:

return new DefaultStreamedContent();

Wenn ich eine echte PDF-Datei erstelle, ist die Ausnahme weg. Ich möchte jedoch nicht, dass eine PDF-Datei angezeigt wird, wenn der Benutzer die Datei nicht ausgewählt hat.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage