Java 如何将jasper文件路径传递给子报表的子报表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3702565/
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
how to pass jasper file path to subreport of subreport
提问by Manu
I am using iReport tool in conjunction with JasperReports 1.3.4.
我将 iReport 工具与 JasperReports 1.3.4 结合使用。
I have a master report, which contain two subreports. One of these subreports has an embedded subreport. All of the .jasper files reside in the same directory.
我有一个主报告,其中包含两个子报告。这些子报表之一具有嵌入式子报表。所有 .jasper 文件都位于同一目录中。
iReport generated the parameter called SUBREPORT_DIR
. The subreportExpression expresses the subreport filename as ![CDATA[$P{SUBREPORT_DIR} + "subreport.jasper"]
.
iReport 生成了名为 的参数SUBREPORT_DIR
。subreportExpression 将子报表文件名表示为![CDATA[$P{SUBREPORT_DIR} + "subreport.jasper"]
.
Everything works great when the report is generated from within iReport. But, I have a small Java web appplication that I am trying to use to generate reports. I pass a "SUBREPORT_DIR"
parameter to JasperFillManager.fillReport(String sourceFileName, Map parameters, JRBeanCollectionDatasource)
. This SUBREPORT_DIR
is set to the directory in which the master report is located.
从 iReport 中生成报告时,一切正常。但是,我有一个小型 Java Web 应用程序,我正试图用它来生成报告。我将"SUBREPORT_DIR"
参数传递给JasperFillManager.fillReport(String sourceFileName, Map parameters, JRBeanCollectionDatasource)
. 这SUBREPORT_DIR
被设置为主报告所在的目录。
fillReport throws an exception:
fillReport 抛出异常:
net.sf.jasperreports.engine.JRException: Could not load object from location :
.sub-subreport.jasper.
It appears that the first subreport is trying to process its subreport(embedded subreport)
, but the SUBREPORT_DIR
is not being resolved, or used properly to generate the name of the sub-subreport.
看起来第一个子报表正在尝试处理它的subreport(embedded subreport)
,但是SUBREPORT_DIR
没有被解析,或者没有正确使用来生成子子报表的名称。
Is there something I'm missing? How should this scenario be handled?
有什么我想念的吗?这种情况应该如何处理?
回答by Marcio Fonseca
Click in Subreport1
, go to the properties tab -> Subreport properties -> Parameters
点击进入Subreport1
,进入properties tab -> Subreport properties -> Parameters
You will pass your 'MasterReport' SUBREPORT_DIR
parameter as a parameter to 'Subreport1'.
Create this entry:
您将您的“ MasterReport”SUBREPORT_DIR
参数作为参数传递给“ Subreport1”。创建此条目:
Name: SUBREPORT_DIR
Expression: $P{SUBREPORT_DIR}
名称:SUBREPORT_DIR
表达式:$P{SUBREPORT_DIR}
Don't forget to recompile your reports and make sure to republish your web app in the application server.
不要忘记重新编译您的报告并确保在应用程序服务器中重新发布您的 Web 应用程序。
回答by Allan
I had the same issue and setting the SUBREPORT_DIR to the full package name containing my reports worked.
我遇到了同样的问题,并将 SUBREPORT_DIR 设置为包含我的报告的完整包名称。
For example:
例如:
"com/mycomp/myapp/reports/"
“com/mycomp/myapp/reports/”
Our set ups are similar in that you have all reports in the same directory. The difference is I am running my app from the desktop (Java SE). However, my reports were located in a jar file. When I tried to set teh SUBREPORT_DIR to c:\path\to\app\myapp.jar!\com\mycomp\myapp\reports Jasper failed to locate the file.
我们的设置类似,因为您将所有报告都放在同一目录中。不同之处在于我从桌面 (Java SE) 运行我的应用程序。但是,我的报告位于 jar 文件中。当我尝试将 SUBREPORT_DIR 设置为 c:\path\to\app\myapp.jar!\com\mycomp\myapp\reports 时,Jasper 未能找到该文件。
Hope this helps.
希望这可以帮助。
回答by Dag
As Marcio already explained, your SUBREPORT_DIR
parameter has to be passed from the calling report to the subreport.
Using relative path with .
or ./
is not reliable, an absolute path is always to prefer.
正如Marcio 已经解释的那样,您的SUBREPORT_DIR
参数必须从调用报告传递到子报告。使用相对路径.
或./
不可靠,绝对路径总是首选。
So you have to write in your jrxml file:
所以你必须在你的 jrxml 文件中写入:
<subreport isUsingCache="false">
<subreportParameter name="SUBREPORT_DIR">
<subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR}]]></subreportParameterExpression>
</subreportParameter>
<subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "mySubreport.jasper"]]></subreportExpression>
</subreport>
You have to pass the subreportParameterExpression
with the $P{SUBREPORT_DIR}
, elswise it won't pass the parameters (only setting <subreportParameter name="SUBREPORT_DIR"/>
is not enough).
你必须通过subreportParameterExpression
与$P{SUBREPORT_DIR}
,elswise也不会传递参数(仅设置<subreportParameter name="SUBREPORT_DIR"/>
是不够的)。
回答by Kusi Asiedu Daniel
just set $P{SUBREPORT_DIR}
as prompt and pass your value(the path to the directory may not be
valid like c:\something.jasper
instead of c:\\something.jasper
)
只需设置$P{SUBREPORT_DIR}
为提示并传递您的值(目录的路径可能无效,c:\something.jasper
而不是c:\\something.jasper
)