我正在尝试在Jasper中生成表格报告。具体来说,我试图遍历像这样的表中的列表:

Java代码:

List<String> severity = (List<String>)session.getAttribute("severity");
List<String> messages = (List<String>)session.getAttribute("messages");

LinkedHashMap parameters = new LinkedHashMap();
parameters.put("severity", severity);
parameters.put("messages", messages);

jPrint = JasperFillManager.fillReport(jasperreport,parameters,new JREmptyDataSource());
JasperExportManager.exportReportToPdfStream(jPrint,baos);

JRXML :

<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="jasper_report_template" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a3d5ea75-61fe-4bd0-a2ef-43652ca82bb5">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="severity" class="java.util.List">
        <fieldDescription><![CDATA[severity]]></fieldDescription>
    </field>
    <field name="messages" class="java.util.List">
        <fieldDescription><![CDATA[messages]]></fieldDescription>
    </field>
    <columnHeader>
        <band height="23" splitType="Stretch">
            <staticText>
                <reportElement mode="Opaque" x="0" y="-180" width="535" height="15" backcolor="#70A9A9" uuid="9b5b79e0-e146-41d9-b665-6cf9441c44f7"/>
                <box>
                    <bottomPen lineWidth="1.0" lineColor="#CCCCCC"/>
                </box>
                <text><![CDATA[ ]]></text>
            </staticText>
            <staticText>
                <reportElement x="290" y="0" width="121" height="15" uuid="427a0dc3-acc5-445d-afdf-325f56220d5b"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Error Severity]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="3" width="136" height="15" uuid="9d05c15b-478c-49d3-a334-c5d6bfc62e53"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Erorr Messages]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="16" splitType="Stretch">
            <staticText>
                <reportElement mode="Opaque" x="0" y="0" width="535" height="14" backcolor="#E5ECF9" uuid="b0ac6c6e-6d86-44de-801c-fcb85f1c5cb7"/>
                <box>
                    <bottomPen lineWidth="0.25" lineColor="#CCCCCC"/>
                </box>
                <text><![CDATA[ ]]></text>
            </staticText>
            <textField>
                <reportElement x="290" y="0" width="121" height="15" uuid="d106ecf9-2dc8-49fb-b63c-e348b96f871d"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{severity}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="0" width="136" height="15" uuid="d106ecf9-2dc8-49fb-b63c-e348b96f871d"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{messages}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

所以我的问题是:

1)如何在Jrxml中打印这些列表?

2)是正确的方法来遍历JRXML中的列表吗?

3)如何编写JRXML代码以迭代List? (子报表在这种情况下有用吗?)

最佳答案

传递两个LinkedHashMap并不是您需要实现的最佳数据结构。

如果您按照以下方式更改数据结构,则jrxml仅需进行少量更改即可使用。

创建一个包含您的错误的bean。

public class ErrorBean {
    private String severity;
    private String messages;
    public String getSeverity() {
        return severity;
    }
    public void setSeverity(String severity) {
        this.severity = severity;
    }
    public String getMessages() {
        return messages;
    }
    public void setMessages(String messages) {
        this.messages = messages;
    }
}

用数据填充您的bean
//Pass our two LinkList to our new ErrorBean
List<ErrorBean> errorBeans = new ArrayList<ErrorBean>();
for (int i = 0; i < severity.size(); i++) {
    ErrorBean eb = new ErrorBean();
    eb.setSeverity(severity.get(i));
    if (messages.size()>i){
        eb.setMessages(messages.get(i));
    }
    errorBeans.add(eb);
}

通过JRBeanCollectionDataSourceerrorBeans传递给JasperReport
Map<String,Object> parameters = new HashMap<String,Object>();
jPrint = JasperFillManager.fillReport(jasperreport,parameters,new JRBeanCollectionDataSource(errorBeans));

现在,您对jrxml进行的唯一小的修改就是定义字段的正确类(从java.util.List切换到java.lang.String)
<field name="severity" class="java.lang.String">
    <fieldDescription><![CDATA[severity]]></fieldDescription>
</field>
<field name="messages" class="java.lang.String">
    <fieldDescription><![CDATA[messages]]></fieldDescription>
</field>

关于java - 如何使用列表在JasperReports中生成动态表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34736221/

10-10 15:13