2014-10-27 53 views
0

我是新的JasperReports。所以,我已经成功创建了报告功能,我有3个参数,其中包含日期和2个字符串。到目前为止,当查询有结果(非空)时,情况会非常好。但是如果查询是空的,所创建的pdf将是纯白色的空白。我可以验证空数据源/空白pdf吗?

所以,我想要做的是,我要验证它,每当它要生成纯白色PDF,我不想显示空白PDF,而是我想显示一些错误消息。这可能吗?

这是我的代码,在JSP中使用JavaScript。

<% 
    Connection conn = null; 
    try{ 
     Class.forName("oracle.jdbc.OracleDriver"); 
     conn = DriverManager.getConnection("jdbc:oracle:thin:@333.0.0.1:3333:sample", "asd", "asd"); 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } 

    String method_param = request.getParameter("method"); 
    if(method_param.equals("default")){ 
     method_param = "%"; 
    } 

    String date_param = request.getParameter("date"); 
    if(date_param == ""){ 
     date_param = "%"; 
    } 

    String status_param = request.getParameter("status"); 
    if(status_param.equals("default")){ 
     status_param = "%"; 
    } 


    Map parameters = new HashMap(); 
    parameters.put("method_param", method_param); 
    parameters.put("date_param", date_param); 
    parameters.put("status_param", status_param); 

    JasperPrint jasperPrint = JasperFillManager.fillReport("C:/myaddress/lalala/WEB-INF/reports/Reports.jasper", parameters, conn); 
    OutputStream outputStream = response.getOutputStream(); 
    JRExporter exporter = new JRPdfExporter(); 
    response.setContentType("application/pdf"); 
    response.setHeader("Content-Disposition", "inline; filename=\"report.pdf\""); 
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); 
    exporter.exportReport(); 

     if (outputStream != null) 
     { 
      try 
      { 
      outputStream.close(); 
      } 
      catch (IOException ex){ 
      ex.printStackTrace(); 
      } 
     } 


%> 

它运行良好,但我只是想避免空白pdf。

回答

2

jasper report提供了一个名为REPORT_COUNT的预定义变量。它包含报告中存在的记录总数。

您可以使用此变量来显示适当的消息,例如'找不到记录'。

谢谢

+1

嗨,这个想法很好,但是当我尝试它时,它仍然显示我“文档没有页面”。我认为这是因为查询导致null?但为什么即使它是空的,它也不能显示PDF? – Nico 2014-10-28 01:48:50

+1

嗨,我已经可以显示空白PDF与头,否则,当我想实现使用打印时表达式,为什么它不工作在我的报告的细节部分? – Nico 2014-10-28 03:05:47

+1

嗨,将表达式设置为新布尔值($ V {REPORT_COUNT} .intValue()== 0)和evaluationTime =“Report” – amdg 2014-10-28 04:11:01