3

我正在尝试从列表中生成一个简单的JR报告。

我不断收到错误从 bean 检索字段值:名称

此错误是由于错误的 getter 方法名称造成的,因为 jasper 使用反射从 bean 中获取字段。然而,即使在更正了 getter 方法名称之后。我不断收到这个异常。还有其他问题吗?

我的jrxml文件是

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jasperReport PUBLIC  "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport name="simpleReport">
  <field name="name" class="java.lang.String"/>
  <field name="count" class="java.lang.String"/>
  <title>
        <band height="50">
            <staticText>
          <reportElement x="0" y="0" width="180" height="15"/>
            <textElement/>
          <text><![CDATA[Report]]></text>
        </staticText>
        </band>
    </title>
  <pageHeader>
      <band/>
  </pageHeader>
  <columnHeader>
        <band height="20">
        <staticText>
          <reportElement x="180" y="0" width="180" height="20"/>
        <textElement>
            <font isUnderline="true"/>
        </textElement>
        <text><![CDATA[Event Name]]></text>
      </staticText>
      <staticText>
        <reportElement x="360" y="0" width="180" height="20"/>
        <textElement>
          <font isUnderline="true"/>
        </textElement>
        <text><![CDATA[Count]]></text>
      </staticText>
    </band>
  </columnHeader>
  <detail>
    <band height="20">
      <textField>
        <reportElement x="180" y="0" width="180" height="15"/>
        <textElement/>
        <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
      </textField>
      <textField>
        <reportElement x="360" y="0" width="180" height="15"/>
        <textElement/>
        <textFieldExpression><![CDATA[$F{count}]]></textFieldExpression>
      </textField>
    </band>
  </detail>
  <columnFooter>
    <band/>
  </columnFooter>
  <pageFooter>
    <band height="15">
      <staticText>
        <reportElement x="0" y="0" width="40" height="15"/>
        <textElement/>
        <text><![CDATA[Page:]]></text>
      </staticText>
      <textField>
        <reportElement x="40" y="0" width="100" height="15"/>
        <textElement/>
        <textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
      </textField>
    </band>
  </pageFooter>
  <summary>
    <band/>
  </summary>
</jasperReport>

豆类是

class EventBean {
    private String name;
    private String count;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCount() {
        return count;
    }
    public void setCount(String count) {
        this.count = count;
    }
}

class EventNameList {
    public ArrayList<EventBean> getDataBeanList() {
        ArrayList<EventBean> list = new ArrayList<EventBean>();

        list.add(generate("Flow", "100"));
        list.add(generate("Non flow", "300"));
        list.add(generate("Allow", "600"));
        list.add(generate("Deny", "50"));

        return list;
    }

    private EventBean generate(String name, String country) {
        EventBean bean = new EventBean();
        bean.setName(name);
        bean.setCount(country);

        return bean;
    }
}

我在这里生成报告

JasperCompileManager.compileReportToFile(inpuutjrxml, outputjasper);

EventNameList list = new EventNameList();
JRBeanCollectionDataSource beanList = new JRBeanCollectionDataSource(list.getDataBeanList());

JasperPrint jasperPrint = JasperFillManager.fillReport(outputjasper, new HashMap(), beanList);

JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream(pefoutput));

我们是否需要对 bean 类进行更多修改?

4

2 回答 2

5

解决方案很简单——你应该将JavaBean类的访问修饰符更改为public

像这样:

public class EventBean {
    private String name;
    private String count;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }
}

不要忘记您正在使用自己的软件包。


您可以在此处找到有关JavaBean 数据源的更多信息

于 2013-08-08T07:38:44.607 回答
0

Bean 属性应以小写字母开头

例子 :

public class DataBean {
     private String name;
     private String dateStart;
     private String DateEnd; //  ->dateEnd
     private String prixTotale;
     private String prixPayer;
于 2017-05-19T14:07:15.120 回答