【问题标题】:JasperReports SubDataset for jr:table from JasperPrint fillReport来自 JasperPrint fillReport 的 jr:table 的 JasperReports 子数据集
【发布时间】:2018-04-11 09:51:08
【问题描述】:

我正在尝试从 java 获取数据源到 jrxml 上的数据集以显示在 jr:table 上,但是当我尝试将数据导入数据集时它崩溃了,所以我猜错误来自导入或具有处理数据源的数据类型的参数。错误如下所示。有任何想法吗?全部。

错误:

[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 类 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>

【问题讨论】:

    标签: java collections hashmap jasper-reports pojo


    【解决方案1】:

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

    dados = new JRBeanCollectionDataSource(teste);
    

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

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

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

    不过,Jasper 还走得更远,包括精心设计的报表服务器和引擎。

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

    “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>();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多