【问题标题】:Show Pdf using p:media in Primfaces [duplicate]在 Primfaces 中使用 p:media 显示 Pdf [重复]
【发布时间】:2017-12-16 21:33:39
【问题描述】:

我想查看在我的 Oracle 数据库中存储为 blob 的 pdf 文件。我在 xhml 中使用 <p:media/> ,在后备 bean 中使用 StreamedContent

这里是xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:c="http://java.sun.com/jsp/jstl/core"
  xmlns:p="http://primefaces.org/ui">

 <p:outputPanel id="outputPanelDialogShowPdf">

    <div style="text-align: center">
        <p:media value="#{bean.media}" width="600px" height="300px" player="pdf" />
    </div>

    <p:spacer height="1px" />
    <div style="text-align: center">
        <p:commandButton value="#{msgs.button_cancel}" icon="ui-icon-cancel" oncomplete="dialogShowPdf.hide();" 
                         action="#{bean[cancelAction]}" immediate="true" />
    </div>
</p:outputPanel>

这里是调用动作bean的动作按钮(它是数据表的一部分)

    <p:commandButton value="Show PDF"
                              update=":formMediaDataDialogShowPdf:outputPanelDialogShowPdf" 
                              oncomplete="PF('dialogShowPdf').show()" 
                              action="#{ bean.showPdfAction }" rendered="#{ mediaData.mediaType == 'application/pdf'}"> 
                 <f:setPropertyActionListener value="#{mediaData}" target="#{bean.selectedMediaData}" />
            </p:commandButton> 

这就是 bean 中的动作

public void showPdfAction(){

    if (null != selectedMediaData) { 
        media = new DefaultStreamedContent(new ByteArrayInputStream(selectedMediaData.getMediaData()),"application/pdf");   
    }
}

问题是媒体没有显示,即使 bean 中的操作没有错误地运行,并且 bean 中的 media 属性正在正确初始化。

任何人都可以为此提供一些帮助

【问题讨论】:

  • @kukeltje 你有什么想法阻碍代码吗?
  • 是的,我读过,但这与我要问的有什么关系吗?只是问问。
  • 感谢@Kukeltje .. 更新了问题。

标签: jsf primefaces media


【解决方案1】:

经过几个小时的研究,我找到了一个非常适合我的解决方案。

我已将 bean 中的操作更新如下

if (null != selectedMediaData) { 
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);

    session.setAttribute("pdfBytesArray", selectedMediaData.getMediaData());
}

现在,当我将pdfBytesArray 作为会话属性传递时,我创建了一个 servlet,我可以在其中从会话中获取此属性,然后处理字节数组,然后将其写入内容类型为 @ 的响应流987654323@

public class PdfPreviewServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    byte[] pdfBytesArray = (byte[]) request.getSession().getAttribute("pdfBytesArray");
    request.getSession().removeAttribute("pdfBytesArray");
    response.setContentType("application/pdf");
    response.setContentLength(pdfBytesArray.length);
    response.getOutputStream().write(pdfBytesArray);
} }

然后用@WebServlet("/pdfPreviewServlet")注释servlet

然后将&lt;p:media/&gt;改为&lt;p:media value="/pdfPreviewServlet" player="pdf"/&gt;

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2015-07-01
    • 2013-01-07
    • 1970-01-01
    • 2016-03-24
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多