尝试从资源获取文件时出现FileNotFound异常

时间:2012-05-01 10:50:18

标签: java spring jasper-reports

我无法生成myJasperReports.jasper文件,因为它在尝试读取文件并创建报告时抛出FileNotFoundException。我正在使用jasper-reports 3.7.5和基于注释的Spring。有人能说出问题可能是什么吗?

在web.xml文件中:

<bean id="myReportTemplate" class="org.springframework.core.io.ClassPathResource">
    <constructor-arg value="mypackage/reports/jasper/template/SampleJasper.jasper"/>
</bean>

感谢。

这是堆栈跟踪:

[ERROR] java.io.FileNotFoundException: class path resource [mypackage/reports/jasper/template/SampleJasper.jasper] cannot be resolved to URL because it does not exist
[ERROR] at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:179)
[ERROR] at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:48)
[ERROR] at mypackage/reports.ReportsServiceImpl.prepareChart(ReportsServiceImpl.java:93)
[ERROR]

我在pom.xml文件中使用的插件

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <executions>
      <execution>
        <phase>compile</phase>
          <goals>
            <goal>exploded</goal>
          </goals>
     </execution>
   </executions>
   <configuration>
     <webappDirectory>${webappDirectory}</webappDirectory>
   </configuration>
 </plugin>

 <plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>jasperreports-maven-plugin</artifactId>
   <configuration>
     <outputDirectory>${project.build.directory}/jasper</outputDirectory>
   </configuration>
   <executions>
     <execution>
       <goals>
         <goal>compile-reports</goal>
       </goals>
     </execution>
   </executions>
   <dependencies>
     <dependency>
       <groupId>jasperreports</groupId>
       <artifactId>jasperreports</artifactId>
       <version>2.0.5</version>
     </dependency>
   </dependencies> 
</plugin>

1 个答案:

答案 0 :(得分:1)

  

问题是SampleJasper.jasper文件没有得到   在指定文件夹中生成,文件夹本身不会创建。

要生成Jasper报告,您应致电JasperCompileManager.compileReport(),然后传递.jrxml文件。

但是在您的情况下,您指定了.jasper文件,这意味着您应该已经编译并将编译文件放在您在application-context.xml中提供的位置。

如果你没有预编译它,那么你应该指定jrxml文件进行编译,然后编译文件,你将把它传递给Jasper查看器。

Spring有jasper integration

的文档
相关问题