如何在jaspersoft报告中添加表中的java bean集合 [英] How to add collection of java bean in table in jaspersoft report

查看:127
本文介绍了如何在jaspersoft报告中添加表中的java bean集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在报告中使用jaspersoft。我需要在表中添加我的java bean字段集合,我使用java bean集合作为数据适配器。我能够显示我作为java bean获取的数据,但我没有找到一种方法将这些字段添加到jaspersoft报告中的表中。请帮助我从过去的一些数据中窃听这个问题仍然不是我的问题。以下是我正在尝试的源代码。

I am using jaspersoft for my report. I need to add my collection of java bean fields in the table which I got it using collection of java beans as the data adapter. I am able to display data that I get as java beans, but I am not finding a way to add those fields to the table in jaspersoft report. Please help I am been bugging around this from past few data still I am not iron out of this issue. Below is the source code on what i am trying.

    <?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2015-12-23T15:07:03 -->
<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="javabeantest1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="78330407-cdc3-4758-b2a3-2ddae526bed9">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="test factory3"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="javabean" uuid="adf401bd-6c3e-4316-8ff1-285d86d56c48">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="TestFactory1"/>
        <queryString language="SQL">
            <![CDATA[]]>
        </queryString>
        <field name="name" class="java.lang.String"/>
        <field name="age" class="java.lang.Integer"/>
    </subDataset>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="name" class="java.lang.String"/>
    <field name="age" class="java.lang.Integer"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="87" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="33" splitType="Stretch">
            <staticText>
                <reportElement x="70" y="3" width="100" height="30" uuid="2c3c0b2c-ef69-423b-ba00-47bfb52dfade"/>
                <text><![CDATA[name]]></text>
            </staticText>
            <staticText>
                <reportElement x="170" y="1" width="100" height="30" uuid="8c4db8c2-212f-4427-89b7-5eaf3091a1aa"/>
                <text><![CDATA[age]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="31" splitType="Stretch">
            <textField>
                <reportElement x="70" y="0" width="100" height="30" uuid="39fd9f41-27dd-406f-aa32-d6e930dc346a"/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="170" y="0" width="100" height="30" uuid="1c916b19-cb2f-481c-8d0d-349eced5a8a2"/>
                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="249" splitType="Stretch">
            <componentElement>
                <reportElement x="290" y="49" width="200" height="200" uuid="0d721a59-5052-42a4-bd71-b61e51308115">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                </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="javabean" uuid="31830583-c665-4c8d-af3a-1a5a0aba5306">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource() ]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="100" uuid="89affd55-2820-4860-a01c-ce794c5941a9">
                        <jr:tableHeader style="Table 1_TH" height="30"/>
                        <jr:tableFooter style="Table 1_TH" height="30"/>
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="33b03723-89a8-4eba-b08a-e9360ecf3e08"/>
                                <text><![CDATA[name]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table 1_CH" height="30"/>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="54e2604e-96f8-4e83-b506-ca2beeef5751"/>
                                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100" uuid="41fcbd02-87dc-4a72-8464-d9609e5e592a">
                        <jr:tableHeader style="Table 1_TH" height="30"/>
                        <jr:tableFooter style="Table 1_TH" height="30"/>
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="f4078108-0c94-406e-907f-d20a0932afeb"/>
                                <text><![CDATA[age]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table 1_CH" height="30"/>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="b1250d44-7bb0-445f-8c46-ab6c880872a8"/>
                                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </summary>
</jasperReport>


推荐答案

你的java bean必须是这样的(对应的)到您的 subDataset 命名为javabean)。

Your java bean needs to be something like this (correspond to your subDataset named javabean).

public class JavaBean {
  private String name;
  private int age;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}

然后你需要一个 List< ; JavaBean> 的JavaBean。

Then you need a List<JavaBean> of the JavaBean's.

让我们假设您将此列表传递为填充过程中的参数(在参数映射中使用键 listOfJavaBeans )。

Lets imagine that you pass this List as a parameter during the fill process (in the parameter map with the key listOfJavaBeans).

in jrxml 定义参数

<parameter name="listOfJavaBeans" class="java.util.List"/> 

将其传递到您的表作为数据源使用

<datasetRun subDataset="javabean" uuid="31830583-c665-4c8d-af3a-1a5a0aba5306">
    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{listOfJavaBeans})]]></dataSourceExpression>
</datasetRun>

这篇关于如何在jaspersoft报告中添加表中的java bean集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆