很多人遇到过jasperReport生成的图片会在IE下缓存,多个报表都成了同一个图片.
网上有很多是从jasperReport的编译方式入手,也可以解决.但是我这里要说的是跟Struts2整合后的解决办法,一种更简单的办法
原来我们在做报表的时候:
<result name="success" type="result">
<param name="location">/jasper/regUseLine.jasper</param>
<param name="format">HTML</param>
<param name="reportParameters">reportParameter</param>
<param name="dataSource">chardatabean</param>
<param name="imageServletUrl">/servlets/image?image=</param>
</result>
struts.xml中给Action配置一个result,其中有个属性叫imageServletUrl,解决办法就是给这个请求路径加一个时间戳.
第一步,先写一个Struts2的Result,可以继承JasperReportsResult,然后覆盖里面的属性值,加一个时间戳参数.如此就解决IE下的缓存了.
package report.common.result;
import java.util.Date;
import org.apache.struts2.views.jasperreports.JasperReportsResult;
import com.opensymphony.xwork2.ActionInvocation;
@SuppressWarnings("serial")
public class JsperReportResult extends JasperReportsResult {
@Override
public String getImageServletUrl(){
String[] url = this.imageServletUrl.split("\\?");
StringBuffer t = new StringBuffer("");
t.append(url[0]+("?_t="+(new Date()).getTime())).append("&"+url[1]);
return t.toString();
}
@Override
protected void doExecute(String finalLocation, ActionInvocation invocation)
throws Exception {
this.imageServletUrl = this.getImageServletUrl();
super.doExecute(finalLocation, invocation);
}
}
第二部,修改你的struts.xml,
<package name="jasperReport" extends="struts-default,jasperreports-default">
<result-types>
<result-type name="jasperResult" class="report.common.result.JsperReportResult"></result-type>
</result-types>
<action name="HTML" class="report.actions.jsperreport.ChardataAction">
<result name="success" type="jasperResult">
<param name="location">/jasper/regUseLine.jasper</param>
<param name="format">HTML</param>
<param name="reportParameters">reportParameter</param>
<param name="dataSource">chardatabean</param>
<param name="imageServletUrl">/servlets/image?image=</param>
</result>
</action>
</package>
注意,将你的返回类型改为你配置的result-type,我这里名字叫jasperResult