2012-09-15 236 views
0

在我的Web應用程序中,我生成了一些報告,其中包括JasperReports。在我管理的bean我有以下報表生成代碼:JasperPrint崩潰應用程序

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打開在我的 碼頭。但是當他到達第5行並執行時,應用程序似乎卡住了,並且沒有響應。好的另一件事是:我使用休眠。我怎麼能從Hibernate獲取Connection對象(通過JasperFillManager.fillReport(rep,param,con))傳遞給他?

回答

0

ramkitech.com發現錯誤!在我的應用程序正在使用Maven和下面的依賴構件正被用於導入碧玉報告庫:

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

如果你正在使用作爲一種語言(在我的情況下)常規,然後你需要添加:

<dependency> 
    <groupId>org.codehaus.groovy</groupId> 
    <artifactId>groovy-all</artifactId> 
    <version>1.7.5</version> 
</dependency>