报告中的空值

时间:2015-05-26 10:57:53

标签: java jasper-reports

我正在尝试使用Jasper Reports生成PDF报告,当我预览报告时,但是当我从html页面输入id null时,下面的图片显示了结果:

enter image description here

这里是我的代码:
的index.html

 <form action="Print">
   <input type="text"  name="id" >
   <input type="submit"  value="print">
 </form>

Print.java

 response.setContentType("application/pdf");
 int id=Integer.parseInt(request.getParameter("id"));

 // set input and output stream
 ServletOutputStream servletOutputStream = response.getOutputStream();  
 ByteArrayOutputStream baos = new ByteArrayOutputStream();

 FileInputStream fis;
 BufferedInputStream bufferedInputStream;

 try {          
     // get report location
     ServletContext context = getServletContext();
     String reportLocation = context.getRealPath("");

     // get report
     fis = new FileInputStream(reportLocation + "/newReport.jasper");
     bufferedInputStream = new BufferedInputStream(fis);
     Map map=new HashMap();
     map.put("parameter1", id);
     // export to pdf           
     JasperReport jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream);
     JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
       map, new JREmptyDataSource());
     JasperExportManager.exportReportToPdfStream(jasperPrint, baos);
     response.setContentLength(baos.size());
     baos.writeTo(servletOutputStream);

     // close it
     fis.close();
     bufferedInputStream.close();
 } catch (Exception e) {
     e.printStackTrace();
 } finally{
     servletOutputStream.flush();
     servletOutputStream.close();
     baos.close();
 }        

1 个答案:

答案 0 :(得分:0)

检查你的地图

map.put("parameter1", id);

已正确定义!

您的密钥名为“parameter1”可能有问题。