栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java JRBeanCollectionDataSource:如何显示来自JavaBean的java.util.List中的数据?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java JRBeanCollectionDataSource:如何显示来自JavaBean的java.util.List中的数据?

这是工作示例。
该样本的关键点:

  • 使用的_THIS表达;
  • 使用列表(JR:列表) 在组分详细频带
    用于生成报告的代码段:
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);}

JavaBean代码:

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;    }}

jrxml文件:

<?xml version="1.0" encoding="UTF-8"?><jasperReport ...>    <subDataset name="dataset1">        <field name="city" > <fieldDescription><![CDATA[_THIS]]></fieldDescription>        </field>    </subDataset>    <field name="id" />    <field name="cities" />    <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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/431858.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号