Java JasperReports:如何在jsp页面中调用报表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3746649/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:17:05  来源:igfitidea点击:

JasperReports: How to call the report in jsp page

javajspjasper-reportsstruts-1

提问by krishna

I made one jasper report using iReport 3.7.4 version, now i have to use that or call that report in my java application where i am using servlets, jsp and struts framework, apache tomcat as server.

我使用 制作了一份 jasper 报告iReport 3.7.4 version,现在我必须使用该报告或在我使用 servlet、jsp 和 struts 框架、apache tomcat 作为服务器的 java 应用程序中调用该报告。

I want steps regarding how to call the jasper report with some example.

我想要有关如何通过一些示例调用 jasper 报告的步骤。

回答by Neuquino

This piece of code should give you some idea on how to do it

这段代码应该给你一些关于如何去做的想法

JasperReport jr=JasperCompileManager.compileReport("yourJRXMLFilePath");
JasperPrint jrPrint = JasperFillManager.fillReport(jr,mapWithParameters,aJRDataSource);
JasperExportManager.chooseYourFavoriteMethod(jrPrint,"destinationFile");

Otherwise, check the apiThe first line can be ommited if you had already compiled the file with iReport. Check the api for the correct method on JasperFillManager in this case.

否则,检查api如果您已经使用 iReport 编译了文件,则可以省略第一行。在这种情况下,在 JasperFillManager 上检查 api 的正确方法。

回答by Bozho

  1. Compile the report in iReport
  2. Place the compiled report on the classpath
  3. load it with

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
    
  4. Fill it with data. dataSourceis the DataSourceinstance you have - for example a BeanCollectionDataSource

    JasperPrint jasperPrint = 
         JasperFillManager.fillReport(jasperReport, params, dataSource);
    
  5. Export it

    JRPdfExporter exporter = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
    exporter.exportReport();
    
  6. The outputStreamabove may be either a response.getOutputStream()or a FileOutputStream(), depending on whether you want to send it to a client or you want to store it as a file. If you want to send it to the client, you'd have to send the Content-Dispositionheader, and some more, but that depends on the format you want to save to. In case you want to printon the client, it's quite a different question - you'd need some client-side code, an applet, for example.

  1. 在 iReport 中编译报告
  2. 将编译后的报告放在类路径上
  3. 加载它

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
    
  4. 用数据填充它。dataSourceDataSource您拥有的实例 - 例如BeanCollectionDataSource

    JasperPrint jasperPrint = 
         JasperFillManager.fillReport(jasperReport, params, dataSource);
    
  5. 导出

    JRPdfExporter exporter = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
    exporter.exportReport();
    
  6. outputStream上面可以是一个response.getOutputStream()或一个FileOutputStream(),取决于你是否想将它发送到客户端,或者您希望将其保存为一个文件。如果要将其发送给客户端,则必须发送Content-Disposition标头以及更多信息,但这取决于要保存的格式。如果您想在客户端上打印,这是一个完全不同的问题 - 例如,您需要一些客户端代码,一个小程序。

回答by fernando

in the first answer, point 5: After

在第一个答案中,第 5 点:之后

JRPdfExporter exporter= new JRPdfExporter();

JRPdfExporter exporter= new JRPdfExporter();

add

添加

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

回答by false9striker

This is a different way of doing the same.

这是一种不同的方式来做同样的事情。

    JasperReport jasperReport;
    JasperPrint jasperPrint;
    Map<String, Object> param = new HashMap<String, Object>();
    try{
        String sourceFileName = ".jrxml";
        jasperReport = JasperCompileManager.compileReport(sourceFileName);
        jasperPrint = JasperFillManager.fillReport(jasperReport,param,new JRBeanCollectionDataSource(getDetails()));
        JasperExportManager.exportReportToPdfFile(jasperPrint, ".pdf");
    }
    catch(Exception e){
    }

回答by false9striker

Best solution (For better performance as well), will be calling a compiled report.

最佳解决方案(为了更好的性能),将调用编译报告。

you can see the example below

你可以看到下面的例子

import java.io.IOException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class PdfFromJasperFile {
  public static void main(String[] args) throws JRException, IOException {

    JasperPrint jasperPrint = JasperFillManager.fillReport("report.jasper",  new HashMap<String, Object>(), 
    new JREmptyDataSource());
    JasperExportManager.exportReportToPdfFile(jasperPrint, "sample.pdf");

  }
}

回答by Petter Friberg

After 6 years @Bozho answernow (v5 and v6) contains deprecatedcode on point 5 JRExporterParameter.OUTPUT_STREAM, but I will try to improve the other points while I'm at it

6 年后@Bozho现在回答(v5 和 v6)包含第 5 点JRExporterParameter.OUTPUT_STREAM已弃用的代码 ,但我会尝试改进其他点

  1. Load the report

    compiledversion .jasper

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
    

    or the non compiledversion .jrxml(slower since need to compile but feasible)

    JasperReport jasperReport = JasperCompileManager.compileReport("path/to/myReport.jrxml");
    
  2. Fill the report

    with nothing(datasource generated inside report or just static text)

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
    

    with datasource:

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
    

    with database connection(may the most common, sql executed inside report)

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, connection);
    
  3. Export report

    JRPdfExporter exporter = new JRPdfExporter()
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
    SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
    configuration.setMetadataAuthor("Petter"); //Set your pdf configurations, 
    exporter.setConfiguration(configuration);
    exporter.exportReport();
    
  4. If you like to stream the reportdirectly to web page this is how contentTypeand Content-dispositionis set and how you retrieve the outputStream

    response.setContentType("application/x-pdf");
    response.setHeader("Content-disposition", "inline; filename=myReport.pdf");
    OutputStream outputStream = response.getOutputStream();
    
  1. 加载报表

    编译版本.jasper

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
    

    或在非编译版本.jrxml(慢,因为需要编译但可行)

    JasperReport jasperReport = JasperCompileManager.compileReport("path/to/myReport.jrxml");
    
  2. 填写报告

    什么(数据源产生的内部报告,或只是静态文本)

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
    

    数据源

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
    

    与数据库连接(可能是最常见的,在报表中执行的sql)

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, connection);
    
  3. 导出报告

    JRPdfExporter exporter = new JRPdfExporter()
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
    SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
    configuration.setMetadataAuthor("Petter"); //Set your pdf configurations, 
    exporter.setConfiguration(configuration);
    exporter.exportReport();
    
  4. 如果你喜欢流报告直接到网页这是怎么contentTypeContent-disposition你的设置,以及如何检索outputStream

    response.setContentType("application/x-pdf");
    response.setHeader("Content-disposition", "inline; filename=myReport.pdf");
    OutputStream outputStream = response.getOutputStream();