我的JasperReport的具体用法

我的系统:jsp+strtus+ejb+dao+db
我的思路:在Action(ro DispatchAction)组装好报表所需的参数,然后传递给统一的显示servlet

我的实现
1.显示servlet
2.参数class
3.Action

1.显示servlet(此处为html)
public class JRHtmlServlet extends HttpServlet {
  /**
   *
   */
   public void service(
          HttpServletRequest request,
          HttpServletResponse response
          ) throws IOException, ServletException{

    //得到所需参数
    JRParameterInterface jreportParam = null;
    jreportParam = (JRParameterInterface)request.getAttribute("jreportParam");

    response.setContentType("text/html;charset=GB2312");
    PrintWriter out = response.getWriter();

    try
    {

            JasperReport jasperReport = (JasperReport)JRLoader.loadObject(jreportParam.getJasperFile().getPath());

            JasperPrint jasperPrint =
                    JasperFillManager.fillReport(
                            jasperReport,
                            jreportParam.getParameters(),
                            jreportParam.getDataSet()
                            );

            JRHtmlExporter exporter = new JRHtmlExporter();

            Map imagesMap = new HashMap();
            request.getSession().setAttribute("IMAGES_MAP", imagesMap);

            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
            exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
            exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "JRReport/image?image=");

            exporter.exportReport();
    }
    catch (JRException e)
    {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>JasperReports - Web Application Sample</title>");
//            out.println("<link rel=/"stylesheet/" type=/"text/css/" href=/"../stylesheet.css/" title=/"Style/">");
            out.println("</head>");

            out.println("<body bgcolor=/"white/">");

            out.println("<span class=/"bnew/">JasperReports encountered this error :</span>");
            out.println("<pre>");

            e.printStackTrace(out);

            out.println("</pre>");

            out.println("</body>");
            out.println("</html>");
    }
}

}
2.参数class

public interface JRParameterInterface {
//set所需参数
  void setParameters(Object object, Object object1);
//返回所需file
  File  getJasperFile();
//返回页面的parameter
  Map getParameters();
//返回页面的数据,如果为空,返回 (new JREmptyDataSource()),否则返回 (new WebappDataSource(data))
  JRDataSource getDataSet();
}

实现类
public class JReportParam implements JRParameterInterface {


  private DataSet jrData;
  private File jasperFile;
  private JRDataSource jrDataSource;
  private Map parameterMap;

  public JReportParam(File jasperFile,DataSet data) {
    this.jasperFile = jasperFile;
    jrData = data;
    parameterMap = new HashMap();
  }

  public JReportParam(File jasperFile) {
    this(jasperFile,(DataSet)null);
  }

  public JReportParam(File jasperFile,JRDataSource JRdata) {
    this.jasperFile = jasperFile;
    jrDataSource = JRdata;
    parameterMap = new HashMap();
  }

  //set所需参数
  public void setParameters(Object object, Object object1){
    parameterMap.put(object, object1);
  };

  //返回所需file
  public File  getJasperFile(){
    return jasperFile;
  };

  //返回页面的parameter
  public Map  getParameters(){
    return parameterMap;
  };
//返回页面的数据,如果为空,返回 (new JREmptyDataSource()),否则返回 (new WebappDataSource(data))
  public JRDataSource getDataSet(){
    if (jrDataSource == null){
      if ((jrData==null) || (jrData.getRetCode().equals("NO_DATA"))){
        jrDataSource = new JREmptyDataSource();
      }else{
        jrDataSource = new WebappDataSource(jrData);
      }
    }
    return jrDataSource;
  }


}
3.Action
//需要得到绝对路径path
       File reportFile = new File(path +"demo.jasper");

       JReportParam jreportParam = new JReportParam(reportFile, dataset);
       jreportParam.setParameters("ReportTitle", "my demo");
       httpServletRequest.setAttribute("jreportParam", jreportParam);

       actionForward = actionmapping.findForward(reportType);

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 2

打赏作者

avidmouse

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值