页面:
<img alt="" src="<%=request.getContextPath()%>/https/mystruts/test_view_picture.html" />urlRewrite:
<rule>
<from>^/https/mystruts/test_view_picture.html$</from>
<to>/pages/mystruts/testViewPicture.action</to>
</rule>
struts-config:
<package name="pic" extends="jfreechart-default" namespace="/pages/mystruts">
<action name="testViewPicture" class="com.ito.hsr.action.get.TestViewPictureAction" method="getChart">
<result name="success" type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action>
</package>
public class TestViewPictureAction extends ActionSupport{ public JFreeChart getChart(){
JFreeChart chart = ChartFactory.createPieChart3D(
"图书统计图", // 图表标题
getDataSet(), //数据
true, // 是否显示图例
false, //是否显示工具提示
false //是否生成URL
);
//重新设置图标标题,改变字体
chart.setTitle(new TextTitle("图书统计图", new Font("黑体", Font.ITALIC , 22)));
//取得统计图标的第一个图例
LegendTitle legend = chart.getLegend(0);
//修改图例的字体
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
//获得饼图的Plot对象
PiePlot plot = (PiePlot)chart.getPlot();
//设置饼图各部分的标签字体
plot.setLabelFont(new Font("隶书", Font.BOLD, 18));
//设定背景透明度(0-1.0之间)
plot.setBackgroundAlpha(0.9f);
//设定前景透明度(0-1.0之间)
plot.setForegroundAlpha(0.50f);
System.out.println("chart===="+chart);
return chart;
}
private DefaultPieDataset getDataSet(){
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Spring2.0",47000);
dataset.setValue("J2EE",38000);
dataset.setValue("Ajax",31000);
dataset.setValue("JavaScript",29000);
dataset.setValue("Ajax In Action",25000);
return dataset;
}
}报错
java.lang.ClassCastException: org.jfree.chart.JFreeChart
<img alt="" src="<%=request.getContextPath()%>/https/mystruts/test_view_picture.html" />urlRewrite:
<rule>
<from>^/https/mystruts/test_view_picture.html$</from>
<to>/pages/mystruts/testViewPicture.action</to>
</rule>
struts-config:
<package name="pic" extends="jfreechart-default" namespace="/pages/mystruts">
<action name="testViewPicture" class="com.ito.hsr.action.get.TestViewPictureAction" method="getChart">
<result name="success" type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action>
</package>
public class TestViewPictureAction extends ActionSupport{ public JFreeChart getChart(){
JFreeChart chart = ChartFactory.createPieChart3D(
"图书统计图", // 图表标题
getDataSet(), //数据
true, // 是否显示图例
false, //是否显示工具提示
false //是否生成URL
);
//重新设置图标标题,改变字体
chart.setTitle(new TextTitle("图书统计图", new Font("黑体", Font.ITALIC , 22)));
//取得统计图标的第一个图例
LegendTitle legend = chart.getLegend(0);
//修改图例的字体
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
//获得饼图的Plot对象
PiePlot plot = (PiePlot)chart.getPlot();
//设置饼图各部分的标签字体
plot.setLabelFont(new Font("隶书", Font.BOLD, 18));
//设定背景透明度(0-1.0之间)
plot.setBackgroundAlpha(0.9f);
//设定前景透明度(0-1.0之间)
plot.setForegroundAlpha(0.50f);
System.out.println("chart===="+chart);
return chart;
}
private DefaultPieDataset getDataSet(){
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Spring2.0",47000);
dataset.setValue("J2EE",38000);
dataset.setValue("Ajax",31000);
dataset.setValue("JavaScript",29000);
dataset.setValue("Ajax In Action",25000);
return dataset;
}
}报错
java.lang.ClassCastException: org.jfree.chart.JFreeChart
解决方案 »
- oracle 内置表添加数据
- java 如何将PDF 转化 为tiff 文件??
- 求CMS系统例子程序
- getHibernateTemplate()的使用方法还有里面的类具体是做什么的
- 哪位大哥能帮忙解释一下MQ吗? 还有JMS
- 高分挑战高手:jsp访问ejb
- 150分请ireport 导出excel问题
- 求高人点经hibernate:(在线等。。。)JDBC Driver class not found: com.mysql.jdbc.Driver
- [高分救助]JBuilder+Weblogic开发CMP的数据源问题
- java dos命令下 把外部用到的jar包和自己的class文件打成一个jar
- J2EE 下出现The requested resource () is not available.错误
- 一个类中实现了2次序列化接口 java.io.Serializable,会出问题吗?
<img alt="" src="<%=request.getContextPath()%>/https/mystruts/test_view_picture.html" />引用有问题吧,应该指定action
<img src="<%=graphURL%>"
usemap="#<%=filename %>">
<package name="jFreeChartDemonstration" extends="struts-default"
namespace="/jfreechart">
<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>
</result-types>
<action name="JFreeChartAction" class="com.tangjun.struts2.JFreeChartAction">
<result type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
</package>
LZ,你配置错误了。
<action name="testViewPicture" class="com.ito.hsr.action.get.TestViewPictureAction" method="getChart">
<result name="success" type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action>
这样当然要报类转换错误了,你这个是String,action返回的时候是JfreeChart。
<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>
</result-types>这句我加上还是报错
Action代码package com;import java.awt.Font;import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;import com.opensymphony.xwork2.ActionSupport;public class TestViewPictureAction extends ActionSupport{
private JFreeChart chart;
public String execute(){
chart = ChartFactory.createPieChart3D(
"图书统计图", // 图表标题
getDataSet(), //数据
true, // 是否显示图例
false, //是否显示工具提示
false //是否生成URL
);
//重新设置图标标题,改变字体
chart.setTitle(new TextTitle("图书统计图", new Font("黑体", Font.ITALIC , 22)));
//取得统计图标的第一个图例
LegendTitle legend = chart.getLegend(0);
//修改图例的字体
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
//获得饼图的Plot对象
PiePlot plot = (PiePlot) chart.getPlot();
//设置饼图各部分的标签字体
plot.setLabelFont(new Font("隶书", Font.BOLD, 18));
//设定背景透明度(0-1.0之间)
plot.setBackgroundAlpha(0.9f);
//设定前景透明度(0-1.0之间)
plot.setForegroundAlpha(0.50f);
System.out.println("chart===="+chart);
return "success";
}
private DefaultPieDataset getDataSet(){
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Spring2.0",47000);
dataset.setValue("J2EE",38000);
dataset.setValue("Ajax",31000);
dataset.setValue("JavaScript",29000);
dataset.setValue("Ajax In Action",25000);
return dataset;
}
public JFreeChart getChart() {
return chart;
}
public void setChart(JFreeChart chart) {
this.chart = chart;
}
}
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>
<package name="pic" extends="struts-default" namespace="/">
<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>
</result-types>
<action name="testViewPicture" class="com.TestViewPictureAction">
<result name="success" type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action>
</package>
<constant name="struts.custom.i18n.resources" value="globalMessages" />
</struts> 主要的问题:
action中不能直接返回JFreeChart类型,应该返回字符串。并且声明一个JFreeChart类型的变量,名称必须是chart,并提供get/set方法。另外struts.xml中必须声明chart类型
<package name="jfreechart-default" extends="struts-default">
<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult">
<param name="height">150</param>
<param name="width">200</param>
</result-type>
</result-types>
</package>
</struts>