我正在开发一个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();
}
}
}
答案 0 :(得分:0)
首先导出你报告给流,类似的东西:
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
parameters, beanDataSource);
JasperExportManager.exportReportToPdfStream(jasperPrint, out);
然后将输出流转换为输入流,并使用getter / setter将其分配给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>