终结解决方案之最: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
终结解决方案之最:struts2整合jasperreport再现图片无法显示相关推荐
- Spring和Struts2整合见问题之一
最近在做一个模拟网上银行小系统时遇到了问题,自己怎么也调不出来,然后各种百度,并在多个论坛上提问,问题终于在传智论坛上被于洋老师给指出来了,真是非常感谢! 下面说一下我遇到的问题: Struts2中A ...
- Text2SQL 语义解析数据集、解决方案、paper资源整合项目
https://github.com/yechens/NL2SQL Text2SQL 语义解析数据集.解决方案.paper资源整合项目
- struts2整合spring应用实例
我们知道struts1与spring整合是靠org.springframework.web.struts.DelegatingActionProxy来实现的,以下通过具体一个用户登录实现来说明stru ...
- Struts2 整合jQuery实现Ajax功能
为什么80%的码农都做不了架构师?>>> Struts2 整合jQuery实现Ajax功能 技术领域很多东西流行,自然有流行的道理,这几天用了jQuery,深感有些人真是聪明绝 ...
- spring与struts2整合出现错误HTTP Status 500 - Unable to instantiate Action
在进行spring和struts2整合的时候因为大意遇到了一个问题,费了半天神终于找到了问题所在,故分享出来望广大博友引以为戒!! 我们都知道在spring和struts2整合时,spring接管了a ...
- MyBatis数据持久化(十一)Mybatis3、Spring4、Struts2整合开发
上一节我们將Mybatis和Spring4进行整合,本节向大家介绍Mybatis在Web开发中的应用,并与主流的MVC框架Struts2进行整合. 我们首先需要获取Struts2框架,Struts2官 ...
- 使用struts2完成ckeditor和图片上传
代码地址如下: http://www.demodashi.com/demo/12427.html 使用struts2完成ckeditor和ckeditor图片上传 ckeditor版本ckeditor ...
- struts2生成随机验证码图片
之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...
- 关于有道云笔记md文档图片不显示的解决方案
关于有道云笔记md文档图片不显示的解决方案 1.存在问题 将Typora写好的文章(包含图片)在有道云文档中做备份,发现图片(图床上的图片)无法加载 2.思路与解决方案 将文章发表到CSDN或者掘金等 ...
- word中图片为嵌入式格式时显示不全_“word嵌入式图片不显示的解决办法”的解决方案...
解决方案 方案一: 1.使用word时,很多时候都会需要插入图片时显示不出来的问题,如图: 2.如果只有一张图片的话,可以设置图片版式,只要不是嵌入型即可,比如设置成浮于文字上方,即可显示,拖动一下图 ...
最新文章
- 将div垂直居中放置在另一个div中[重复]
- spring8: di依赖注入--构造注入
- 怎么判断网络回路_电源纹波要怎么测?
- linux常用关机命令及其区别-Shutdown halt reboot init
- linux 安装wdcp控制面板
- toj 4065 The Coco-Cola Store
- AI+社交,快手商业化落地之道
- 【华为云技术分享】关于Linux下Nginx的安装及配置
- pandas小记:pandas数据结构和基本操作
- 全网首发:怎样制作CDKEY(6)-CDKEY破解
- AXURE中SVG矢量图标的使用方法,以及图标颜色的改变方法
- rbf神经网络 c语言,RBF神经网络极简介绍及其算法R语言实现
- 汽车无人驾驶相应专业词汇
- 软件企业出口退税计算机题,出口退税计算题解析
- 基于微信小程序的毕业设计题目(36)PHP电影院售票小程序(含开题报告、任务书、中期报告、答辩PPT、论文模板)
- Reincarnation HDU - 4622 (后缀自动机)
- 苹果 WWDC21 发布会全汇总,iOS 15更个性化,全家桶协作更有生产力
- 程序猿和测试媛——组合在一起的原因
- 星星之火-52:6G十大领域关键技术
- 关于客户机服务器与微内核结构操作系统,第1章 操作系统概述1
热门文章
- 图像识别没你想的那么难!看完这篇你也能成专家
- python数独游戏
- 如何用matlab求出矩阵简化阶梯形顺带算出主元所在的列
- macOS Outlook 查看邮件的源码 HTML源码
- Profile Owner使用总结
- DeepFool论文翻译---DeepFool: a simple and accurate method to fool deep neural networks
- iOS 开发 code sign 代码签名深入剖析
- MongoDB实战(MongoDB开发者现身说法)
- 使用阿里云的ip地址查询服务-使用java调用ip地址查询服务
- h5调用手机相册摄像头以及文件夹