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

Posted

技术标签:

【中文标题】JRBeanCollectionDataSource:如何从 JavaBean 显示来自 java.util.List 的数据?【英文标题】:JRBeanCollectionDataSource: How to show data from the java.util.List from JavaBean? 【发布时间】:2012-08-25 21:57:18 【问题描述】:

我的 JavaBean 包含 java.util.List

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

如何在Detail波段显示这个List的数据?

【问题讨论】:

你能帮我解决这个问题吗? ***.com/questions/55262894/… 【参考方案1】:

这是工作示例。

本示例的要点:

使用_THIS表达式; 在 Detail 波段中使用 List (jr:list) 组件

生成报告的代码sn-p:

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" 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  splitType="Stretch">
            <staticText>
                <reportElement x="138" y="28"  />
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Bean with List sample]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band >
            <staticText>
                <reportElement x="0" y="0"  />
                <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"  />
                <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  splitType="Stretch">
            <textField>
                <reportElement stretchType="RelativeToTallestObject" x="0" y="0"  />
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$Fid]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement x="100" y="0"  />
                <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($Fcities)]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents  >
                        <textField>
                            <reportElement x="0" y="0"  />
                            <box leftPadding="10">
                                <topPen lineWidth="1.0"/>
                                <leftPen lineWidth="1.0"/>
                                <bottomPen lineWidth="1.0"/>
                                <rightPen lineWidth="1.0"/>
                            </box>
                            <textElement/>
                            <textFieldExpression><![CDATA[$Fcity]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </detail>
</jasperReport>

结果将是:


其他相关问题是How do I print a list of strings contained within another list in iReport? 问题和Passing the List of primitive type objects as datasource for subreport 问题。

【讨论】:

您有城市列表,但在 中您选择“城市”字段。它来自哪里?请解释一下。 @GirishK 查看 jrxml 文件中的字段声明 您使用“城市”作为集合的字段名称,尽管 BeanWithList 中的列表的实际名称是“m_cities”,并且在“子数据集”元素中也没有“城市”的引用。它实际上是如何工作的? @MichaelAs-s-raf 公共吸气剂是public List&lt;String&gt; getCities()。这意味着您可以使用cities名称 我在上次编辑时将其切换为此语法,以使其更清晰,如果您不喜欢它,请随时回滚。【参考方案2】:
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);

【讨论】:

以上是关于JRBeanCollectionDataSource:如何从 JavaBean 显示来自 java.util.List 的数据?的主要内容,如果未能解决你的问题,请参考以下文章