在我的网络应用程序中,我使用 JasperReports 生成一些报告。在我的托管bean中,我有以下报告生成代码:
public void generateReport(ActionEvent e) { // Inside a Managed Bean
try {
pathReport = ReportHelper.reportPath(FacesContext.getCurrentInstance(), "rep_attendees.jasper");
Map<String, Object> param = new HashMap<String, Object>();
param.put("EVENT_ID", getEvent().getId());
param.put("START_DATE", null);
param.put("END_DATE", null);
try {
Properties connectionProps = new Properties();
connectionProps.put("user", "root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyDB", connectionProps);
pdfFile = ReportHelper.generateReport(pathRelatorio, parametros, con);
} catch (Exception ex) {
log.error(ex.getMessage());
}
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
ReportHelper的代码:
public class ReportHelper {
private static final Logger log = Logger.getLogger(RelatorioHelper.class);
public static byte[] generateReport(String path, Map<String, Object> param, Connection con) throws FileNotFoundException, JRException, Exception {
JasperReport rep = null; // 1
JasperPrint print = null; // 2
byte[] pdf = null; // 3
try {
rep = buildReport(path); // 4
print = loadReport(rep, param, con); // 5 NEVER RETURNS
pdf = JasperExportManager.exportReportToPdf(print); // 6 Don't reach this line
} catch (Exception e) {
log.error(e.getMessage());
throw e;
}
return pdf;
}
public static String reportPath(FacesContext context, String reportName) {
String repPath = ((ServletContext) context.getExternalContext().getContext()).getRealPath("/resources/reports/") + "/" + reportName;
return repPath;
}
private static JasperReport buildReport(String path) throws FileNotFoundException, JRException {
return (JasperReport) JRLoader.loadObject(new File(path));
}
private static JasperPrint loadReport(JasperReport report, Map<String, Object> param, Connection con) throws JRException {
return JasperFillManager.fillReport(report, param, con);
}
}
以下是报告xml:
<?xml version="1.0" encoding="UTF-8"?>
<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="SisEventos_RelatorioEvento" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="DATA_INICIO" class="java.util.Date" isForPrompting="false">
<defaultValueExpression><![CDATA[null]]></defaultValueExpression>
</parameter>
<parameter name="DATA_TERMINO" class="java.util.Date" isForPrompting="false">
<defaultValueExpression><![CDATA[null]]></defaultValueExpression>
</parameter>
<parameter name="ID_EVENTO" class="java.lang.Integer" isForPrompting="false">
<defaultValueExpression><![CDATA[1]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[select idt_evento, nme_evento, dta_inicio_evento, dta_termino_evento, nme_participante, eml_participante from tb_inscricao as t1
inner join tb_evento as t2 on t1.cod_evento = t2.idt_evento
inner join tb_participante as t3 on t1.cod_participante = t3.idt_participante
where (t1.cod_evento = $P{ID_EVENTO} and $P{DATA_INICIO} is not null and $P{DATA_TERMINO} is not null and t2.dta_inicio_evento between $P{DATA_INICIO} and $P{DATA_TERMINO}) or (t1.cod_evento = $P{ID_EVENTO} and $P{DATA_INICIO} is null and $P{DATA_TERMINO} is not null and t2.dta_termino_evento <= $P{DATA_TERMINO}) or (t1.cod_evento = $P{ID_EVENTO} and $P{DATA_INICIO} is not null and $P{DATA_TERMINO} is null and t2.dta_inicio_evento >= $P{DATA_INICIO}) or (t1.cod_evento = $P{ID_EVENTO} and $P{DATA_INICIO} is null and $P{DATA_TERMINO} is null)
group by t2.nme_evento order by t2.dta_inicio_evento asc;]]>
</queryString>
<field name="idt_evento" class="java.lang.Integer"/>
<field name="nme_evento" class="java.lang.String"/>
<field name="dta_inicio_evento" class="java.sql.Timestamp"/>
<field name="dta_termino_evento" class="java.sql.Timestamp"/>
<field name="nme_participante" class="java.lang.String"/>
<field name="eml_participante" class="java.lang.String"/>
<group name="gEventos">
<groupExpression><![CDATA[$F{nme_evento}]]></groupExpression>
<groupHeader>
<band height="42">
<rectangle>
<reportElement x="25" y="21" width="530" height="20" forecolor="#666666" backcolor="#666666"/>
</rectangle>
<staticText>
<reportElement x="35" y="21" width="267" height="20" forecolor="#FFFFFF"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<text><![CDATA[Participante]]></text>
</staticText>
<staticText>
<reportElement x="302" y="21" width="186" height="20" forecolor="#FFFFFF"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<text><![CDATA[E-mail]]></text>
</staticText>
<staticText>
<reportElement x="488" y="21" width="68" height="20" forecolor="#FFFFFF"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<text><![CDATA[Presente]]></text>
</staticText>
<textField isStretchWithOverflow="true">
<reportElement x="0" y="1" width="270" height="20"/>
<textElement>
<font size="14" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Evento: " + $F{nme_evento}]]></textFieldExpression>
</textField>
<line>
<reportElement x="270" y="12" width="280" height="1"/>
</line>
</band>
</groupHeader>
<groupFooter>
<band height="50"/>
</groupFooter>
</group>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="60" splitType="Stretch">
<rectangle>
<reportElement x="0" y="0" width="200" height="60" forecolor="#E6E6E6" backcolor="#E6E6E6"/>
</rectangle>
<staticText>
<reportElement x="0" y="0" width="200" height="60" forecolor="#666666" backcolor="#FFFFFF"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial" size="30"/>
</textElement>
<text><![CDATA[SisEventos]]></text>
</staticText>
<staticText>
<reportElement x="200" y="35" width="355" height="25"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font fontName="Arial" size="18" isBold="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Relatório de Participantes dos Eventos]]> </text>
</staticText>
<line>
<reportElement x="200" y="59" width="355" height="1"/>
</line>
</band>
</title>
<pageHeader>
<band height="30" splitType="Stretch">
<staticText>
<reportElement x="0" y="5" width="80" height="20"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[Filtro: ]]></text>
</staticText>
<textField isStretchWithOverflow="true">
<reportElement x="80" y="5" width="475" height="20"/>
<textElement/>
<textFieldExpression><![CDATA["Data de início: " + (($P{DATA_INICIO} != null) ? $P{DATA_INICIO}.toLocaleString().substring(0, 10) : "Não informada") + " - Data de término: " + (($P{DATA_TERMINO} != null) ? $P{DATA_TERMINO}.toLocaleString().substring(0, 10) : "Não informada")]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<columnHeader>
<band splitType="Stretch"/>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<rectangle>
<reportElement x="25" y="0" width="530" height="20" forecolor="#E6E6E6" backcolor="#E6E6E6">
<printWhenExpression><![CDATA[$V{COLUMN_COUNT}%2==0]]></printWhenExpression>
</reportElement>
</rectangle>
<textField isStretchWithOverflow="true">
<reportElement x="34" y="0" width="267" height="20"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{nme_participante}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="302" y="0" width="185" height="20"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{eml_participante}]]></textFieldExpression>
</textField>
<rectangle radius="10">
<reportElement x="500" y="2" width="15" height="15"/>
</rectangle>
</band>
</detail>
<columnFooter>
<band splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="355" y="0" width="200" height="20"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA["Página "+$V{PAGE_NUMBER}+" de "+ $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" pattern="EEE, dd MMM yyyy - HH:mm:ss" isBlankWhenNull="true">
<reportElement x="0" y="0" width="200" height="20"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA["Impresso em: " + new java.util.Date().toLocaleString()]]></textFieldExpression>
</textField>
<line>
<reportElement x="0" y="0" width="555" height="1"/>
</line>
</band>
</pageFooter>
<summary>
<band splitType="Stretch"/>
</summary>
</jasperReport>
当他到达第4行时,会发生一件奇怪的事情:名为 org.apache.cataline.startup.bootstrap 的工具在我的Dock中打开。但当他到达第5行并执行时,应用程序似乎停滞不前并且没有响应。好的另一件事是:我正在使用休眠。我如何从Hibernate获取Connection对象(通过JasperFillManager.fillReport(rep,param,con))?
答案 0 :(得分:0)
ramkitech.com发现了错误!在我的应用程序中,我使用Maven,并使用以下依赖项工件导入jasper报告库:
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>4.7.0</version>
</dependency>
但我还需要以下依赖项:
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
如果您在报告中使用groovy作为语言(如我的情况),那么您需要添加:
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.7.5</version>
</dependency>