java 当路径指向 JAR 文件时,将 SUBREPORT_DIR 传递给子报告 [Jasper Reports 4.5]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10739547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing SUBREPORT_DIR to subreport when the path points inside a JAR file [Jasper Reports 4.5]
提问by Daniel Szalay
I'm having problems passing the SUBREPORT_DIR
path to my subreport. The reports are actually in the same folder, but inside a JAR file. I've tried something like this (might be inaccurate):
我在传递SUBREPORT_DIR
到我的子报表的路径时遇到问题。这些报告实际上位于同一个文件夹中,但位于 JAR 文件中。我试过这样的事情(可能不准确):
parameterList = new HashMap<String, Object>();
URL mainReport = this.getClass().getResource("mainReport.jasper");
String mainReportPath = mainReport.getPath();
String subreportDir = mainReportPath.substring(0, mainReportPath.lastIndexOf("/")+1);
parameterList.put("SUBREPORT_DIR", subreportDir);
This path points inside the JAR file, but I get net.sf.jasperreports.engine.JRException: Resource not found at : ...
upon report generation. How can I make this work?
此路径指向 JAR 文件内部,但我会net.sf.jasperreports.engine.JRException: Resource not found at : ...
生成报告。我怎样才能使这项工作?
EDIT:
编辑:
I tried to define my subreport without the SUBREPORT_DIR
, but no luck:
我试图在没有 的情况下定义我的子报表SUBREPORT_DIR
,但没有运气:
<subreport>
<reportElement x="0" y="32" width="475" height="17"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{current})]]></dataSourceExpression>
<subreportExpression><![CDATA["mysubreport.jrxml"]]></subreportExpression>
</subreport>
回答by Daniel Szalay
My main problem was, that I was using a custom JAR loader, which Jasper didn't know of. The good news is it is also possible to pass a subreport as java.net.URL
or java.io.InputStream
:
我的主要问题是,我使用的是 Jasper 不知道的自定义 JAR 加载器。好消息是也可以将子报表作为java.net.URL
或传递java.io.InputStream
:
Java code:
爪哇代码:
InputStream subInputStream = this.getClass().getResourceAsStream("sub.jasper");
parameterList.put("SUBREPORT_INPUT_STREAM", subInputStream);
JRXML:
JRXML:
<subreportExpression class="java.io.InputStream"><![CDATA[$P{SUBREPORT_INPUT_STREAM}]]></subreportExpression>
回答by mdahlman
It's probably easiest to remove SUBREPORT_DIR
completely. iReport adds it by default as a convenience to keep track of your subreports. But you don't need to keep it.
SUBREPORT_DIR
完全删除可能最容易。默认情况下,iReport 添加它以方便跟踪您的子报表。但你不需要保留它。
In your case the reports are in the same directory and in the .jar, so they'll be on the classpath. Change the reference in the .jrxml from $P{SUBREPORT_DIR} + "mysubreport.jasper"
to simply "mysubreport.jasper"
. Then delete the parameter from the report and get rid of your code that tries to figure out what directory to use.
在您的情况下,报告位于同一目录和 .jar 中,因此它们将位于类路径中。改变从.jrxml为基准$P{SUBREPORT_DIR} + "mysubreport.jasper"
简单"mysubreport.jasper"
。然后从报告中删除参数并删除试图找出要使用的目录的代码。
回答by Jacob Schoen
If I am not mistaken you should be able to reference the subreport just based on the location of the compiled jasper file inside the jar. For instance if the subreport is in a package named test.reports
for the SUBREPORT_DIR
value you should be able to pass in /test/reports/
.
如果我没记错的话,您应该能够仅根据 jar 中已编译的 jasper 文件的位置来引用子报告。例如,如果子报表位于以您应该能够传入test.reports
的SUBREPORT_DIR
值命名的包中/test/reports/
。
If that does not work, try moving the compiled reports into the default package and just set SUBREPORT_DIR
as /
instead.
如果这不起作用,请尝试将编译后的报告移动到默认包中并设置SUBREPORT_DIR
为/
。
回答by Gabriel Vieira
In the "KEY" property of the subreport object within the main file, try assigning the subreport name defined in the subreport file, so the subreport value will be returned to the main report.
在主文件中子报表对象的“KEY”属性中,尝试分配子报表文件中定义的子报表名称,这样子报表的值就会返回到主报表中。