Struts2整合JasperReport输出HTML显示图片问题

我用的是struts2.0.11,iReport3.6.7
struts2整合JasperReport参考:http://www.blogjava.net/sterning/archive/2008/01/02/172317.html

按照一下方法解决我没有成功:(在浏览器上显示有很多红色XX)

1.web.xml配置
<servlet>
<servlet-name>JasperReportImageServlet</servlet-name>
<servlet-class>
net.sf.jasperreports.j2ee.servlets.ImageServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JasperReportImageServlet</servlet-name>
<url-pattern>/image</url-pattern>
</servlet-mapping>
2.配置成struts.action.extends=action
3.在WebRoot建立一个images文件夹,放入px文件
若要显示自己在报表中定义的图片,再加上红色部分
<action name="HTMLReport" class="reportAction">
<result type="jasper">
<param name="location">/jasper/report.jasper</param>
<param name="format">HTML</param>
<param name="reportParameters">map</param>
<param name="dataSource">funList</param>
<param name="imageServletUrl"><![CDATA[/image?image=]]></param>
</result>
</action>

用重写JasperReportsResult.java中的doExcute()方法
@SuppressWarnings({ "unchecked", "deprecation" })
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
if (this.format == null) {
this.format = FORMAT_PDF;
}

if (dataSource == null) {
String message = "No dataSource specified...";
LOG.error(message);
throw new RuntimeException(message);
}

if (LOG.isDebugEnabled()) {
LOG.debug("Creating JasperReport for dataSource = " + dataSource + ", format = " + this.format);
}

HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(ServletActionContext.HTTP_RESPONSE);

//construct the data source for the report
ValueStack stack = invocation.getStack();
ValueStackDataSource stackDataSource = new ValueStackDataSource(stack, dataSource);

format = conditionalParse(format, invocation);
dataSource = conditionalParse(dataSource, invocation);

if (contentDisposition != null) {
contentDisposition = conditionalParse(contentDisposition, invocation);
}

if (documentName != null) {
documentName = conditionalParse(documentName, invocation);
}

// (Map) ActionContext.getContext().getSession().get("IMAGES_MAP");
if (!TextUtils.stringSet(format)) {
format = FORMAT_PDF;
}

if (!"contype".equals(request.getHeader("User-Agent"))) {
// Determine the directory that the report file is in and set the reportDirectory parameter
// For WW 2.1.7:
// ServletContext servletContext = ((ServletConfig) invocation.getInvocationContext().get(ServletActionContext.SERVLET_CONFIG)).getServletContext();
ServletContext servletContext = (ServletContext) invocation.getInvocationContext().get(ServletActionContext.SERVLET_CONTEXT);
String systemId = servletContext.getRealPath(finalLocation);
Map parameters = new ValueStackShadowMap(stack);
File directory = new File(systemId.substring(0, systemId.lastIndexOf(File.separator)));
parameters.put("reportDirectory", directory);
parameters.put(JRParameter.REPORT_LOCALE, invocation.getInvocationContext().getLocale());

byte[] output;
JasperPrint jasperPrint;

// Fill the report and produce a print object
try {
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(systemId);

jasperPrint =
JasperFillManager.fillReport(jasperReport,
parameters,
stackDataSource);
} catch (JRException e) {
LOG.error("Error building report for uri " + systemId, e);
throw new ServletException(e.getMessage(), e);
}

// Export the print object to the desired output format
try {
if (contentDisposition != null || documentName != null) {
final StringBuffer tmp = new StringBuffer();
tmp.append((contentDisposition == null) ? "inline" : contentDisposition);

if (documentName != null) {
tmp.append("; filename=");
tmp.append(documentName);
tmp.append(".");
tmp.append(format.toLowerCase());
}

response.setHeader("Content-disposition", tmp.toString());
}

if (format.equals(FORMAT_PDF)) {
response.setContentType("application/pdf");

// response.setHeader("Content-disposition", "inline; filename=report.pdf");
output = JasperExportManager.exportReportToPdf(jasperPrint);
} else {
JRExporter exporter;

if (format.equals(FORMAT_CSV)) {
response.setContentType("text/plain");
exporter = new JRCsvExporter();
} 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);
[color=red]//<--begin added by twolf, 20101229
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 -->[/color]
// 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)) {
response.setContentType("application/vnd.ms-excel");
exporter = new JRXlsExporter();
} else if (format.equals(FORMAT_XML)) {
response.setContentType("text/xml");
exporter = new JRXmlExporter();
} else if (format.equals(FORMAT_RTF)) {
response.setContentType("application/rtf");
exporter = new JRRtfExporter();
} else {
throw new ServletException("Unknown report format: " + format);
}

output = exportReportToBytes(jasperPrint, exporter);
}
} catch (JRException e) {
String message = "Error producing " + format + " report for uri " + systemId;
LOG.error(message, e);
throw new ServletException(e.getMessage(), e);
}

response.setContentLength(output.length);

ServletOutputStream ouputStream;

try {
ouputStream = response.getOutputStream();
ouputStream.write(output);
ouputStream.flush();
ouputStream.close();
} catch (IOException e) {
LOG.error("Error writing report output", e);
throw new ServletException(e.getMessage(), e);
}
} else {
// Code to handle "contype" request from IE
try {
ServletOutputStream outputStream;
response.setContentType("application/pdf");
response.setContentLength(0);
outputStream = response.getOutputStream();
outputStream.close();
} catch (IOException e) {
LOG.error("Error writing report output", e);
throw new ServletException(e.getMessage(), e);
}
}
}
然后struts.xml添加上
<result-types>
<result-type name="jasper" class="com.wj.common.util.JasperReportsResult" default="false"/>
</result-types>
XX图标就可以消失了。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值