将JasperReports与相对路径一起使用 [英] Using JasperReports with a relative path

查看:147
本文介绍了将JasperReports与相对路径一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络应用程序,客户端已要求查看一些报告。方法是使用iReport并在屏幕上显示报告。

I have a web app and the client has requested to see some reports. The approach has been to use iReport and show them the report on screen.

我已经问了这样的事情。但今天我发现报告文件(jrxml)的路径是绝对的。所以我必须改变程序,以便接受相对路径。我一直在尝试这样做,但似乎jrxml或编译(.jasper)文件都不接受既不编译也不填充报告的相对路径。

I have already asked something like this. But today I have discovered that the paths to the report files (jrxml) are absolute. So I have to change the program so it accepts relative paths. I have been trying to do this, but it seems that neither the jrxml or the compiled (.jasper) files accepts relative paths to neither compile or to fill the report.

这就是我迄今为止所做的:

This is what I have got this far:

//path is generated as request.getContextPath() + "/jrxmlFiles/"
public void generateReport(HttpServletResponse res, ConexionAdmin con, String path) throws Exception{ 

    ServletOutputStream out = null;
    ByteArrayOutputStream bos    = new ByteArrayOutputStream();

    JasperDesign jasperDesign = JRXmlLoader.load(path);
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

  byte[] bytes = JasperRunManager.runReportToPdf(jasperReport, pars, con.initConexion());
        res.setContentType("application/pdf");
  res.setContentLength(bytes.length);
  out = res.getOutputStream();
  out.write(bytes, 0, bytes.length);

  res.setHeader("Cache-Control", "cache");
  res.setHeader("Content-Disposition", "attachment; filename=report.pdf"); 
  res.setHeader("Pragma", "cache");
  res.setContentLength(bos.size());

  out.write(bos.toByteArray());
  out.flush();
  bos.close();
  out.close(); 
  res.flushBuffer();
}

这似乎适用于绝对路径,但却让我感激不尽:

This seems to work with absolute paths, but throws me:

Exception Message
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException

更改为相对路径时。我在网上搜索没有成功如何改变我的适合度。

when changed to a relative path. I have searched the net with no success in how to change to my fits.

我有jasper API的javaDoc,但如果我能帮助它,我宁愿不读它。

I have the javaDoc for the jasper API, but I rather not read it through if I can help it.

推荐答案


  • 路径必须是绝对路径。

  • 仅编译 .jrxml 文件到 .jasper 文件,如果 .jrxml 正在修改。通常,您只需加载 .jasper 文件并完全跳过编译。它要快得多。

  • 存储 .jasper .jrxml 以外的文件你的网络根目录。

  • 在你的所有报告中创建以下参数:

    • Paths must be absolute.
    • Only compile .jrxml files to .jasper files if the .jrxml is being modified. Usually you can just load the .jasper file and skip compilation altogether. It is much faster.
    • Store .jasper and .jrxml files outside of your web root.
    • Create the following parameters throughout all your reports:

      
         ROOT_DIR = "/full/path/to/reports/"
         IMAGE_DIR = $P{ROOT_DIR} + "images/"
         STYLES_DIR = $P{ROOT_DIR} + "styles/"
         SUBREPORT_DIR = $P{ROOT_DIR} + "subreports/"
         COMMON_DIR = $P{ROOT_DIR} + "common/"


    • 参考项目相对于 $ P {ROOT_DIR} (例如, $ P {IMAGE_DIR} 是根据 $ P {ROOT_DIR} 来定义的。

    • 从您的环境中传递 $ P {ROOT_DIR} 的值。

    • 松散地结合你的应用于您使用的任何报告框架。

    • Reference items relative to $P{ROOT_DIR} (e.g., $P{IMAGE_DIR} is defined in terms of $P{ROOT_DIR}).
    • Pass the value of $P{ROOT_DIR} in from your environment.
    • Loosely couple your application to any reporting framework you use.
    • 然后在必要时使用表达式。例如,引用子报告如下:

      Then use the expressions when necessary. For example, reference subreports as follows:

      <subreportExpression>
        <![CDATA[$P{SUBREPORT_DIR} + "subreport.jasper"]]>
      </subreportExpression>
      

      这将允许子报告目录在不同环境之间变化。

      This will allow the subreport directory to vary between environments.

      这篇关于将JasperReports与相对路径一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆