reports jasperreports jasperreport jasper example español java jasper-reports

java - example - jasperreports server



JRBeanCollectionDataSource: ¿Cómo mostrar datos de java.util.List desde JavaBean? (1)

Posible duplicado:
¿Cómo imprimo una lista de cadenas contenidas dentro de otra lista en iReport?

Mi JavaBean contiene java.util.List .

Userinfo { private String username; private String password; List<Address> listAddress; }

¿Cómo mostrar los datos de esta lista en la banda de detalles?


Su pregunta duplica el ¿Cómo imprimo una lista de cadenas contenidas dentro de otra lista en iReport? pregunta y relacionado con Pasar la lista de objetos de tipo primitivo como fuente de datos para la pregunta de subinforme .

Aquí está la muestra de trabajo.

Los puntos clave de esta muestra:

  • uso de la expresión _THIS ;
  • usando el componente List (jr: list) en la banda Detail

El fragmento de código para generar el informe:

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(); System.out.println(e.getMessage()); } } private static JRDataSource getDataSource() { Collection<BeanWithList> coll = new ArrayList<BeanWithList>(); BeanWithList bean = new BeanWithList(Arrays.asList("London", "Paris"), 1); coll.add(bean); bean = new BeanWithList(Arrays.asList("London", "Madrid", "Moscow"), 2); coll.add(bean); bean = new BeanWithList(Arrays.asList("Rome"), 3); coll.add(bean); return new JRBeanCollectionDataSource(coll); }

El código de JavaBean:

public class BeanWithList { private List<String> m_cities; private Integer m_id; public BeanWithList(List<String> cities, Integer id) { m_cities = cities; m_id = id; } public List<String> getCities() { return m_cities; } public Integer getId() { return m_id; } }

El archivo jrxml:

<?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>

El resultado será: