为了生成热点图的map标签,我修改了org.apache.struts2.dispatcher.ChartResult类的execute方法 public void execute(ActionInvocation invocation) throws Exception {
JFreeChart chart = null; if (chartSet) {
chart = this.chart;
} else {
chart = (JFreeChart) invocation.getStack().findValue("chart");
} if (chart == null) {
throw new NullPointerException("No chart found");
} HttpServletResponse response = ServletActionContext.getResponse();
OutputStream os = response.getOutputStream();
FileOutputStream fos = new FileOutputStream(ServletActionContext.getRequest().getRealPath("/chart.map"));
ChartRenderingInfo info = new ChartRenderingInfo();
ChartUtilities.writeChartAsJPEG(os, chart, width, height,info);
PrintWriter pw = new PrintWriter(fos);
ChartUtilities.writeImageMap(pw,"mapzexiao", info,true);
pw.flush();
os.flush();
}ChartAction中的public String charts(){
return SUCCESS;
}
private CategoryDataset getDataset(){
省略,能正确取得数据集
}
public JFreeChart getChart() {
..设置的相关代码
return chart;
}
struts.xml文件相关配置: <action name="chart" class="chartAction" method="charts">
<result name="success" type="chart">
<param name="height">500</param>
<param name="width">800</param>
</result>
</action>
jsp页面:
<img src="chart.action" usemap="#map0"/>
<jsp:include page="chart.map" />
这样可以正确得到图表。但是map文件都是上一次生成的
因为当jsp文件加载map文件时,新的map文件还没输出。也就是图片文件流还没有完成,就已经加载了已经存在的(即上一次生成的)chart.map文件
所以嵌套到jsp页面的map文件就还是上一次的map文件,只有刷新页面就能正确得到所需要的map文件。请求解决办法,或者有没有其他更好的办法。
JFreeChart chart = null; if (chartSet) {
chart = this.chart;
} else {
chart = (JFreeChart) invocation.getStack().findValue("chart");
} if (chart == null) {
throw new NullPointerException("No chart found");
} HttpServletResponse response = ServletActionContext.getResponse();
OutputStream os = response.getOutputStream();
FileOutputStream fos = new FileOutputStream(ServletActionContext.getRequest().getRealPath("/chart.map"));
ChartRenderingInfo info = new ChartRenderingInfo();
ChartUtilities.writeChartAsJPEG(os, chart, width, height,info);
PrintWriter pw = new PrintWriter(fos);
ChartUtilities.writeImageMap(pw,"mapzexiao", info,true);
pw.flush();
os.flush();
}ChartAction中的public String charts(){
return SUCCESS;
}
private CategoryDataset getDataset(){
省略,能正确取得数据集
}
public JFreeChart getChart() {
..设置的相关代码
return chart;
}
struts.xml文件相关配置: <action name="chart" class="chartAction" method="charts">
<result name="success" type="chart">
<param name="height">500</param>
<param name="width">800</param>
</result>
</action>
jsp页面:
<img src="chart.action" usemap="#map0"/>
<jsp:include page="chart.map" />
这样可以正确得到图表。但是map文件都是上一次生成的
因为当jsp文件加载map文件时,新的map文件还没输出。也就是图片文件流还没有完成,就已经加载了已经存在的(即上一次生成的)chart.map文件
所以嵌套到jsp页面的map文件就还是上一次的map文件,只有刷新页面就能正确得到所需要的map文件。请求解决办法,或者有没有其他更好的办法。
让浏览器再去读取图片。不过ChartDeleter这个类设计的很烂,如果要求不高重构时还是可用的!