问题 p:dataExporter无法识别p:cellEditor


我有一个可编辑的 <p:dataTable> 同 <p:cellEditor> 我想使用该表格的内容导出为PDF格式 <p:dataExporter>

我已经包括了 itext 2.1.7 jar。我得到了PDF格式的输出,但它显示了 Object#toString() 所有的价值观 <p:cellEditor> 像这样的组件:

org.primefaces.component.celleditor.CellEditor@1bd59e1

如何导出的输出值 <p:cellEditor> 代替?


8465
2018-01-19 05:23


起源

没有显示一些代码,我担心任何人都难以提供任何帮助。 - Arjan Tijms


答案:


<p:cellEditor> 确实不被PrimeFaces标准数据输出者所认可。 我以前曾向PF家伙报告此事 问题4013 以一个不仅提到的例子 CellEditor, 但也 HtmlGraphicImage (我们使用图像来显示布尔状态,其中 alt 我们想在PDF / XML / XLS / CSV报告中显示)。

首先,创建一个扩展标准的新类 PDFExporter 如下:

public class ExtendedPDFExporter extends PDFExporter {

    @Override
    protected String exportValue(FacesContext context, UIComponent component) {
        if (component instanceof CellEditor) {
            return exportValue(context, ((CellEditor) component).getFacet("output"));
        }
        else if (component instanceof HtmlGraphicImage) {
            return (String) component.getAttributes().get("alt");
        }
        else {
            return super.exportValue(context, component);
        }
    }

}

然后,要使用它,请以编程方式而不是通过它来调用它 <p:dataExporter>

<p:dataTable binding="#{table}" editable="true" ...>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column exportable="false"><p:rowEditor /></p:column>
</p:dataTable>
<h:commandLink value="PDF" action="#{bean.exportPDF(table, 'filename')}" />

public void exportPDF(DataTable table, String filename) throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();
    Exporter exporter = new ExtendedPDFExporter();
    exporter.export(context, table, filename, false, false, "UTF-8", null, null);
    context.responseComplete();
}

随意找到数据表 UIComponent#findComponent() 而是仅在动作方法中设置文件名。上面的代码只是示例。


12
2018-01-19 11:39



你是我的#1 JSF参考!你真棒! - fareed
在99%的案例中,Balus有正确的答案。此解决方案也适用于ExcelExporter。但是我在运行这个CSVxporter时遇到了问题 - Przemek
@Przemek:他们的 CSVExporter 在某些版本中确实存在一个错误,但这与具体问题无关。 - BalusC
@BalusC对不起。为我的案子开了一个问题 - Przemek
这是 <p:dataTable binding="#{table}" ... 实际代码还是只是作为占位符?由于特殊的绑定结构,我的页面抛出奇怪的异常并且不呈现。 - Kawu


我同意,我也发现这种方法可以最有效地定制Exporter行为,并且最不痛苦。

任何有兴趣使用的人 预处理/后处理 用这个方法?这是一个如何做到这一点的例子。

我敢于从上面的答案中略微修改方法:

public void exportPDF(DataTable table, String filename, 
        String preProcessor, String postProcessor) throws IOException {

    FacesContext context = FacesContext.getCurrentInstance();
    ExpressionFactory factory = context.getApplication().getExpressionFactory();

    MethodExpression preProcessorME = factory.createMethodExpression(
        context.getELContext(), preProcessor, null, new Class[] {Object.class});
    MethodExpression postProcessorME = factory.createMethodExpression(
        context.getELContext(), postProcessor, null, new Class[] {Object.class});

    Exporter exporter = new ExtendedPDFExporter();
    exporter.export(context, table, filename, false, false, "UTF-8", 
        preProcessorMe, postProcessorME);

    context.responseComplete();

}

这就是你在页面中使用它的方式(同样,我刚刚修改了上面的例子):

<p:dataTable binding="#{table}" editable="true" ...>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column exportable="false"><p:rowEditor /></p:column>
</p:dataTable>
<h:commandLink value="PDF" action="#{bean.exportPDF(table, 'filename', 
    '#{yourBean.preProcessPDF}', '#{yourBean.postProcessPDF}')}" />

请注意 没有嵌套的EL声明 (无论如何都不允许),最后两个参数是包含EL表达式的简单字符串。


4
2018-04-03 11:15



谢谢你工作得很好。我永远不会猜到它。 - ihebiheb