2013-05-21 68 views
0

我正在開發一個struts2應用程序,我想在瀏覽器上顯示我生成的報告。我已經成功地在磁盤上生成了我的報告。但是現在我想在瀏覽器中看到它。這裏有一些代碼。通過瀏覽器上的jaspereports顯示生成的報告

我的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(); 
      } 
     }  
} 
+0

將報告導出到流並使用Struts2'stream'結果。 –

+0

@ AleksandrM,謝謝你的回覆,但是我不明白你的做法,有沒有鏈接,或者你可以給我解釋一下更多 – djoStack

+0

究竟是什麼?請參閱http://struts.apache.org/development/2.x/docs/stream-result.html。 –

回答

0

首先你彙報流出口,類似的東西:

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

然後輸出流轉換爲輸入流並將其分配到您的動作中,以獲得者/設置者爲inputStream變量。並配置動作以使用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>