我想将我的DataBean(TestModelA)映射到现有的XML,但JasperReports给了我一个例外。问题是我有自定义字段(TestModelB),我在该类中有公共getter和setter,但是jasper无法识别它们。我该如何解决这个问题?
TestModelA:
public class TestModelA {
private Long id;
private String label_key;
private TestModelB testModelB;
public TestModelA(Long id, String label_key, TestModelB testModelB) {
this.id = id;
this.label_key = label_key;
this.testModelB = testModelB;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLabel_key() {
return label_key;
}
public void setLabel_key(String label_key) {
this.label_key = label_key;
}
public TestModelB getTestModelB() {
return testModelB;
}
public void setTestModelB(TestModelB testModelB) {
this.testModelB = testModelB;
}
}
TestModelB:
public class TestModelB {
private Long owner;
public TestModelB(Long owner) { this.owner = owner; }
public Long getOwner() {
return owner;
}
public void setOwner(Long owner) {
this.owner = owner;
}
}
我的XML文件:
<?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="report3" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="077cd490-b99c-4ed1-ad76-b15714625957">
<property name="ireport.zoom" value="2.1435888100000016"/>
<property name="ireport.x" value="94"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[SELECT MODULE.ID, MODULE.LABEL_KEY, MODULE.OWNER FROM MODULE]]>
</queryString>
<field name="id" class="java.lang.Long"/>
<field name="label_key" class="java.lang.String"/>
<field name="owner" class="java.lang.Long"/>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="21" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="185" height="20" uuid="68ea9590-9d19-4bc5-9540-d059208b0ce8"/>
<text><![CDATA[id]]></text>
</staticText>
<staticText>
<reportElement x="185" y="0" width="185" height="20" uuid="325690ac-7218-4000-8ec6-a492d185477a"/>
<text><![CDATA[label_key]]></text>
</staticText>
<staticText>
<reportElement x="370" y="0" width="185" height="20" uuid="20521e4b-9057-42a9-8adc-10309abc8935"/>
<text><![CDATA[owner]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="23" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="185" height="20" uuid="11901f0e-b03d-47f8-a8aa-35bab36d26bf"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="185" y="0" width="185" height="20" uuid="b715cb6b-3650-4266-a1cf-552ee35bc08f"/>
<textFieldExpression><![CDATA[$F{label_key}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="370" y="0" width="185" height="20" uuid="5c41b534-a74e-4b91-b35d-fcbb88789000"/>
<textFieldExpression><![CDATA[$F{owner}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
我得到例外:
Exception in thread "main" net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : owner
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)
at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100)
at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:1330)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1231)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1207)
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1554)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:149)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:841)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:88)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:653)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:969)
at net.didorenko.Reporter.creatReport(Reporter.java:99)
at net.didorenko.Reporter.threeReport(Reporter.java:78)
at net.didorenko.Reporter.main(Reporter.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.NoSuchMethodException: Unknown property 'owner' on class 'class net.didorenko.model.TestModelA'
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1313)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
... 20 more
答案 0 :(得分:1)
您正在将TestModelA
类作为数据源传递,您需要在此类中创建方法或从jasper报告中正确引用该方法。
解决方案1在TestModelA
public Long getOwner() {
return getTestModelB().getOwner(); //Handle NullPointer?
}
解决方案2在jrxml中正确引用
<field name="testModelB" class="net.didorenko.model.TestModelB"/>
...
<textField>
<reportElement x="370" y="0" width="185" height="20" uuid="5c41b534-a74e-4b91-b35d-fcbb88789000"/>
<textFieldExpression><![CDATA[$F{testModelB}.getOwner()]]></textFieldExpression>
</textField>