`

终结解决方案之最:struts2整合jasperreport再现图片无法显示

阅读更多

关于如题问题,今日做了最终汇总,并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.iteye.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.iteye.com/blog/508715 

 

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

0
0
分享到:
评论
1 楼 lkang1981 2010-01-04  
..楼主鸡动了。。
     继续努力吧 嘿嘿  ~

相关推荐

    struts2整合jasperreport

    在本文中,我们将深入探讨如何整合Struts2框架与JasperReport3来创建动态报表,并利用iReport工具进行报表设计。Struts2是一个流行的Java Web应用程序框架,它提供了MVC(模型-视图-控制器)架构,而JasperReport则...

    整合Struts2.1+jasperreport

    Struts2.1和JasperReport的整合是Java Web开发中的一个重要话题,它涉及到Web应用程序的MVC(模型-视图-控制器)架构和数据报告的生成。Struts2.1是一个强大的MVC框架,而JasperReport则是一个用于创建复杂报表的...

    Struts2+JasperReport报表应用

    Struts2和JasperReport的整合是Web应用程序中创建动态报表的一种常见方法。JasperReport是一个功能强大的开源报表引擎,允许开发人员通过iReports这样的设计工具以可视化的形式创建复杂的报表模板。Struts2,作为一...

    Struts2整合JasperReport所需要的jar包

    - `struts2-core.jar`:Struts2框架的核心库,包含了Action、Result、Interceptor等核心概念的实现。 - `struts2-convention-plugin.jar`:用于自动配置Action类和结果页面的插件。 - `struts2-json-plugin.jar`...

    Struts2_JasperReport整合的例子

    Struts2和JasperReport是Java开发中两个重要的框架,它们在企业级应用开发中扮演着关键角色。Struts2是一个强大的MVC框架,用于构建动态、数据驱动的Web应用程序,而JasperReport则是一个开源的报表工具,可以生成...

    Struts2 + JasperReport应用一:导PDF,Excel,HTML显示

    在将Struts2与JasperReport整合时,首先需要在Struts2项目中添加JasperReport的依赖库,这通常包括jasperreports、jcommon、jfreechart等。接下来,我们需要创建一个Action类,该类将负责处理报表生成的请求,并调用...

    Struts2与Jasperreport报表结合

    2. 配置Struts2:在struts.xml中定义Action,指定处理报表请求的方法。 3. 实现Action:在Action中,加载数据源,执行SQL,将结果集转换为JRDataSource。 4. 填充报表:使用JasperFillManager填充报表并导出为所需的...

    struts2+Ireport+Jasperreport实现报表导出

    Struts2、iReport与JasperReport是Java开发中用于构建高效、动态Web应用程序和报表生成的工具。在这个项目中,这些技术结合在一起,提供了一种强大的报表导出解决方案。以下是对这些关键技术及其在项目中的应用的...

    ssh三大框架简单整合,struts2整合JasperReport报表,图表

    ssh三大框架简单整合,struts2整合JasperReport报表,解决HTML显示图片不出来,PDF中文不显示的问题 网上找答案,乱七八糟的!我研究了几天,终于彻底搞明白了!代码里有些注释,有凝问的可以留言。。。。。。 ...

    整合Struts2+JasperReport Web报表应用示例

    Struts2 和 JasperReport 的整合是为了解决在 Web 应用中生成复杂报表的需求。JasperReport 是一个强大的开源报表引擎,它允许开发者通过设计模板来生成各种类型的报表,如 PDF、HTML、Excel 等。而 Struts2 是一个...

    maven struts2 jasperreport ireport亲测通过整合多家资源

    在这个场景下,`Maven`、`Struts2`、`JasperReport` 和 `iReport` 是四个关键的技术组件,它们共同作用于创建一个强大的、数据驱动的Web应用程序。下面将详细介绍这些技术及其整合过程。 首先,`Maven` 是一个项目...

    struts2 jasperreport

    struts2 jasperreport

    struts2+jasperReport+ireport做报表总结

    当报表中引用了px图片但无法找到时,通常是因为Struts2框架的请求处理机制。解决方法是在`struts.xml`中添加 `&lt;constant name="struts.action.extension" value="action" /&gt;`,以确保Struts2能正确解析请求。此外,...

    Struts2与jasperReport简单示例

    4. **整合Struts2**:创建一个Struts2 Action,这个Action负责生成报表。在Action中调用上述步骤生成报表实例,然后将其设置为Action的一个属性,以便传递到视图。 5. **配置结果类型**:在struts.xml中,为这个...

    struts2-jasperreport-plugin-2.1.6.1

    官方发布的 struts2-jasperreport-plugin-2.1.6存在以下问题: 1、普通报表HTML、Excel格式浏览存在px图片无法显示; 2、解决问题:图形报表一次请求返回多张时存在报表覆盖异常现象; 3、支持数据源单一(改造后支持...

    Struts2集成jasperreport3.7.1所需的Jar包

    Struts2是一个流行的Java Web应用程序框架,用于构建和管理MVC(模型-视图-控制器)架构的应用。它提供了一种灵活且强大的方式来组织和控制应用的业务逻辑。而JasperReport则是一款强大的报告生成工具,允许开发者...

Global site tag (gtag.js) - Google Analytics