a4j:mediaOutput无法呈现PDF [英] a4j:mediaOutput not rendering PDF

查看:120
本文介绍了a4j:mediaOutput无法呈现PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是在单击链接时在JSF应用程序中显示从数据库中提取的PDF作为弹出窗口.但是我的模式面板没有渲染PDF.取而代之的是,我在面板的左上角有一个暗灰色的小框.

I have a task of displaying a PDF fetched from the database as a pop up in my JSF application upon clicking a link. However my modal panel isn't rendering the PDF. Instead I get a small dark grey box at the top left corner of the panel.

这是我的XHTML代码:

Here's my XHTML code:

<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中调用的为<a4j:mediaOutput>创建内容的方法.

Here's the method I'm calling in my bean to create content for <a4j:mediaOutput>.

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

我不确定<a4j:mediaOutput>value属性的重要性,但是我在互联网上找到了一个示例,该示例从DB获取相似的内容,并且该值设置为1.我使用RichFaces 3.3 .

I am not sure of the significance of the value attribute in the <a4j:mediaOutput> but I found an example on internet where similar content was being fetched from DB and and the value was set as 1. I m using RichFaces 3.3.

推荐答案

今天,我的一个朋友遇到了这个问题,就像您一样,我一直在努力寻找解决方案.经过一段时间的研究,我可以解决这个问题.

A friend of mine came to me with this problem today and like you I've struggled to come up with a solution. After some time studying servlet I could solve the problem.

您的方法应类似于:

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'将其发送出去,以便它可以像输出发送下载流一样呈现输出.

It seems that only writing out the OutputStream is not enough so you have to create a ServletOutputStream and send it throghout the 'FacesContext' so it can render the output as if it was sending a download stream.

这篇关于a4j:mediaOutput无法呈现PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆