0

我正在开发一个 struts2 应用程序,我想在浏览器上显示我生成的报告。我已经成功地在磁盘上生成了我的报告。但现在我想在浏览器上看到它。这里有一些代码。

我的 index.jsp

<body>
<a href="HTML.action">Reporting</a>
</body>

我的动作 DataBeanList

public class DataBeanList extends ActionSupport {

      public ArrayList<DataBean> getDataBeanList() {
     ArrayList<DataBean> dataBeanList = new ArrayList<DataBean>();

      dataBeanList.add(produce("Manisha", "India"));
      dataBeanList.add(produce("Dennis Ritchie", "USA"));
      dataBeanList.add(produce("V.Anand", "India"));
      dataBeanList.add(produce("Shrinath", "California"));
      dataBeanList.add(produce("issam", "casa"));

      return dataBeanList;
   }

   /**
    * This method returns a DataBean object,
    * with name and country set in it.
    */
   private DataBean produce(String name, String country) {
      DataBean dataBean = new DataBean();
      dataBean.setName(name);
      dataBean.setCountry(country);
      return dataBean;
   }


public void exporte(){

String sourceFileName = "D://Test/workspace/ztest/WebContent/reports/jasper_report_template.jasper";
               // + "test/jasper_report_template.jasper";
        //D:\Test\workspace\ztest\WebContent\reports
          String printFileName = null;
          DataBeanList DataBeanList = new DataBeanList();
          ArrayList dataList = DataBeanList.getDataBeanList();
          JRBeanCollectionDataSource beanColDataSource =
             new JRBeanCollectionDataSource(dataList);

          Map parameters = new HashMap();
          try {
             printFileName = JasperFillManager.fillReportToFile(sourceFileName,
                parameters, beanColDataSource);
             if (printFileName != null) {
                /**
                 * 1- export to PDF
                 */
                JasperExportManager.exportReportToPdfFile(printFileName,
                      "D://sample_report.pdf");


             }
          } catch (JRException e) {
             e.printStackTrace();
          }
       }    
}
4

1 回答 1

0

首先将您报告到流中的导出,如下所示:

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
            parameters, beanDataSource);
JasperExportManager.exportReportToPdfStream(jasperPrint, out);

然后将输出流转换为输入流,并在您的操作中inputStream使用 getter/setter 将其分配给变量。并配置操作以使用stream结果。

<action ...>
  <result type="stream">
    <param name="inputName">inputStream</param>
    <param name="contentType">application/pdf</param>
    <param name="contentDisposition">attachment;filename="report.pdf"</param>
  </result>
</action>
于 2013-05-22T08:23:54.403 回答