页面:
<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

解决方案 »

  1.   

    呀,俺刚这项目也用到JFreeChart
      

  2.   

    web.xml里配置jfreechart,你配置了没?
      

  3.   

    多说一句,我在action中对图片进行保存,确实能得到图片,我觉得报错是在返回图片到页面时出了问题。。
      

  4.   

    页面:
    <img alt="" src="<%=request.getContextPath()%>/https/mystruts/test_view_picture.html" />引用有问题吧,应该指定action
      

  5.   

    老感觉你的src写的有问题,难道不用这么写吗
    <img src="<%=graphURL%>" 
    usemap="#<%=filename %>">
      

  6.   

    <%=request.getContextPath()%>/https/mystruts/test_view_picture.html这个是UrlRewrite 其实还是指向一个action
      

  7.   

    代码发到我邮箱[email protected],我看看?
      

  8.   


    <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。
      

  9.   


    <result-types>
                <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>
            </result-types>这句我加上还是报错
      

  10.   

    我研究了一下你的代码,然后改好了,帖代码:
    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类型
      

  11.   

    还有package中的extends属性必须是struts-default,如果你写jfreechart-default,则必须在struts2-jfreechart-plugin-xxx.jar中struts-plugin.xml中声明这个package才行<struts>
        <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>
      

  12.   

    楼主要是解决了,把代码发到我邮箱[email protected]里可以吗?我主要是想看一下你的urlrewrite是怎么用的,顺便学习一下,谢谢!