使用带有相对路径的 JasperReports [英] Using JasperReports with a relative path

查看:41
本文介绍了使用带有相对路径的 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 正在被修改,则仅将 .jrxml 文件编译为 .jasper 文件.通常你可以只加载 .jasper 文件并完全跳过编译.它要快得多.
  • .jasper.jrxml 文件存储在您的 Web 根目录之外.
  • 在您的所有报告中创建以下参数:

    • 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天全站免登陆