java - 我可以验证空数据源/空白 pdf 吗?

标签 java javascript jsp jasper-reports

我是 JasperReports 的新用户。。所以,我已经成功创建了我想要的报告函数,我有 3 个参数,其中包含日期和 2 个字符串。到目前为止,当查询有结果(不为空)时,一切进展顺利。但如果查询为空,创建的 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。

最佳答案

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

您可以利用此变量来显示适当的消息,例如“未找到记录”。

谢谢

关于java - 我可以验证空数据源/空白 pdf 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26584939/

相关文章:

eclipse - Tomcat 7.0 : Integrate with Eclipse

java - 我的程序如何获得系统待机/hibernate 模式的通知并重新启动?

javascript - 如何延迟页面渲染直到刷新 vue 变量?

javascript - html 在单击按钮时从 json 数据加载更多

javascript - 当用户在文本框中键入内容时,过滤由 java 代码添加的 HTML 选择元素

java - JSTL 在 ul - li html 标记中打印数组列表值。正确的算法。

java - jar 中的 MANIFEST.MF 有问题

java - Hibernate HQL 多对一连接

java - 按位置直接从 RecyclerView 访问 ViewHolder

java - 大文件的 DOM 与 SAX XML 解析