2016-04-19 59 views
1

我想將我的DataBean(TestModelA)映射到現有的XML,但JasperReports給我一個例外。問題是我有自定義字段(TestModelB),並且在該類中有公共getter和setter,但碧玉不識別它們。我怎麼解決這個問題?Jasper Reports不復合DataBean

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 

回答

1

你逝去的TestModelA類作爲數據源,你需要或者創建或從碧玉報告正確地引用方法在這個類中的方法。

方案1 TestModelA

public Long getOwner() { 
    return getTestModelB().getOwner(); //Handle NullPointer? 
} 

解決方案在JRXML創建方法2參考正確

<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> 
+0

感謝您的回答! – quento

+0

解決方案3:將報告字段名稱從所有者更改爲testModelB.owner – dada67