【发布时间】:2019-01-16 02:17:28
【问题描述】:
我在尝试加载 .war 文件中的 .jasper subReport 时遇到问题。
这个想法是加载一个位于“parte1”文件夹内的子报表。
在documentation 中说您不能通过相对路径来加载子报表文件...
您不能使用相对路径来定位子报表文件;也就是说,如果您在 c:\myreport\main_report.jasper 中有报告,则不能使用 ..\mysubreports\mysubreport.jasper 之类的表达式来引用子报告。
这是因为 JasperReports 不会在内存中保存它正在使用的 Jasper 文件的原始位置。考虑到 Jasper 对象不一定是从物理文件中加载的,这很有意义。
所以我正在尝试将完整路径传递给 .jasper 文件。
1.- 我使用@Autowired 获取上下文,然后将路径作为字符串完成...
parameters.put("SUBREPORT_DIR", contextPath + "WEB-INF" + File.separator + "report" + File.separator + "parte1" + File.separator);
2.- 将路径作为参数传递给 jasperReport
printFileName = JasperFillManager.fillReportToFile(getPath(sourceFileName), parameters, new JRBeanCollectionDataSource(lstDataSource));
3.- 给出加载子报表文件的路径
<subreport>
<reportElement x="50" y="50" width="200" height="200" uuid="ced7076b-b6ba-4973-8e4c-cd289bd77c4e"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{motivoReclamo})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "ClsMotivoReclamoReportBean.jasper"]]></subreportExpression>
</subreport>
但是,当尝试运行它时。它显示此错误消息...
2019-01-15 05:29:19 [http-nio-8081-exec-2] 错误 xxx.xxx.xx.report.PDFGenerator - net.sf.jasperreports.engine.JRException:在以下位置找不到资源: C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps\xxxxxxxxx\WEB-INF\report\parte1\ClsMotivoReclamoReportBean.jasper。
提前致谢。
更新
我正在使用 Spring Boot 并生成一个 .war 文件
我正在使用这个 maven 插件来编译报告并生成 .jasper 文件。
https://github.com/alexnederlof/Jasper-report-maven-plugin
然后我使用这个 maven 插件来复制报告中需要的一些图像...
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/report</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/jasperfiles</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.jrxml</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
然后我使用这个 maven 插件将 jasper 文件复制到 .war 中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/generated-sources/report</directory>
<targetPath>WEB-INF/classes/report</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
【问题讨论】:
标签: java jasper-reports war