日期:2014-05-17  浏览次数:20761 次

终结解决方案之最: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 doExecut