我正在尝试在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? (在这种情况下,子报告是否有用?)
答案 0 :(得分:4)
传递两个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);
}
中将errorBeans
传递给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>