0

我在我的项目中使用 Primefaces 5.1、Primefaces Extensions 3.0.0 和 GlassFish 4.0,但 pe:documentViewer 有很多问题。我的代码是:

  • 在 xhtml 中

    <f:event listener="#{previewComponent.onPrerender}" type="preRenderView"/>
    <p:tab title="#{text['attach.documento']}" class="borderless nopadding">
        <pe:documentViewer value="#{previewComponent.file2}"/>
    </p:tab>
    
  • 在豆

    公共类 PreviewComponent 扩展 BasePage 实现 Serializable {

    private String filePath;
    
    private StreamedContent file2;
    
    //events
    public void onPrerender(ComponentSystemEvent event) {
        try {
            if (this.filePath == null) {
                if (log.isDebugEnabled()) {
                    log.debug("OnPreRender: filePath is null!");
                }
                ByteArrayOutputStream out = new ByteArrayOutputStream();
    
                Document document = new Document();
                PdfWriter.getInstance(document, out);
                document.open();
    
                for (int i = 0; i < 50; i++) {
                    document.add(new Paragraph("Ficheiro não encontrado."));
                }
    
                document.close();
                setFile2(new DefaultStreamedContent(new ByteArrayInputStream(out.toByteArray()), "application/pdf"));
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("OnPreRender: filePath is " + this.filePath);
                }
                String fileName = FilenameUtils.getName(filePath);
                String mimeType = this.getFacesContext().getExternalContext().getMimeType(fileName);
                if (log.isDebugEnabled()) {
                    log.debug("fileName: " + fileName);
                    log.debug("mimeType: " + mimeType);
                }
                setFile2(new DefaultStreamedContent(new FileInputStream(filePath), mimeType));
            }
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("OnPreRender: error is " + e.getMessage());
            }
        }
    }
    
    //GETTERS and SETTERS
    public String getFilePath() {
        return filePath;
    }
    
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
    
    public StreamedContent getFile2() {
        return file2;
    }
    
    public void setFile2(StreamedContent file2) {
        this.file2 = file2;
    }
    

    }

最初我有以下错误:

严重:生命周期处理期间出现异常 java.lang.RuntimeException:这里没有名称为 Faces Servlet 的 Web 组件。

在我在 web.xml 中添加这个之后:

<servlet-mapping>
    <servlet-name>faces</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

在此之后我有更多的错误。为什么?请给我一些答案:D

4

0 回答 0