来自JasperPrint fillReport的jr:table的JasperReports SubDataset

时间:2018-04-11 09:51:09

标签: java collections hashmap jasper-reports pojo

我试图将java中的数据源放到jrxml上的数据集中以显示在jr:table上,但是当我尝试将数据放入数据集时它会崩溃,所以我猜错误来自于import或具有处理数据源的数据类型的参数。错误显示如下。有任何想法吗? Ty all。

错误:

[JRException: Errors were encountered when compiling report expressions class file:
1. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
                value = ((net.sf.jasperreports.engine.JRBeanCollectionDataSource)parameter_dados.getValue()); //$JR_EXPR_ID=8$
                          <---------------------------------------------------->
2. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
                value = ((net.sf.jasperreports.engine.JRBeanCollectionDataSource)parameter_dados.getValue()); //$JR_EXPR_ID=8$
                          <---------------------------------------------------->
3. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
                value = ((net.sf.jasperreports.engine.JRBeanCollectionDataSource)parameter_dados.getValue()); //$JR_EXPR_ID=8$
                          <----------------------------------------------------> 3 errors .]

处理Jasper报告的Java方法:

public File getRelatorioPdf(AuditoriaEntity auditoriaEntity) throws JRException, IOException {

        File pdf = null;
        Map<String, Object> parametros = new HashMap<>();
        parametros.put("objetivo", "TESTE OBJETIVO");
        parametros.put("dataInicio", "09/05/2017");
        parametros.put("dataFim", "09/05/2017");
        parametros.put("equipa", "Equipa TESTE");
        parametros.put("coordenador", "Coordenador TESTE");
        parametros.put("processo", "PROCESSO TESTE");
        parametros.put("ambito", "AMBITO TESTE");

        //TESTE DE LISTA
        JRBeanCollectionDataSource dados = null;
        List <testeLista> teste = new ArrayList<testeLista>();
        teste.add(new testeLista("teste1", "1", "2"));
        teste.add(new testeLista("teste2", "3", "4"));
        teste.add(new testeLista("teste3", "5", "6"));
        teste.add(new testeLista("teste4", "7", "8"));
        teste.add(new testeLista("teste5", "9", "10"));
        teste.add(new testeLista("teste6", "11", "12"));
        dados = new JRBeanCollectionDataSource(teste);

        final String jasperReportPath = "jasper-reports/relatorio_auditoria/relatorio_auditoria.jrxml";
        InputStream inputStreamTemplate = this.environment.resourceAsStream(jasperReportPath);

        JasperReport report = JasperCompileManager.compileReport(inputStreamTemplate);
        JasperPrint print = JasperFillManager.fillReport(report, parametros, dados);
        pdf = File.createTempFile("temp", ".pdf");
        pdf.deleteOnExit();
        JasperExportManager.exportReportToPdfStream(print, new FileOutputStream(pdf));

        return pdf;
    }

Java Class testeLista:

public class testeLista{
    private String naoConformidade;
    private String numNC;
    private String numOBS;

    public testeLista(String naoConformidade, String numNC, String numOBS) {
        this.naoConformidade = naoConformidade;
        this.numNC = numNC;
        this.numOBS = numOBS;
    }

    public String getNaoConformidade() {
        return naoConformidade;
    }

    public void setNaoConformidade(String naoConformidade) {
        this.naoConformidade = naoConformidade;
    }

    public String getNumNC() {
        return numNC;
    }

    public void setNumNC(String numNC) {
        this.numNC = numNC;
    }

    public String getNumOBS() {
        return numOBS;
    }

    public void setNumOBS(String numOBS) {
        this.numOBS = numOBS;
    }
}

贾斯珀报告:

<?xml version="1.0" encoding="UTF-8"?>

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1e9d3196-bef6-4b86-97d2-b6eb6e579cec">
    <import value="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"></import>
    <!--<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>-->
    <subDataset name="Dataset1">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="naoConformidade" class="java.lang.String"/>
        <field name="numNC" class="java.lang.String"/>
        <field name="numOBS" class="java.lang.String"/>
    </subDataset>
    <parameter name="dados" class="net.sf.jasperreports.engine.JRBeanCollectionDataSource"/>
    <parameter name="coordenador" class="java.lang.String"/>
    <parameter name="processo" class="java.lang.String"/>
    <parameter name="ambito" class="java.lang.String"/>
    <parameter name="objetivo" class="java.lang.String"/>
    <parameter name="equipa" class="java.lang.String"/>
    <parameter name="dataFim" class="java.lang.String"/>
    <parameter name="dataInicio" class="java.lang.String"/>
<pageFooter>
        <band height="160" splitType="Stretch">
            <!--<textField>
                <reportElement x="10" y="0" width="100" height="20"/>
                <textFieldExpression><![CDATA[$F{naoConformidade}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="110" y="0" width="30" height="20"/>
                <textFieldExpression><![CDATA[$F{numNC}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="140" y="0" width="30" height="20"/>
                <textFieldExpression><![CDATA[$F{numOBS}]]></textFieldExpression>
            </textField>-->

            <componentElement>
                <reportElement x="0" y="0" width="160" height="20">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.column_header"/>
                    <property name="com.jaspersoft.studio.table.style.detail"/>
                </reportElement>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="Dataset1">
                        <datasetParameter name="REPORT_DATA_SOURCE">
                            <datasetParameterExpression><![CDATA[$P{dados}]]></datasetParameterExpression>
                        </datasetParameter>
                        <!--<dataSourceExpression><![CDATA[$P{dados}]]></dataSourceExpression>-->
                    </datasetRun>
                    <jr:column width="100">
                        <jr:detailCell height="20">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="20"/>
                                <textFieldExpression><![CDATA[$F{naoConformidade}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="30">
                        <jr:columnHeader height="20">
                            <staticText>
                                <reportElement x="0" y="0" width="30" height="20"/>
                                <text><![CDATA[NC]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell height="20">
                            <textField>
                                <reportElement x="0" y="0" width="30" height="20"/>
                                <textFieldExpression><![CDATA[$F{numNC}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="30">
                        <jr:columnHeader height="20">
                            <staticText>
                                <reportElement x="0" y="0" width="30" height="20"/>
                                <text><![CDATA[OBS]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell height="20">
                            <textField>
                                <reportElement x="0" y="0" width="30" height="20"/>
                                <textFieldExpression><![CDATA[$F{numOBS}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </pageFooter>
</jasperReport>

1 个答案:

答案 0 :(得分:0)

错误告诉您变量“dados”在此行的构造函数中没有可接受的“java对象类型”。

dados = new JRBeanCollectionDataSource(teste);

变量引用“teste”应该是java.util.Collection OF JavaBean对象。

请注意,类“JRBeanCollectionDataSource”应仅包含引用(作为java.util.Collection)运行代码的应用程序的服务器配置的“已注册bean类”。

将Java Bean添加到列表时,它们的id引用应该与配置文件中的相同,尽管服务器线程可以访问其“Application”对象以获取所有配置的bean的集合。

然而,Jasper还进一步发展,包括一个深思熟虑的报表服务器和引擎。

您应该指定它是故意单独的Jasper报表服务器还是与JSP引擎和servlet引擎系统集成到早期服务器类型。

TOO

“testeLista”对象更明智地写为TestLista

// Declare case sensitive class name first letter is capital (java convention of class naming)
public class TesteLista{.....
// normal class name first letter capitalized
List <TesteLista> teste = new ArrayList<TesteLista>();