【问题标题】:a4j:mediaOutput not rendering PDFa4j:mediaOutput 不呈现 PDF
【发布时间】:2015-02-25 14:32:38
【问题描述】:

我的任务是在单击链接时在我的 JSF 应用程序中显示从数据库中提取的 PDF 作为弹出窗口。但是我的模态面板没有呈现 PDF。相反,我在面板的左上角看到一个小的深灰色框。

这是我的 XHTML 代码:

<rich:column styleClass="viewUserTable">
<f:facet name="header">
    <h:outputText value="Pdf" />
</f:facet>
<h:outputLink value="#" id="link" style="color:blue;margin: 0 auto;">
    Proof
    <rich:componentControl for="panel" attachTo="link"
        operation="show" event="onclick" />
</h:outputLink>
<rich:modalPanel id="panel" width="350" height="100">
    <f:facet name="header">
        <h:outputText value="PDF"></h:outputText>
    </f:facet>
    <a4j:mediaOutput element="object" mimeType="application/pdf"
        id="media" session="false" createContent="#{getMyBean.showPdf}"
        value="1" style="width:800px; height:600px;" cacheable="false"
        standby="loading...">
    </a4j:mediaOutput>
</rich:modalPanel>

这是我在 bean 中调用的为 &lt;a4j:mediaOutput&gt; 创建内容的方法。

public void showPdf(OutputStream stream, Object object) throws SQLException, IOException {

     Blob proof= myObject.getPdf(someValue);//DB call to get the pdf
     InputStream inStream = proof.getBinaryStream();
     int length = -1;
     byte[] buffer = new byte[4096];

     while ((length = inStream.read(buffer)) != -1) {
         stream.write(buffer, 0, length);
     }  
}

我不确定&lt;a4j:mediaOutput&gt;value 属性的重要性,但我在互联网上找到了一个示例,其中从数据库中获取了类似的内容,并且该值设置为1。我使用的是RichFaces 3.3 .

【问题讨论】:

    标签: jsf pdf richfaces ajax4jsf


    【解决方案1】:

    我的一个朋友今天来找我这个问题,和你一样,我一直在努力想出一个解决方案。经过一段时间的研究,我可以解决这个问题。

    你的方法应该是这样的:

    public void showPdf(OutputStream stream, Object object) throws SQLException, IOException {
    
        Blob proof = myObject.getPdf(someValue);//DB call to get the pdf
    
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        ServletOutputStream sout = null;
    
        response.setContentType("application/pdf");
        //set a constant for a name to avoid cache and duplicate files on server 
        response.setHeader("Content-Disposition","filename=fileName_" + (new Date()).getTime() + ".pdf" );
    
        response.setHeader("Content-Transfer-Encoding", "binary;");
        response.setHeader("Pragma", " ");
        response.setHeader("Cache-Control", " ");
        response.setHeader("Pragma", "no-cache;");
        response.setDateHeader("Expires", new java.util.Date().getTime());
        //Here you will have to pass to total lengh of bytes
        //as I used a file I called length method
        response.setContentLength(Long.valueOf(  proof.someByteLengthProperty()  ).intValue());
    
        sout = response.getOutputStream();
        byte[] buf = new byte[4096];
        InputStream is = new FileInputStream(proof.getBinaryStream());
        int c = 0;
        while ((c = is.read(buf, 0, buf.length)) > 0) {
            sout.write(buf, 0, c);
        }
        sout.flush();
        is.close();
    
    }
    

    似乎只写出OutputStream 是不够的,因此您必须创建一个ServletOutputStream 并通过“FacesContext”发送它,这样它就可以像发送下载流一样呈现输出。

    【讨论】:

      猜你喜欢
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      相关资源
      最近更新 更多