试验了一下午,终于找到图片不能显示的原因,struts2是一个拦截器,把servlet拦截了,故不能找到配置的DisplayChart,通过在strus.xml里配置
<constant name="struts.action.extension" value="do,action"/>让struts2不拦截servlet,就可以显示map了。
注:此带热点的Action没有使用struts2的jfreechart插件。、
导入包:
struts2-jfreechart-plugin-2.1.8.1.jar
jfreechart-1.0.13.jar
jcommon-1.0.16.jar
1.ChartAction(带热点的Action)
package com.cs.struts2.helloworld;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.data.general.DefaultPieDataset;
import com.opensymphony.xwork2.ActionSupport;
public class ChartAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{
private HttpServletRequest request;
private HttpServletResponse response;
private String mapMessage;
private String src;
public String getChart() throws IOException {
//生成饼图
JFreeChart chart = ChartFactory.createPieChart(
"图书销售统计表", //图表标题
getDateSet(), //数据
true, //是否显示图例
true, //是否显示工具提示
true //是否生成URL
);
//设置标题及标题字体
chart.setTitle(new TextTitle("图书销售统计图",new Font("黑体",Font.ITALIC,22)));
//建一个图例
LegendTitle legendTitle = chart.getLegend(0);
//设置图例字体
legendTitle.setItemFont(new Font("宋体",Font.BOLD,14));
//获取饼图plot对象
PiePlot plot = (PiePlot) chart.getPlot();
//根据key指定各个数据饼图的颜色
plot.setSectionPaint("JAVA教程", Color.RED);
plot.setSectionPaint("c++教程", Color.BLUE);
plot.setSectionPaint("C#教程", Color.GREEN);
plot.setSectionPaint("VC++教程", Color.ORANGE);
//设置plot字体
plot.setLabelFont(new Font("宋体",Font.BOLD,18));
//设置背景透明度(0~1)
plot.setBackgroundAlpha(0.9f);
//-------------------热点设置分割线----------------------------
//设定链接
plot.setNoDataMessage("No data available");
String str =request.getContextPath()+"/first/bchart.action";
//StandardPieURLGenerator()不设置第二个参数"type"默认为category
plot.setURLGenerator(new StandardPieURLGenerator(str,"type"));
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
StandardEntityCollection sec = new StandardEntityCollection();
//生成RenderingInfo实例,info参数就是图片的热点信息
ChartRenderingInfo info = new ChartRenderingInfo(sec);
response.setCharacterEncoding("GBK");
String filename = null;
try {
filename = ServletUtilities.saveChartAsJPEG(chart, 700, 500, info, request.getSession());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mapMessage = ChartUtilities.getImageMap("map0", info);
src = request.getContextPath()+"/servletDisplayChart?filename=" + filename;
return "first";
}
private static DefaultPieDataset getDateSet() {
//提供生成饼图的数据
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("JAVA教程", 47);
dataset.setValue("c++教程", 23);
dataset.setValue("C#教程", 20);
dataset.setValue("VC++教程", 10);
return dataset;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public String getMapMessage() {
return mapMessage;
}
public void setMapMessage(String mapMessage) {
this.mapMessage = mapMessage;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
}
2.ChartActionB(热点目标Action)
package com.cs.struts2.helloworld;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import com.opensymphony.xwork2.ActionSupport;
public class ChartActionB extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
public JFreeChart getChart() throws IOException {
//---------------------逻辑判断分割线-------------------------------
CategoryDataset dataset;
String queryString = request.getQueryString();
queryString = java.net.URLDecoder.decode(queryString , "UTF-8");
System.out.println(queryString);
String type = null;
for(String param : queryString.split("&"))
{
String[] nameValue = param.split("=");
type = nameValue[0].equals("type") ? nameValue[1] : type;
}
String subTitle = "";
if(type.equals("JAVA教程"))
{
dataset = getDateSet1();
subTitle="JAVA教程";
}
else if(type.equals("c++教程"))
{
dataset = getDateSet4();
subTitle="c++教程";
}
else if(type.equals("C#教程"))
{
dataset = getDateSet2();
subTitle="C#教程";
}
else
{
dataset = getDateSet3();
subTitle="VC++教程";
}
//String title = subTitle + "图书的销售情况";
//-------------------------生成图片分割线---------------------
//生成柱状图
JFreeChart chart = ChartFactory.createBarChart (
"图书销售统计表", //图表标题
"月份", //目录轴的显示标签
"销量", //数值轴的显示标签
dataset, //数据
//PlotOrientation.HORIZONTAL, //图表方向水平
PlotOrientation.VERTICAL, //图表方向垂直
false, //是否显示图例
false, //是否显示工具提示
false //是否生成URL
);
//设置标题及标题字体
chart.setTitle(new TextTitle("<<"+subTitle+">>销售统计图",new Font("黑体",Font.ITALIC,22)));
//建一个图例
//LegendTitle legendTitle = chart.getLegend(0);
//设置图例字体
//legendTitle.setItemFont(new Font("宋体",Font.BOLD,14));
//获取柱状图plot对象
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//设置柱型的颜色
plot.getRenderer().setSeriesPaint(0, Color.blue);
//取得横轴
CategoryAxis categoryAxis = plot.getDomainAxis();
//设置横轴的字体
categoryAxis.setLabelFont(new Font("宋体",Font.BOLD,22));
//设置分类标签以45度倾斜
//categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
//设置分类标签字体
categoryAxis.setTickLabelFont(new Font("宋体",Font.BOLD,22));
//取得纵轴
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
//设置纵轴的字体
numberAxis.setLabelFont(new Font("宋体",Font.BOLD,22));
//设置背景透明度(0~1)
plot.setBackgroundAlpha(0.9f);
return chart;
}
//------------------------提供数据分割线--------------------
private static DefaultCategoryDataset getDateSet4() {
System.out.println("c++");
//提供生成柱形图的数据
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(47,"c++教程", "06年10月");
dataset.addValue(60,"c++教程", "06年11月");
dataset.addValue(20,"c++教程","06年12月" );
dataset.addValue(50,"c++教程","07年1月");
return dataset;
}
private static DefaultCategoryDataset getDateSet1() {
System.out.println("java");
//提供生成柱形图的数据
DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
dataset1.addValue(47,"JAVA教程", "06年10月");
dataset1.addValue(23,"JAVA教程", "06年11月");
dataset1.addValue(20,"JAVA教程","06年12月" );
dataset1.addValue(10,"JAVA教程", "07年1月");
return dataset1;
}
private static DefaultCategoryDataset getDateSet2() {
System.out.println("C#");
//提供生成柱形图的数据
DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
dataset2.addValue(27,"C#教程", "06年10月");
dataset2.addValue(23,"C#教程", "06年11月");
dataset2.addValue(40,"C#教程","06年12月" );
dataset2.addValue(70,"C#教程", "07年1月");
return dataset2;
}
private static DefaultCategoryDataset getDateSet3() {
System.out.println("VC++");
//提供生成柱形图的数据
DefaultCategoryDataset dataset3 = new DefaultCategoryDataset();
dataset3.addValue(48,"VC++教程", "06年10月");
dataset3.addValue(63,"VC++教程", "06年11月");
dataset3.addValue(29,"VC++教程","06年12月" );
dataset3.addValue(49,"VC++教程","07年1月");
return dataset3;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
3.struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.action.extension" value="do,action"/>
<package name="first" extends="struts-default" namespace="/first">
<action name="chart" class="com.cs.struts2.helloworld.ChartAction" method="getChart">
<result name="first">first.jsp</result>
</action>
</package>
<package name="b" namespace="/first" extends="jfreechart-default">
<action name="bchart" class="com.cs.struts2.helloworld.ChartActionB">
<result type="chart">
<param name="width">700</param>
<param name="height">400</param>
</result>
</action>
</package>
</struts>
4.first.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<P ALIGN="CENTER">
<s:property value="mapMessage" escape="false"/>
<img src="<s:property value='src'/>" border=0 usemap="#map0">
</P>
5.web.xml
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servletDisplayChart</url-pattern>
</servlet-mapping>