gpt4 book ai didi

java - Jasper子报告错误

转载 作者:行者123 更新时间:2023-12-01 15:22:58 28 4
gpt4 key购买 nike

com.hughes.exception.HughesException    at com.hughes.service.serviceImpl.HomeServiceImpl.sendTicketEmail(HomeServiceImpl.java:1094)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method).................................................................................................Caused by: net.sf.jasperreports.engine.JRException: Resource not found at : nullinvoiceDetail.jasper    at net.sf.jasperreports.repo.RepositoryUtil.getResource(RepositoryUtil.java:155)    at net.sf.jasperreports.repo.RepositoryUtil.getReport(RepositoryUtil.java:126)    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateReport(JRFillSubreport.java:317)    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:347)    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:275)    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:473)    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2021)    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:755)    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:265)    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:128)    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:836)    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:765)    at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:84)    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:624)    at com.hughes.service.serviceImpl.HomeServiceImpl.sendTicketEmail(HomeServiceImpl.java:1046)    ... 81 more
JasperReport jasperReport = JasperCompileManager.compileReport(hdnWebInfPath+seperator+"reports"+seperator+"invoice.jrxml");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, model, new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, fPath+seperator+fileName);

这在简单报告时有效不适用于子报告...

最佳答案

我的 Jarper 知识已经有几个版本了,但希望这会有所帮助。

这是因为 jasper 没有找到您的子报表。 Jasper 可以编译您的报表,但当您调用 compileReport 时,它不会编译引用的子报表。填写报表时,找不到引用的子报表,因为它们在工作目录中不可用。

(每次请求时都编译报告是一个坏主意(除非您有一些非常重要的理由这样做)。)

有几种方法可以解决这个问题。一是确保路径正确并在应用程序部署之前预编译报告。 Alex K 提供的链接是一个很好的来源。如果需要在应用程序中进行编译,则可以使用以下解决方案:

在网络应用程序中,我发现一种工作实践是处理编译,而填写报告则是手动管理它。这是我写的辅助类。希望它有用(它使用 Spring ,但这些部件很容易更换)。

public class ReportSource {

// Key = document name, Value = path to .jrxml
private Map<String, String> reports;
private final Map<String, JasperReport> compiled;

private final boolean lazy;

private final Logger log = Logger.getLogger(ReportSource.class);

public ReportSource(final boolean lazyBuild) {
super();
lazy = lazyBuild;
compiled = new HashMap<String, JasperReport>();
}

public void setCompileTargets(final Map<String, String> targets) {
reports = new HashMap<String, String>(targets);
if (!lazy) {
for (final String key : targets.keySet()) {
compile(key);
}
}
}

public JasperReport getReport(final String reportName) {
if (compiled.get(reportName) == null) { // not found or not compiled
log.info("Lazily compiling: " + reportName);
return compile(reportName);
}
return compiled.get(reportName);
}

private JasperReport compile(final String reportName) {
final String path = reports.get(reportName);

InputStream fis = null;
JasperReport report = null;
try {
final FileSystemResourceLoader resourceLoader = new FileSystemResourceLoader();
fis = resourceLoader.getResource(path).getInputStream();
log.info("Compiling report: " + reportName + " (" + path + ")");
report = JasperCompileManager.compileReport(fis);
} catch (final IOException ioe) {
throw new IllegalStateException("Configuration error file: " + path + " (for key: " + reportName +") not found.", ioe);
} catch (final JRException jre) {
throw new IllegalStateException("Configuration error file: " + path + " (for key: " + reportName +") not found.", jre);
}

compiled.put(reportName, report);
return report;
}
}

借助此类,您可以在文档中引用子报表,如下所示:

<subreport>
<reportElement x="0" y="0" width="200" height="30"/>
<subreportParameter name="data">
<subreportParameterExpression><![CDATA[$P{data}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="REPORT_RESOURCE_BUNDLE">
<subreportParameterExpression><![CDATA[$P{REPORT_RESOURCE_BUNDLE}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
<subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{data}.getSubreport("name_of_your_subreport")]]></subreportExpression>
</subreport>

这里 $P{data} 是作为文档参数提供的合适对象,其中 getSubreport() 最终调用 ReportSource.getReport() 。如果直接提供 ReportSource 作为参数,它当然可以是 $P{reportSource}.getReport("....") 。 (我们使用ReportModel方法;简而言之,它是适合报告上下文的表示模型)。

关于java - Jasper子报告错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10598958/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com