I need to show JRBeanCollectionDataSource data in Table component (JasperReports).
Here is my template, ShowPerson.jrxml file:
<?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="ShowPerson" pageWidth="612" pageHeight="792" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="304c4c4e-c99a-4399-8081-748d3b7c0b8c">
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="Table Dataset 1" whenResourceMissingType="Empty" uuid="63b01547-bce2-47c9-ba15-666f94d11387">
<queryString language="SQL">
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String"/>
<field name="age" class="java.lang.Integer"/>
</subDataset>
<parameter name="INFO" class="java.lang.String"/>
<title>
<band height="40" splitType="Stretch">
<staticText>
<reportElement uuid="e96450a8-8358-4cae-a094-3add06d57de2" x="0" y="20" width="56" height="20"/>
<textElement/>
<text><![CDATA[Info]]></text>
</staticText>
<textField>
<reportElement uuid="e21e9932-ebfe-4bb5-904d-ea99e141866b" x="56" y="20" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$P{INFO}]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="40" splitType="Stretch">
<componentElement>
<reportElement uuid="dea5d821-81b6-434b-ae27-4f3a0268e805" key="table 1" x="0" y="0" width="572" height="40"/>
<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="Table Dataset 1" uuid="39f8f0bf-8a2b-44f4-9a4c-0c873da79fba">
<datasetParameter name="REPORT_DATA_SOURCE">
<datasetParameterExpression><![CDATA[]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
</datasetRun>
<jr:column width="169" uuid="aae649c4-6a69-485f-8a0d-87022df793ee">
<jr:tableHeader height="20" rowSpan="1">
<staticText>
<reportElement uuid="795dac43-0ff0-482c-89a0-7dac3b27d513" x="0" y="0" width="169" height="20"/>
<textElement/>
<text><![CDATA[Name]]></text>
</staticText>
</jr:tableHeader>
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement uuid="4f1ab13a-d776-4cd5-a2a7-a5cf23360816" x="0" y="0" width="169" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="173" uuid="a6997eea-131e-4555-80e6-a20d4decb18f">
<jr:tableHeader height="20" rowSpan="1">
<staticText>
<reportElement uuid="5f6e2413-8025-47ca-9638-9609ea13f93f" x="0" y="0" width="173" height="20"/>
<textElement/>
<text><![CDATA[Age]]></text>
</staticText>
</jr:tableHeader>
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement uuid="195d51a0-9e45-4201-ad67-d3026ce2e72c" x="0" y="0" width="173" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
My POJO:
public class Person {
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;
}
}
Main class for building report:
public class OpenReport {
public static void main(String[] args) throws JRException {
Map<String, Object> peopleMap = new HashMap<>();
peopleMap.put("Sisco", 17);
peopleMap.put("Eve", 19);
peopleMap.put("John", 20);
peopleMap.put("George", 21);
peopleMap.put("Steve", 18);
ArrayList<Person> dataList = new ArrayList<Person>();
for(Map.Entry<String, Object> personMap : peopleMap.entrySet()) {
Person person = new Person();
person.setName(personMap.getKey());
person.setAge(Integer.valueOf(personMap.getValue().toString()));
dataList.add(person);
}
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
parameters.put("INFO", "Hello");
JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, beanColDataSource);
JFrame frame = new JFrame("Report");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JRViewer(jasperPrint));
frame.pack();
frame.setVisible(true);
}
}
1.Send your datasource from the server as a parameter, and fill the report with a different one (can be empty).
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
parameters.put("INFO", "Hello");
parameters.put("DS1", beanColDataSource);
JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
2.Decalre a new parameter in the report of type JRBeanCollectionDataSource
<parameter name="DS1" class="net.sf.jasperreports.engine.JRBeanCollectionDataSource"/>
3.Set TableDatasource to use the $P{DS1} parameter.
<jr:table ...>
<datasetRun subDataset="Table Dataset 1">
<datasetParameter name="REPORT_DATA_SOURCE">
<datasetParameterExpression><![CDATA[$P{DS1}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
.....
4.Declare the fields (name, age) in Table Dataset 1.
5.In the table, in the DetailBand add a TextField in each column with the corresponding field ($F{name} and $F{age} respectively).
#laura: I get new error when trying set the TableDatasource to use the $P{DS1} parameter.
net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
Now, I have found the solution, maybe not the best.
Map parameters = new HashMap();
parameters.put("INFO", "Hello");
parameters.put("DS1", dataList);
JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
Modify the class of $P{DS1} to java.util.Collection or java.util.List
And set the table datasource to
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{DS1})
Related
I am trying to fill the jasper report using customized sql qyery but I am getting this error:-
java.lang.NoSuchMethodException: Unknown property 'vehicle_class' on class 'class [Ljava.lang.Object;'
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1270) ~[commons-beanutils-1.9.4.jar:1.9.4]
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:809) ~[commons-beanutils-1.9.4.jar:1.9.4]
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:885) ~[commons-beanutils-1.9.4.jar:1.9.4]
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:464) ~[commons-beanutils-1.9.4.jar:1.9.4]
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:185) ~[jasperreports-6.17.0.jar:6.17.0-6d93193241dd8cc42629e188b94f9e0bc5722efd]
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:170) ~[jasperreports-6.17.0.jar:6.17.0-6d93193241dd8cc42629e188b94f9e0bc5722efd]
This is my Jasper File
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.17.0.final using JasperReports Library version 6.17.0-6d93193241dd8cc42629e188b94f9e0bc5722efd -->
<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="ViolationClassAndAverageSpeed" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="dcaca94b-6e83-47dc-bd14-baca011b1845">
<style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="Dataset1" uuid="1f3f11d6-c688-4625-b387-2808efd481fc">
<queryString language="SQL">
<![CDATA[SELECT vehicle_class, COUNT(*)/SUM(1e0/speed) AS speed FROM violations GROUP BY vehicle_class]]>
</queryString>
<field name="vehicle_class" class="java.lang.String"/>
<field name="speed" class="java.lang.Double"/>
<group name="vehicle_class">
<groupExpression><![CDATA[$F{vehicle_class}]]></groupExpression>
</group>
</subDataset>
<queryString language="SQL">
<![CDATA[SELECT vehicle_class, COUNT(*)/SUM(1e0/speed) AS speed FROM violations GROUP BY vehicle_class]]>
</queryString>
<field name="vehicle_class" class="java.lang.String"/>
<field name="speed" class="java.lang.Double"/>
<group name="vehicle_class">
<groupExpression><![CDATA[$F{vehicle_class}]]></groupExpression>
</group>
<detail>
<band height="349" splitType="Stretch">
<componentElement>
<reportElement x="10" y="10" width="200" height="200" uuid="5df1aded-bce1-4d9a-8c73-1950d7161a02">
<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="Dataset1" uuid="a331d750-4a48-46e4-9f1c-cac4cfbf2e72">
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
</datasetRun>
<jr:column width="100" uuid="4545a2e7-6253-4848-a00b-063337993b35">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:groupHeader groupName="vehicle_class">
<jr:cell style="Table_CH" height="30"/>
</jr:groupHeader>
<jr:groupFooter groupName="vehicle_class">
<jr:cell style="Table_CH" height="30"/>
</jr:groupFooter>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="ec2d5ab9-d65e-4485-acbc-a41aad6097d8"/>
<text><![CDATA[vehicle_class]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="b46de1f3-0544-4e4b-a52e-0d46e5884010"/>
<textFieldExpression><![CDATA[$F{vehicle_class}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="fdb570b8-20de-42d9-ba5a-38f593535eb7">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:groupHeader groupName="vehicle_class">
<jr:cell style="Table_CH" height="30"/>
</jr:groupHeader>
<jr:groupFooter groupName="vehicle_class">
<jr:cell style="Table_CH" height="30"/>
</jr:groupFooter>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="2c555423-d793-45d4-96f0-e9603c6edac6"/>
<text><![CDATA[speed]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="f5539efd-3550-4a93-9df1-3e172ce72d6a"/>
<textFieldExpression><![CDATA[$F{speed}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
Repository
#Repository
public interface ViolationRepository extends JpaRepository<Violation, Long>{
#Query(value = "SELECT vehicle_class, COUNT(*)/SUM(1e0/speed) AS avg_speed FROM violations GROUP BY vehicle_class", nativeQuery = true)
List<Object> findCustomAll();
}
Controller Class
#RequestMapping( "/pdf")
public void getReportsinPDF(HttpServletResponse response) throws JRException, IOException {
InputStream jasperStream = (InputStream) this.getClass().getResourceAsStream("/ViolationClassAndAverageSpeed.jasper");
// Adding attribute names
Map<String, Object> params = new HashMap<>();
// Fetching the student from the data database.
final JRBeanCollectionDataSource source = new JRBeanCollectionDataSource(violationnService.getViolation());
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, source);
response.setContentType("application/x-pdf");
response.setHeader("Content-disposition", "inline; filename=ViolationList.pdf");
final ServletOutputStream outStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
}
Entity Class
#Entity
#Table(name = "violations")
public class Violation {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "violation_id")
private long violation_id;
#Column(name = "speed", length = 3)
private Integer speed;
#Column(name = "vehicle_class", length = 10)
private String vehicle_class;
public String getVehicle_class() {
return vehicle_class;
}
public void setVehicle_class(String vehicle_class) {
this.vehicle_class = vehicle_class;
}
public Integer getSpeed() {
return speed;
}
public void setSpeed(Integer speed) {
this.speed = speed;
}
}
Service Class:
#Service
public class ViolationService {
#Autowired
private ViolationRepository violationRepo;
public List<Object> getViolation() {
return violationRepo.findCustomAll();
}
}
I'm using jdbc adapter and I am trying to fill jasper report using this query in my repository
SELECT vehicle_class, COUNT(*)/SUM(1e0/speed) AS speed FROM violations GROUP BY vehicle_class
I removed field description from report but its still not worth it.
I have been trying to create a jasper report which should look like the following image:
For my sample report, Out-1 and Out-2 are rows for the outer table and asd, asds, adasd are rows for the inner table.
I am trying to achieve it using nested tables. But the table takes the field and I can not assign field to the inner-table.
I am creating jasper reports in Java code rather than using SQL connection to the table for some reason.
JXML
<?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ec55a262-fec4-45f4-9b48-5be98088aafa">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<subDataset name="New Dataset 1" uuid="cdef25ec-abc4-45ba-b70e-28d82716626b">
<field name="sachNr" class="java.lang.String"/>
<field name="akundenNr" class="java.lang.String"/>
<field name="nestedTable" class="java.lang.String"/>
</subDataset>
<subDataset name="New Dataset 2" uuid="eec983aa-a227-4a28-9c44-73cbe31fa024">
<field name="packStNr" class="java.lang.String"/>
<field name="prodDatum" class="java.lang.String"/>
<field name="stueck" class="java.lang.String"/>
</subDataset>
<parameter name=" artikeldatenTable" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<componentElement>
<reportElement key="table" x="0" y="28" width="360" height="50" uuid="fae14075-18a8-4fcc-b6f9-c14e3623e75d"/>
<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="New Dataset 1" uuid="17a10558-3a43-47c2-809d-6362924e5015">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(1)]]></dataSourceExpression>
</datasetRun>
<jr:column width="137" uuid="7a8ca2ff-7a1a-41a0-8c30-b02f7be6e2b9">
<jr:tableHeader height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="137" height="30" uuid="0fe0e502-dea8-45f6-9f96-633f5db90cd8"/>
<textFieldExpression><![CDATA[$F{sachNr}]]></textFieldExpression>
</textField>
</jr:tableHeader>
<jr:detailCell height="42" rowSpan="1">
<componentElement>
<reportElement key="table" x="0" y="0" width="137" height="42" uuid="9e72ca8c-182d-41c0-a021-98493162c7da"/>
<jr:table>
<datasetRun subDataset="New Dataset 2" uuid="7475e256-4bc5-4e88-be92-b6d2eb2fd4c5">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
</datasetRun>
<jr:column width="90" uuid="cd851138-c894-459f-91c9-636c3602b247">
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="ce21338e-c593-4bae-b7c4-08741127575c"/>
<textFieldExpression><![CDATA[$F{packStNr}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="5f2b25a9-e34a-4698-b1ab-e8843dafec3c">
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="5487305d-d22f-46fa-97bc-88e671f7085c"/>
<textFieldExpression><![CDATA[$F{prodDatum}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="93c28f55-9bda-4be0-989a-03ce573715ba">
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="53c4e2b3-7591-49b3-9b13-50cc1f16641a"/>
<textFieldExpression><![CDATA[$F{stueck}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="f898d2e7-1c2f-48fe-9357-61ee5d767a51">
<jr:tableHeader height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="30" uuid="16b70095-2900-46f0-96ec-d638edb784f3"/>
<textFieldExpression><![CDATA[$F{akundenNr}]]></textFieldExpression>
</textField>
</jr:tableHeader>
<jr:detailCell height="42" rowSpan="1"/>
</jr:column>
<jr:column width="111" uuid="0121d9c8-d1f0-447f-84ed-15385693a072">
<jr:tableHeader height="30" rowSpan="1"/>
<jr:detailCell height="42" rowSpan="1"/>
</jr:column>
<jr:column width="90" uuid="ca27e48c-a1d6-4c32-a1de-2580b6786b3d">
<jr:tableHeader height="30" rowSpan="1"/>
<jr:detailCell height="42" rowSpan="1"/>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Outer Table POJO
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
public class OuterTable {
private String sachNr;
private String akundenNr;
private String SUBREPORT_DIR;
private JRBeanCollectionDataSource nestedTable;
public String getSachNr() {
return sachNr;
}
public void setSachNr(String sachNr) {
this.sachNr = sachNr;
}
public String getAkundenNr() {
return akundenNr;
}
public void setAkundenNr(String akundenNr) {
this.akundenNr = akundenNr;
}
public String getSUBREPORT_DIR() {
return SUBREPORT_DIR;
}
public void setSUBREPORT_DIR(String sUBREPORT_DIR) {
SUBREPORT_DIR = sUBREPORT_DIR;
}
public JRBeanCollectionDataSource getNestedTable() {
return nestedTable;
}
public void setNestedTable(JRBeanCollectionDataSource nestedTable) {
this.nestedTable = nestedTable;
}
}
Inner Table POJO
public class InnerTable {
private String packStNr;
private String prodDatum;
private Integer stueck;
public String getPackStNr() {
return packStNr;
}
public void setPackStNr(String packStNr) {
this.packStNr = packStNr;
}
public String getProdDatum() {
return prodDatum;
}
public void setProdDatum(String prodDatum) {
this.prodDatum = prodDatum;
}
public Integer getStueck() {
return stueck;
}
public void setStueck(Integer stueck) {
this.stueck = stueck;
}
}
My Controller
private void generateLagerbestand(List<Artikeldaten> artikelList) throws JRException, FileNotFoundException {
JasperReport jasperReport;
Map<String, Object> parameters = new HashMap<String, Object>();
List <OuterTable> outerTableList = new ArrayList<OuterTable>();
for(int i = 0; i < artikelList.size(); i++) {
List <InnerTable> innerTableList = new ArrayList<InnerTable>();
List<Lager> lagersList = artikelList.get(i).getLagers();
for(int j = 0; j < lagersList.size(); j++) {
InnerTable innerTable = new InnerTable();
innerTable.setPackStNr(lagersList.get(j).getPackstnr());
innerTable.setProdDatum(lagersList.get(j).getProddatum().toString());
innerTable.setStueck(lagersList.get(j).getStueck());
innerTableList.add(innerTable);
}
OuterTable outerTable = new OuterTable();
outerTable.setAkundenNr(artikelList.get(i).getAkundennr());
outerTable.setSachNr(artikelList.get(i).getSachnr());
JRBeanCollectionDataSource nestedTable = new JRBeanCollectionDataSource(innerTableList);
outerTable.setNestedTable(nestedTable);
outerTableList.add(OuterTable);
}
try {
jasperReport = JasperCompileManager.compileReport("PathToMyReport\\MyReport.jrxml");
JRBeanCollectionDataSource outerTableItems = new JRBeanCollectionDataSource(outerTableList);
// artikeldatenTable is outerTable
parameters.put("artikeldatenTable", outerTableItems);
JREmptyDataSource jrEmptyDataSource = new JREmptyDataSource();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrEmptyDataSource);
OutputStream outputStream = new FileOutputStream(new File("PathToMyPDF:\\MyPDF.pdf"));
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
} catch(Exception ex) {
ex.printStackTrace();
}
}
You are missing the concept of the detail band, the detail will iterate on the datasource you pass to report
JasperFillManager.fillReport(jasperReport, parameters, jrEmptyDataSource)
That's an empty datasource!, it will not iterate at all.
Instead, pass
JRBeanCollectionDataSource outerTableItems = new JRBeanCollectionDataSource(outerTableList);
To the report, define the fields of OuterTable in your main report (not a subDatasource) including nestedTable
<field name="nestedTable" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
Then
In detail band you put textFields related to first level "Out-1" and "Out-2" in your example.
Bellow you add the jr:table component taking the datasource from the bean
<datasetRun subDataset="New Dataset 1" uuid="17a10558-3a43-47c2-809d-6362924e5015">
<dataSourceExpression><![CDATA[$F{nestedTable}]]></dataSourceExpression>
</datasetRun>
In general within the bean I would not have JRBeanCollectionDatasource but instead store the data in a normal List<>, the reason is that a JRDatasource is consumable you can only use it once, see this to understand better How to use same JRBeanCollectionDataSource on multiple sub reports?
im having a hard time figuring out how to pass data from a custom JR Datasource to a chart, I got the data passed to the report however whenver I try to pass it to a chart it doesnt show anything.
The Values im trying to pass are the mean, the days, and the SD to create a levey jenning report
heres the code below for the datasource class:
package MyDataAdapt;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
public class GoImplementation implements JRDataSource {
public GoImplementation() {
// TODO Auto-generated constructor stub
}
/**
* For this data adapter the informations are embedded in the code
*/
private static final Integer[] lotNum = {33301141,33301143 };
private static final Integer[] level= {1,3};
private static final double[] mean= {4.49,457.04};
private static final double[] SD= {0.30,22.58};
private static final double[] CV= {6.79,4.94};
private static final Integer[] N= {54,53};
private HashMap<String, String> capitalCities = new HashMap<String, String>();
private static final double[] mean2= {3.92,465.95};
private static final double[] SD2= {0.35,31.11};
private static final double[] CV2= {9.04,6.68};
private static final Integer[] N2= {333,340};
private static final String[] department = {"GENERAL"," "};
private static final String[] instruementArray = {"ARCHITECT cI4100-I1SR"," "};
private static final String[] test = {"Human Chronic Gonadotropin (BhCG) {7K78}(mIU/mL"," "};
private static final String[] method = {"NONE"," "};
private static final String[] reagentArray = {"ABBOTT"," "};
private static final String[] month = {"January, 2015"," "};
private static final String[] shift = {"All"," "};
private static final String[] qcProduct = {"MULTICHEM IA PLUS(05P76-10)"," "};
private static final Integer[] days = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
HashMap<String, Integer> lotp = new HashMap<String, Integer>();
HashMap<String, Double> meanp = new HashMap<String, Double>();
HashMap<String, Double> sdp= new HashMap<String, Double>();
HashMap<String,Integer> dayp = new HashMap<String, Integer>();
/**
* Variable to store how much records were read
*/
private int counter = -1;
/**
* Variables to store the number of fields, and their names, in the report
*/
private HashMap<String, Integer> fieldsNumber = new HashMap<String, Integer>();
/**
* Method used to know if there are records to read.
*/
private int lastFieldsAdded = 0;
#Override
public boolean next() throws JRException {
if (counter<lotNum.length-1) {
counter++;
return true;
}
return false;
}
/**
* This method is called for every field defined in the report. So if i have 2 fields it is called 2 times for every record, and
* for each of them it must provide a value.
* The parameter can be used to understand for which field is requested, in fact it contains the name of the requested field. This
* data adapter is done with the goal of return two values, a name and an age. An easy option would be expect a field with the name
* "Name" and one with name "Age". But we can do something more flexible, in this case we will enumerate all the fields requested and
* and the first two will be assumed to be for name and age, for all the others will be returned an empty string. So we can have a report
* with more than two fields, but the name and the age will be returned each time only for the first two.
*
* If this example is too much complex refer to the method getFieldValue2, where is shown the first explained, and simple solution, where we
* expect two fields with a precise name.
*/
#Override
public Object getFieldValue(JRField jrField) throws JRException {
Integer fieldIndex;
if (fieldsNumber.containsKey(jrField.getName())) {
fieldIndex = fieldsNumber.get(jrField.getName());
}
else {
fieldsNumber.put(jrField.getName(), lastFieldsAdded);
fieldIndex = lastFieldsAdded;
lastFieldsAdded ++;
}
if (fieldIndex == 0) return lotNum[counter];
else if (fieldIndex == 1) return level[counter];
else if (fieldIndex == 2) return mean[counter];
else if (fieldIndex == 3) return SD[counter];
else if (fieldIndex == 4) return CV[counter];
else if (fieldIndex == 5) return N[counter];
else if (fieldIndex == 6) return mean2[counter];
else if (fieldIndex == 7) return SD2[counter];
else if (fieldIndex == 8) return CV2[counter];
else if (fieldIndex == 9) return N2[counter];
else if (fieldIndex == 10) return department[0];
else if (fieldIndex == 11) return instruementArray[0];
else if (fieldIndex == 12) return test[0];
else if (fieldIndex == 13) return method[0];
else if (fieldIndex == 14) return reagentArray[0];
else if (fieldIndex == 15) return month[0];
else if (fieldIndex == 16) return shift[0];
else if (fieldIndex == 17) return qcProduct[0];
else if (fieldIndex == 18) return days;
return "";
}
/**
* Return an instance of the class that implements the custom data adapter.
*/
public static JRDataSource getDataSource(){
return new GoImplementation();
}
}
The Jrxml File
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3 -->
<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="GraphReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b0db03f9-1d9d-4eb5-8f2e-bc1174d4b926">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="CustomDataPackage\DataAdapter.xml"/>
<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>
<subDataset name="LeveyJen" uuid="7a90870e-2a65-494c-9a60-0c66448362de">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="CustomDataPackage\DataAdapter.xml"/>
<parameter name="Mean" class="java.lang.Double">
<property name="com.jaspersoft.studio.js.ic.ds" value="com.jaspersoft.studio.data.defaultdataadapter"/>
</parameter>
<parameter name="SD" class="java.lang.Double"/>
<parameter name="Days" class="java.lang.Integer"/>
<queryString language="SQL">
<![CDATA[]]>
</queryString>
<field name="days" class="java.lang.Integer">
<fieldDescription><![CDATA[The dAYS Used]]></fieldDescription>
</field>
<field name="Mean" class="java.lang.Double">
<fieldDescription><![CDATA[The Mean]]></fieldDescription>
</field>
<field name="SD" class="java.lang.Double">
<fieldDescription><![CDATA[The Standard Deviation]]></fieldDescription>
</field>
<field name="Lot#" class="java.lang.Integer">
<fieldDescription><![CDATA[The lot number]]></fieldDescription>
</field>
<field name="CV" class="java.lang.Double">
<fieldDescription><![CDATA[The Coeficent Variation]]></fieldDescription>
</field>
<group name="Mean">
<groupExpression><![CDATA[$F{Mean}]]></groupExpression>
</group>
</subDataset>
<parameter name="Mean" class="java.lang.Double" evaluationTime="Early">
<defaultValueExpression><![CDATA[4.49]]></defaultValueExpression>
</parameter>
<parameter name="SD" class="java.lang.Double" evaluationTime="Early">
<defaultValueExpression><![CDATA[.30]]></defaultValueExpression>
</parameter>
<parameter name="days" class="java.lang.Integer" evaluationTime="Early">
<defaultValueExpression><![CDATA[1]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="Lot#" class="java.lang.Integer">
<fieldDescription><![CDATA[The lot number]]></fieldDescription>
</field>
<field name="Level" class="java.lang.Integer">
<fieldDescription><![CDATA[The level]]></fieldDescription>
</field>
<field name="Mean" class="java.lang.Double">
<fieldDescription><![CDATA[The Mean]]></fieldDescription>
</field>
<field name="SD" class="java.lang.Double">
<fieldDescription><![CDATA[The Standard Deviation]]></fieldDescription>
</field>
<field name="CV" class="java.lang.Double">
<fieldDescription><![CDATA[The Coeficent Variation]]></fieldDescription>
</field>
<field name="N" class="java.lang.Integer">
<fieldDescription><![CDATA[The Sample]]></fieldDescription>
</field>
<field name="cMean" class="java.lang.Double">
<fieldDescription><![CDATA[The Cummulative Mean]]></fieldDescription>
</field>
<field name="cSD" class="java.lang.Double">
<fieldDescription><![CDATA[The Cummulative Standard Deviation]]></fieldDescription>
</field>
<field name="cCV" class="java.lang.Double">
<fieldDescription><![CDATA[The Cummulative Variation]]></fieldDescription>
</field>
<field name="cN" class="java.lang.Integer">
<fieldDescription><![CDATA[The Cummulative Sample]]></fieldDescription>
</field>
<field name="department" class="java.lang.String">
<fieldDescription><![CDATA[The Department]]></fieldDescription>
</field>
<field name="instrument" class="java.lang.String">
<fieldDescription><![CDATA[The Instrument]]></fieldDescription>
</field>
<field name="test" class="java.lang.String">
<fieldDescription><![CDATA[The Test]]></fieldDescription>
</field>
<field name="method" class="java.lang.String">
<fieldDescription><![CDATA[The Method]]></fieldDescription>
</field>
<field name="reagent" class="java.lang.String">
<fieldDescription><![CDATA[The Reagent]]></fieldDescription>
</field>
<field name="month" class="java.lang.String">
<fieldDescription><![CDATA[The Month]]></fieldDescription>
</field>
<field name="shift" class="java.lang.String">
<fieldDescription><![CDATA[The Shift]]></fieldDescription>
</field>
<field name="qcProduct" class="java.lang.String">
<fieldDescription><![CDATA[The Product Used]]></fieldDescription>
</field>
<field name="days" class="java.lang.Integer">
<fieldDescription><![CDATA[The dAYS Used]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<summary>
<band height="626">
<componentElement>
<reportElement x="20" y="0" width="200" height="200" uuid="80fb1a9e-e915-4f0a-8841-5ec4dc3e3e62">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
</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" whenNoDataType="AllSectionsNoDetail">
<datasetRun subDataset="LeveyJen" uuid="b73b13da-6c82-476a-b05b-a76ceb8b0ace">
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
</datasetRun>
<jr:column width="40" uuid="870bbd63-dd2a-4a61-8488-ae55835df694">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:groupHeader groupName="Mean">
<jr:cell style="Table_CH" height="30"/>
</jr:groupHeader>
<jr:groupFooter groupName="Mean">
<jr:cell style="Table_CH" height="30"/>
</jr:groupFooter>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="40" height="30" uuid="dc2219e0-b6bb-4957-bf7e-ebad553c307d"/>
<text><![CDATA[days]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="40" height="30" uuid="c508b8f7-a940-4c21-8704-65df0d812521"/>
<textFieldExpression><![CDATA[$F{days}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="40" uuid="b7ae7382-8aa7-466f-96f8-ec5549e7ea7f">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:groupHeader groupName="Mean">
<jr:cell style="Table_CH" height="30"/>
</jr:groupHeader>
<jr:groupFooter groupName="Mean">
<jr:cell style="Table_CH" height="30"/>
</jr:groupFooter>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="40" height="30" uuid="4c916eef-3956-41c7-81b5-86ed59d8f816"/>
<text><![CDATA[Mean]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="40" height="30" uuid="3fba2b84-0d0a-4be9-8e39-2e883dc20962"/>
<textFieldExpression><![CDATA[$F{Mean}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="40" uuid="f50bd22f-deee-4ff7-b001-7e5515386adf">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:groupHeader groupName="Mean">
<jr:cell style="Table_CH" height="30"/>
</jr:groupHeader>
<jr:groupFooter groupName="Mean">
<jr:cell style="Table_CH" height="30"/>
</jr:groupFooter>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="40" height="30" uuid="960b3408-1558-4c43-9f90-366aef4bf4fa"/>
<text><![CDATA[SD]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="40" height="30" uuid="ce6ed33b-3b8a-4be7-96e9-4d6032342ba2"/>
<textFieldExpression><![CDATA[$F{SD}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="40" uuid="6ccc302d-bfbf-49c2-b1a9-1574cace4c20">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:groupHeader groupName="Mean">
<jr:cell style="Table_CH" height="30"/>
</jr:groupHeader>
<jr:groupFooter groupName="Mean">
<jr:cell style="Table_CH" height="30"/>
</jr:groupFooter>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="40" height="30" uuid="80e5d6b0-074c-4c5a-9a71-32d7e5ecbef9"/>
<text><![CDATA[Lot#]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="40" height="30" uuid="0d711de1-e894-4c2f-8247-f95a9ae14b14"/>
<textFieldExpression><![CDATA[$F{Lot#}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="40" uuid="361c4990-a417-4dbe-8fad-354daf2da30c">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:groupHeader groupName="Mean">
<jr:cell style="Table_CH" height="30"/>
</jr:groupHeader>
<jr:groupFooter groupName="Mean">
<jr:cell style="Table_CH" height="30"/>
</jr:groupFooter>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="40" height="30" uuid="cb377e1c-7426-45d5-b063-c70ba13d3189"/>
<text><![CDATA[CV]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="40" height="30" uuid="a5b3191c-e675-4018-8727-6afec6544feb"/>
<textFieldExpression><![CDATA[$F{CV}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
<lineChart>
<chart evaluationTime="Report">
<reportElement x="0" y="426" width="555" height="200" uuid="e6cccf0d-fe31-4417-ab27-f8a13aa142ad"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<categoryDataset>
<dataset>
<datasetRun subDataset="LeveyJen" uuid="49b8c004-a74d-4b9f-a772-9f0996c2c7fc">
<datasetParameter name="Mean">
<datasetParameterExpression><![CDATA[$P{Mean}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="SD">
<datasetParameterExpression><![CDATA[$P{SD}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="Days">
<datasetParameterExpression><![CDATA[$P{days}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
</dataset>
<categorySeries>
<seriesExpression><![CDATA[$F{Mean}]]></seriesExpression>
<categoryExpression><![CDATA[$F{days}]]></categoryExpression>
<valueExpression><![CDATA[$F{SD}]]></valueExpression>
</categorySeries>
<categorySeries>
<seriesExpression><![CDATA[$F{days}]]></seriesExpression>
<categoryExpression><![CDATA[$F{days}]]></categoryExpression>
<valueExpression><![CDATA[$F{SD}]]></valueExpression>
</categorySeries>
</categoryDataset>
<linePlot>
<plot/>
<categoryAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</valueAxisFormat>
</linePlot>
</lineChart>
</band>
</summary>
</jasperReport>
After being able to add custom data source from java bean to a report according to Add custom data source to Jaspersoft Studio , I get to the second point of my reporting with jasper.
I have a main report which uses a data base as its data source. Then I add a bean.xml data source to the report and add a table to the main report which uses this bean.xml data source to get java beans.
My goal is to get a field value from the main report and manipulate
its value, then fill the beans with these values and at last fill the
table with the beans.
To do this I have written 3 classes which I use as Scriptlet in the table data set:
This is an illustration of what I need to do:
The problem is in FillTable class, when I us String kNFormelGG = (String) this.getParameterValue("gg"); the created bean.xml fails the test connection with java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException
at net.sf.jasperreports.engine.JRAbstractScriptlet.getParameterValue(JRAbstractScriptlet.java:95)
at net.sf.jasperreports.engine.JRAbstractScriptlet.getParameterValue(JRAbstractScriptlet.java:86)
at org.iqtig.reporting.dataSource.bean.dataSourceXML.FillTable.fillTable(FillTable.java:45)
at org.iqtig.reporting.dataSource.bean.dataSourceXML.JRDataSourceFactory.createCollection(JRDataSourceFactory.java:27)
... 34 more
If I assign a fix value like String kNFormelGG ="Test me" the bean connection encounters no error and after assigning bean.xml as the value for Default Data Adapter in Dataset1 it fills the table with static values.
How can get the data from a parameter or a value dynamically from the main report data source and use it in beans?
I have this assumption that at the time of calling the static factory class from my adapter, the fields are still empty. Maybe I am wrong but I do not find any other declaration for this problem.
BeanFactory Class
import java.util.Collection;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
/**
* Factory for TableCellsBean Klasse
*
* #author iman.gharib
*/
public class JRDataSourceFactory extends JRDefaultScriptlet {
/**
* #return collection der TableCellsBean Objekten
* #throws JRScriptletException
*/
public static Collection<TableCellsBean> createCollection()
throws JRScriptletException {
FillTable ft = new FillTable();
Collection<TableCellsBean> reportRows = ft.fillTable();
return reportRows;
}
}
Bean Class
public class TableCellsBean {
private String fieldName;
private String keyFormel;
private String mK;
private String notation;
private String item;
/**
* Constructor.
*
* #param fieldName
* #param keyFormel
* #param mK
* #param notation
* #param item
*/
public TableCellsBean(final String fieldName, final String keyFormel, final String mK, final String notation, final String item) {
this.fieldName = fieldName;
this.keyFormel = keyFormel;
this.mK = mK;
this.notation = notation;
this.item = item;
}
/**
* Constructo Leer
*/
public TableCellsBean() {
}
public TableCellsBean getme() {
return this;
}
// getter and setters
}
Class for preparing and creating beans
public class FillTable extends JRDefaultScriptlet {
#Override
public void afterColumnInit()
throws JRScriptletException {
fillTable();
}
public ArrayList<String> splitGGArray(final String kNFormelGG) {
ArrayList<String> fieldNames = new ArrayList<>();
String[] array = (kNFormelGG.split(" "));
for (String sub : array) {
fieldNames.add(sub);
}
return fieldNames;
}
public Collection<TableCellsBean> fillTable()
throws JRScriptletException {
// gg is a parameter for table dataset. It is mapped to KN_FormelGG
// which comes from the main report data base
String kNFormelGG = (String) this.getParameterValue("gg");
List<TableCellsBean> listTableCells = new ArrayList<>();
// TableCellsBean tableCell = new TableCellsBean();
for (String fn : splitGGArray(kNFormelGG)) {
listTableCells.add(new TableCellsBean(fn, fn, fn, fn, fn));
// listTableCells.add(tableCell);
}
// JRBeanCollectionDataSource tableCellJRBean = new JRBeanCollectionDataSource(listTableCells);
// Map<String, Object> parameters = new HashMap<>();
// parameters.put("FieldDataSource", tableCellJRBean);
return listTableCells;
}
}
JRXML
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.0.final using JasperReports Library version 6.3.0 -->
<!-- 2016-08-08T14:30: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="main" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4f1480cf-f8f9-420f-96b4-7fc1e41e791b">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="QIDBReport"/>
<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>
<subDataset name="Dataset1" uuid="5677929d-813b-4d39-828c-de966a9d7689">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="bean.xml"/>
<property name="net.sf.jasperreports.data.adapter" value="bean.xml"/>
<scriptlet name="Scriptlet_1" class="org.iqtig.reporting.dataSource.bean.mapBeanAsDatasource.JRDataSourceFactory"/>
<parameter name="gg" class="java.lang.String"/>
<field name="item" class="java.lang.String">
<fieldDescription><![CDATA[item]]></fieldDescription>
</field>
<field name="fieldName" class="java.lang.String">
<fieldDescription><![CDATA[fieldName]]></fieldDescription>
</field>
<field name="me" class="org.iqtig.reporting.dataSource.bean.dataSourceXML.TableCellsBean">
<fieldDescription><![CDATA[me]]></fieldDescription>
</field>
<field name="keyFormel" class="java.lang.String">
<fieldDescription><![CDATA[keyFormel]]></fieldDescription>
</field>
<field name="mK" class="java.lang.String">
<fieldDescription><![CDATA[mK]]></fieldDescription>
</field>
</subDataset>
<parameter name="LB_ID" class="java.lang.Integer">
<defaultValueExpression><![CDATA[62]]></defaultValueExpression>
</parameter>
<parameter name="KN_OffeziellGruppe" class="java.lang.Integer">
<defaultValueExpression><![CDATA[3]]></defaultValueExpression>
</parameter>
<parameter name="FieldDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource" isForPrompting="false"/>
<parameter name="KN_FormelGG" class="java.lang.String" isForPrompting="false"/>
<queryString>
<![CDATA[select * from "KennzahlReferenz2015_QIBericht", "Images"
where LB_ID = $P{LB_ID}
and KN_OffiziellGruppe = $P{KN_OffeziellGruppe}
and IMG_ID = 1]]>
</queryString>
<field name="QI_Praefix" class="java.lang.String"/>
<field name="KN_Id" class="java.lang.Integer"/>
<field name="bewertungsArtTypNameKurz" class="java.lang.String"/>
<field name="refbereich" class="java.lang.String"/>
<field name="refbereichVorjahres" class="java.lang.String"/>
<field name="KN_GGAlleinstehend" class="java.lang.String"/>
<field name="erlaueterungDerRechregeln" class="java.lang.String"/>
<field name="teildatensatzbezug" class="java.lang.String"/>
<field name="mindesanzahlZaeler" class="java.lang.Integer"/>
<field name="mindesanzahlNenner" class="java.lang.Integer"/>
<field name="KN_FormelZ" class="java.lang.String"/>
<field name="KN_FormelGG" class="java.lang.String"/>
<field name="verwendeteFunktionen" class="java.lang.String"/>
<field name="idLb" class="java.lang.String"/>
<field name="LB_LangBezeichnung" class="java.lang.String"/>
<field name="LB_ID" class="java.lang.Integer"/>
<field name="nameAlleinstehend" class="java.lang.String"/>
<field name="KN_BezeichnungAlleinstehendKurz" class="java.lang.String"/>
<field name="QI_ID" class="java.lang.Integer"/>
<field name="IMG_ID" class="java.lang.Integer"/>
<field name="Name" class="java.lang.String"/>
<field name="Image" class="java.lang.Object"/>
<group name="id" isStartNewPage="true">
<groupExpression><![CDATA[$F{KN_Id}]]></groupExpression>
<groupHeader>
<band height="44"/>
</groupHeader>
<groupFooter>
<band height="50"/>
</groupFooter>
</group>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="44" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="34" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="149" splitType="Stretch">
<componentElement>
<reportElement x="170" y="20" width="350" height="100" uuid="38d917fb-dfc2-4c08-890a-09cfe6e2214d">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
<property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
<property name="com.jaspersoft.studio.components.autoresize.proportional" value="true"/>
</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" whenNoDataType="AllSectionsNoDetail">
<datasetRun subDataset="Dataset1" uuid="1b3548f6-7d6b-4070-bb8e-aaefbabdc7c9">
<datasetParameter name="gg">
<datasetParameterExpression><![CDATA[$F{KN_FormelGG}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
<jr:column width="70" uuid="048812d7-0ed1-4db8-a09a-e6242f77c6ce">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="c5aaea84-1367-41df-be8d-7f71e3ea5153"/>
<textFieldExpression><![CDATA[$F{item}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="70" uuid="11b85ada-c9fe-42b6-a646-8bd1697cdec2">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
<jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="728ff44d-1dbd-404c-b8b3-7cc0e1f07f60"/>
<textFieldExpression><![CDATA[$F{fieldName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="70" uuid="892f30cb-fb41-444f-889b-1e005484c35e">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
<jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="e38ac951-71bc-45a6-8ed2-313805a77050"/>
<textFieldExpression><![CDATA[$F{keyFormel}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="70" uuid="7d0d700a-5a75-4c26-94c0-9ef7c53bd719">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column4"/>
<jr:tableHeader style="Table_TH" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="68577007-0344-406c-8aa2-3127d1da1c65"/>
</textField>
</jr:tableHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="873d63c1-1b91-4441-b7bd-f67db7729e7f"/>
<textFieldExpression><![CDATA[$F{mK}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="70" uuid="cf5a1a2f-594d-429f-8925-62d001e1dd00">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column5"/>
<jr:tableHeader style="Table_TH" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="7fb46eb8-d0e1-44ab-89f9-ec31d49b8109"/>
<textFieldExpression><![CDATA[$P{gg}]]></textFieldExpression>
</textField>
</jr:tableHeader>
<jr:detailCell style="Table_TD" height="30"/>
</jr:column>
</jr:table>
</componentElement>
<textField>
<reportElement x="20" y="80" width="100" height="30" uuid="b89cd04c-2569-43ef-9730-445b874855dd"/>
<textFieldExpression><![CDATA[$F{KN_FormelGG}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="20" y="32" width="100" height="30" uuid="e91b4461-5e53-4f85-8992-14e69a1aa05f"/>
<text><![CDATA[KN_FormelGG]]></text>
</staticText>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
My goal is to get a field value from the main report and manipulate
its value, then fill the beans with these values and at last fill the
table with the beans.
I will ignore all your scriplets and other code and show you the simplest way to create a dynamic datasource for another component using a current field, variable or parameter value.
As example we will have these String values in field1 (row1, row2)
Test1_1.23:Test2_4.32:Test3_1.08
Test1_2.12:Test2_5.12:Test3_2.13
We want to split on : and create rows (List of beans), then split on _ to create columns (properties on bean) and display this in a jr:table component.
java code (or Bean)
public class TableCellsBean {
private String name;
private double value;
public TableCellsBean(String name,double value){
this.name = name;
this.value = value;
}
public static JRBeanCollectionDataSource getDatasource(String fieldValue){
List<TableCellsBean> retList = new ArrayList<>();
String[] values = fieldValue.split(":");
for (String v : values) {
String[] sp = v.split("_");
retList.add(new TableCellsBean(sp[0], Double.parseDouble(sp[1])));
}
return new JRBeanCollectionDataSource(retList);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
}
As you can see already inside this bean class I have added a static method that returns a JRBeanCollectionDataSource depending on value passed as parameter.
Basically you pass a fieldValue to this method, it does some logic, creates the TableCellBean, adds them to list and return a JRBeanCollectionDataSource
Note: The method does not need to be static nor inside this class, furthermore exception handling is not considered.
Report - the jrxml
In example we will have field1 containing Test1_1.23:Test2_4.32:Test3_1.08 on row 1 and Test1_2.12:Test2_5.12:Test3_2.13 on row 2.
We define a subdataset that represents out TableCellsBean
<subDataset name="table_dataset" uuid="4dc1b0fb-2588-4f98-8c8d-f0afefbb2fd1">
<field name="name" class="java.lang.String"/>
<field name="value" class="java.lang.Double"/>
</subDataset>
as datasource we call our static method passing the field1 content in current row.
<dataSourceExpression><![CDATA[my.package.TableCellsBean.getDatasource($F{field1})]]></dataSourceExpression>
Full jrxml
<?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="test" language="java" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="886a547e-11bd-434b-a330-d93ee5e4a280">
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="table_dataset" uuid="4dc1b0fb-2588-4f98-8c8d-f0afefbb2fd1">
<field name="name" class="java.lang.String"/>
<field name="value" class="java.lang.Double"/>
</subDataset>
<field name="field1" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
<detail>
<band height="40" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="555" height="20" uuid="a73343b1-ccb2-4a59-b882-381b98efd664"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{field1}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement key="table" style="table" x="0" y="20" width="555" height="20" uuid="09a9a5b8-499b-40d2-b391-ece25772a31e"/>
<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="table_dataset" uuid="05601bdb-5579-4253-90f7-6742739d9714">
<dataSourceExpression><![CDATA[bounty.TableCellsBean.getDatasource($F{field1})]]></dataSourceExpression>
</datasetRun>
<jr:column width="90" uuid="afbbb3d0-573a-495d-ab51-0ae4d601e6fb">
<jr:tableHeader style="table_TH" height="20" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="90" height="20" uuid="e476f026-bbec-4b19-9bd4-f4b21f3377ef"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Name]]></text>
</staticText>
</jr:tableHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="49cac181-b16b-4ab3-b600-78a56fb0f42b"/>
<box leftPadding="3" rightPadding="3"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="4a1b0759-c347-4294-82c5-3aed4762f0c4">
<jr:tableHeader style="table_TH" height="20" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="90" height="20" uuid="bfa965b5-b5a4-484d-bfbd-d8bc753718b1"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Value]]></text>
</staticText>
</jr:tableHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="925192bc-a761-48f5-bad5-097d15587198"/>
<box leftPadding="3" rightPadding="3"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
Test the example
Adding main method to export to pdf
public static void main(String[] args) throws JRException {
//Compile report
JasperReport report = JasperCompileManager.compileReport("myJasperReport.jrxml");
//Setting up some arbitrary data to test the auto creation of datasource
List<String> someData = new ArrayList<>();
someData.add("Test1_1.23:Test2_4.32:Test3_1.08");
someData.add("Test1_2.12:Test2_5.12:Test3_2.13");
//Fill report
JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap<>(),new JRBeanCollectionDataSource(someData));
//Export to pdf
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("myJasperReport.pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
}
Output
Conclusion
The simplest way to provide components with custom datasource depending on running reports values (fields, variables, parameter), is to create a method that generates the datasource and call this in dataSourceExpression
My JavaBean contains the java.util.List.
Userinfo {
private String username;
private String password;
List<Address> listAddress;
}
How to show the data of this List in the Detail band?
Here is the working sample.
The key points of this sample:
using of the _THIS expression;
using List (jr:list) component in Detail band
The snippet of code for generating report:
public static void testBuildPdf() {
try {
Map<String, Object> params = new HashMap<String, Object>();
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
} catch (Exception e) {
e.printStackTrace();
}
}
private static JRDataSource getDataSource() {
Collection<BeanWithList> coll = new ArrayList<BeanWithList>();
coll.add(new BeanWithList(Arrays.asList("London", "Paris"), 1));
coll.add(new BeanWithList(Arrays.asList("London", "Madrid", "Moscow"), 2));
coll.add(new BeanWithList(Arrays.asList("Rome"), 3));
return new JRBeanCollectionDataSource(coll);
}
The JavaBean code:
public class BeanWithList {
// The member's name can be any. The JR engine is using public getter for extracting field's value
private List<String> cities;
private Integer id;
public BeanWithList(List<String> cities, Integer id) {
this.cities = cities;
this.id = id;
}
// getter should be public
public List<String> getCities() {
return this.cities;
}
public Integer getId() {
return this.id;
}
}
The jrxml file:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
<subDataset name="dataset1">
<field name="city" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
</subDataset>
<field name="id" class="java.lang.Integer"/>
<field name="cities" class="java.util.Collection"/>
<title>
<band height="103" splitType="Stretch">
<staticText>
<reportElement x="138" y="28" width="258" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Bean with List sample]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[City name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement x="100" y="0" width="400" height="20"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="dataset1">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="20" width="400">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>
The result will be:
Other related questions are the How do I print a list of strings contained within another list in iReport? question and Passing the List of primitive type objects as datasource for subreport question.
public void generisiIzvestaj(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<Predstava> predstave = (List<Predstava>)request.getSession().getAttribute("predstaveR");
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(predstave);
InputStream inputStream = this.getClass().getResourceAsStream("/reports/Predstave.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
Map<String, Object> params = new HashMap<String, Object>();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
inputStream.close();
response.setContentType("application/x-download");
response.addHeader("Content-disposition", "attachment; filename=PredstaveRezisera.pdf");
ServletOutputStream out = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint,out);