struts2与jasperreport整合其余问题

转载自 walkingz
最终编辑 duzhixing1211

关于如题问题,今日做了最终汇总,并email值apache mail list:http://www.quicktopic.com/43/H/RNEjXQFBJHX

所有问题的内容如下:

Recently,I meet some problems during using "struts2-jasperreport-plugin-2.1.6", which includes:
A. undisplaying common report using the format of html and excel(pdf,good),because of the px image;
B. after resolving the problem A,there is annother problem which is undisplaying chart-report using the format of html(excel and pdf ,good),because of the img_0_0_0 image;
C. after revoling the problem B,there is also one problem which is laying over char-rpoert when one request needs to return at least two chart-reports;
D. single datasource for report;
Above of all the problems except for D have been resovled by myself,I don't know whether these problems belong to bugs,and I just email these problems to some help to others. Resovling method as followings (I apologize for untranslating my mother tongue to english for sharing):

ver 0:原始的struts2-jasperreport-plugin
解决问题:普通报表HTML、Excel格式浏览存在px图片无法显示;
问题原因:sturts2默认的后缀扩展时action,是在struts2-core-xxxxx.jar的org.apache.struts2下的default.properties中定义的,正常情况下是
                   struts.action.extension=action
                 而在struts2.1.6中,却是struts.action.extension=action,,
                 如此的配置使得struts2的拦截器除了拦截后缀为action的url及uri外,还额外拦截任何没有后缀的url及uri,那些不期待被拦截的咚咚也被
   拦截去找相应的action了,致使产生了此问题
解决方案:1、在struts.xml中加<constant name="struts.action.extension" value="action"/>
    2、在webroot根目录下建立一个images目录,放入px

ver 1:
解决问题:图形报表HTML无法显示;
解决方法:强制报表images到磁盘,即修改插件,添加代码,代码片段如下:
public class JasperReportsResult extends StrutsResultSupport implements JasperReportConstants {
     // 省略 ...
    protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
        // 省略...
                 } else if (format.equals(FORMAT_HTML)) {
                response.setContentType("text/html");

                // IMAGES_MAPS seems to be only supported as "backward compatible" from JasperReports 1.1.0

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

                exporter = new JRHtmlExporter();
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imageServletUrl);
                //<--begin added by twolf, 200900902
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, request.getRealPath(File.separator) + imageServletUrl);
                exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
                //end added by twolf -->

                // Needed to support chart images:
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint);
            } else if (format.equals(FORMAT_XLS)) {
               // 省略...
        // Will throw ServletException on IOException.
        writeReport(response, output);
    }
    // 省略 ...
}

ver 2:
解决问题:图形报表一次请求返回多张时存在报表覆盖异常现象
解决方法:散列请求报表存放位置,消除覆盖异常现象
public class JasperReportsResult extends StrutsResultSupport implements JasperReportConstants {
     // 省略 ...
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
        // 省略...
            } else if (format.equals(FORMAT_HTML)) {
                response.setContentType("text/html");

                // IMAGES_MAPS seems to be only supported as "backward compatible" from JasperReports 1.1.0

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

                exporter = new JRHtmlExporter();
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
                //<--begin added by twolf, 200900902
                //屏蔽JRHtmlExporterParameter.IMAGES_URI
                //exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imageServletUrl);
                String xDir = "img" + finalLocation.hashCode(); //构造报表存放子目录,以期解决一次请求返回多个图形报表重叠现象
                String imgServDirUrl = imageServletUrl + xDir + "/";
                File imgRealDir= new File(request.getRealPath(File.separator) + imgServDirUrl);
                if(!imgRealDir.exists()) {
                imgRealDir.mkdirs();
                }
                //重设JRHtmlExporterParameter.IMAGES_URI
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imgServDirUrl);
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR, imgRealDir);
                exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
                //end added by twolf -->
                // Needed to support chart images:
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint);
            } else if (format.equals(FORMAT_XLS)) {
          // 省略...
        // Will throw ServletException on IOException.
        writeReport(response, output);
    }
    // 省略 ...
}
=============以上版本要求在webroot目录下存在images目录,而以下则无需,因会自动创建==========
ver 3:
解决问题:同ver 2,做了改进,配置rptAlone参数决定一次请求返回是否多于一张图形报表,默认一次请求返回一张图形报表
解决方法:增加配置参数,修改JasperReportConstants.java及JasperReportsResult.java

在JasperReportConstants.java中增加
//<begin added by twolf 20090902
   //标识一次请求返回报表是否孤单,呵呵
   public static final String CHART_RPT_ALONE = "Y";   
   public static final String CHART_RPT_NONALONE = "N";
   // end added by twolf 20090902>

在JasperReportsResult.java修改:
添加:
  //<begin added by twolf,20090203
   protected String rptAlone;

public String getRptAlone() {
   return rptAlone;
}
public void setRptAlone(String rptAlone) {
   this.rptAlone = rptAlone;
}   
    //end added by twolf,20090203>
在initializeProperties方法中添加:
//<begin added by twolf,20090203
        rptAlone = conditionalParse(rptAlone, invocation);
        if (!TextUtils.stringSet(rptAlone)) {
        rptAlone = CHART_RPT_ALONE;
        }
        //end added by twolf,20090203>

在doExecute方法中,修改:
else if (format.equals(FORMAT_HTML)) {
                response.setContentType("text/html");

                // IMAGES_MAPS seems to be only supported as "backward compatible" from JasperReports 1.1.0

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

                exporter = new JRHtmlExporter();
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
                //<--begin added by twolf, 20090203
                //屏蔽JRHtmlExporterParameter.IMAGES_URI
                //exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imageServletUrl);
                String xDir;
                String imgServDirUrl;
                if(CHART_RPT_ALONE.equalsIgnoreCase(rptAlone)) {
                xDir = "img" + "_pub";
                } else {
                xDir = "img" + finalLocation.hashCode(); //构造报表存放子目录,以解决一次请求返回多个图形报表覆盖现象              
                }
                imgServDirUrl = imageServletUrl + xDir + "/";
                File imgRealDir = new File(request.getRealPath(File.separator) + imgServDirUrl);
                if(!imgRealDir.exists()) {
                imgRealDir.mkdirs();
                }
                //重设JRHtmlExporterParameter.IMAGES_URI
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imgServDirUrl);
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR, imgRealDir);
                exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
                //end added by twolf,20090203 -->
                // Needed to support chart images:
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint);
            } else if (format.equals(FORMAT_XLS)) {
ver 4:
解决问题:支持多数据源
解决方法:处理中

There is some problem in diplaying my mother tongue,so you could see http://redsnow-fenglin.javaeye.com/blog/461927 ,which is the paper on my blog.
...
I am amazed by the unexpected phenomenon,which it does not work in ver 3 when rptAlone is set JasperReportConstants.CHART_RPT_ALONE,and I doubt file stream's problem, so I change the code again, which as followinigs:
ver 3.1
解决问题:在ver 3中,当rptAlone为JasperReportConstants.CHART_RPT_ALONE时,报表图片路径正确,却不可显示,怀疑文件流关闭问题
解决方法:从新修改代码,如下:
} else if (format.equals(FORMAT_HTML)) {
                response.setContentType("text/html");

                // IMAGES_MAPS seems to be only supported as "backward compatible" from JasperReports 1.1.0

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

                exporter = new JRHtmlExporter();
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
                //<--begin added by twolf, 20090203
                //屏蔽JRHtmlExporterParameter.IMAGES_URI
                //exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imageServletUrl);
               if(CHART_RPT_ALONE.equalsIgnoreCase(rptAlone)) {
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imageServletUrl);
                    exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, request.getRealPath(File.separator) + imageServletUrl);
                    exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
                } else {
                String xDir = "img" + finalLocation.hashCode(); //构造报表存放子目录,以解决一次请求返回多个图形报表覆盖现象
                String imgServDirUrl = imageServletUrl + xDir + "/";
                    File imgRealDir = new File(request.getRealPath(File.separator) + imgServDirUrl);
                    if(!imgRealDir.exists()) {
                    imgRealDir.mkdirs();
                    }
                    //重设JRHtmlExporterParameter.IMAGES_URI
                    exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imgServDirUrl);
                    exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR, imgRealDir);
                    exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);             
                }
                //end added by twolf,20090203 -->
                // Needed to support chart images:
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint);
            } else if (format.equals(FORMAT_XLS)) {

It does work very well now.

源码下载地址:http://redsnow-fenglin.javaeye.com/blog/508715

同步blog http://hi.baidu.com/fenglinquan/blog/item/386103fa91604817a9d3112d.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值