Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pdf PrimeFaces p:媒体不使用@ViewScope bean中的StreamedContent_Pdf_Jsf_Primefaces_Media_View Scope - Fatal编程技术网

Pdf PrimeFaces p:媒体不使用@ViewScope bean中的StreamedContent

Pdf PrimeFaces p:媒体不使用@ViewScope bean中的StreamedContent,pdf,jsf,primefaces,media,view-scope,Pdf,Jsf,Primefaces,Media,View Scope,我有一个问题与Primefaces4 meida类型在浏览器中呈现pdf文件有关。我在primefaces网站的showcase中成功地尝试了该示例。现在我想得到一个新特性,它提供了一个树结构,在左面板中有文档节点。用户可以选择一个文档以在中心面板中显示它。这意味着,一旦用户选择树上的一个文档,它就会在backbean中生成pdf媒体字段 相关代码如下所示: 后座: @ManagedBean @ViewScoped public class DocumentsBean implements Se

我有一个问题与Primefaces4 meida类型在浏览器中呈现pdf文件有关。我在primefaces网站的showcase中成功地尝试了该示例。现在我想得到一个新特性,它提供了一个树结构,在左面板中有文档节点。用户可以选择一个文档以在中心面板中显示它。这意味着,一旦用户选择树上的一个文档,它就会在backbean中生成pdf媒体字段

相关代码如下所示:

后座:

@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;
}

}
我的部分看法是:

<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>
我发现导致此异常的此行:

return new DefaultStreamedContent();

如果我创建了一个真正的pdf文件,那么异常就消失了。但是,如果用户没有选择pdf文件,我真的不希望显示该文件。

您的具体问题是因为webbrowser实际上是在一个物理上完全独立的HTTP请求中下载pdf文件,而不是在HTTP请求中,HTTP请求是基于JSF源代码生成和发送HTML输出的。您可能已经知道视图范围的bean通过
javax.faces.ViewState
hidden输入字段绑定到特定的JSF视图。如果该值被更改或不存在,那么请求将获得一个新的、不同视图范围的bean实例

换句话说,当浏览器在一个单独的HTTP请求中从服务器下载PDF文件时,它使用的不是相同的
@ViewScoped
bean实例,而是一个全新的、完全独立的实例,该实例不具有相同的属性(state)作为绑定到页面的内容,因此整个
StreamedContent
在该点仅为
null

StreamedContent
的此问题与
StreamedContent
的问题有着本质上相同的原因,这两个问题之前已被多次回答:

在您的特定情况下,您需要重新设计整个包,使
DocumentsBean
支持bean通过唯一标识符将PDF文件存储在某个位置(例如临时磁盘、服务器内存、数据库等),然后将该唯一标识符作为请求参数传递给
,如下所示:

<p:media value="#{mediaManager.stream}" width="100%" height="700px" player="pdf">
    <f:param name="id" value="#{documentsBean.mediaId}" />
</p:media>

太好了,真管用!你的博客真的很有帮助。实际上,我刚刚开始使用JSF2,所以我在识别这个问题的类别方面没有太多经验。我还在吸收。
<p:media value="#{mediaManager.stream}" width="100%" height="700px" player="pdf">
    <f:param name="id" value="#{documentsBean.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()));
        }
    }

}