到http://sourceforge.net/projects/jfreechart/下载JFreeChart
struts2-jfreechart-plugin-2.1.6.jar 、jfreechart-1.0.13.jar、jcommon-1.0.16.jar
struts.xml配置
<package name="jFreeChart" extends="jfreechart-default"> <!--这里特别注意-->
<action name="JFreeChart" class="jfreeChartAction">
<result type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
</package>
JFreeChartAction.java
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import com.opensymphony.xwork2.ActionSupport;
public class JFreeChartAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private JFreeChart chart;
@Override
public String execute() throws Exception {
// 设置数据
DefaultPieDataset data = new DefaultPieDataset();
data.setValue( " Java " , new Double( 43.2 ));
data.setValue( " Visual Basic " , new Double( 1.0 ));
data.setValue( " C/C++ " , new Double( 17.5 ));
data.setValue( " tangjun " , new Double( 60.0 ));
// 生成 JFreeChart对象
chart = ChartFactory.createPieChart( " Pie Chart " , data, true , true , false );
return SUCCESS;
}
public JFreeChart getChart() {
return chart;
}
public void setChart(JFreeChart chart) {
this .chart = chart;
}
}
访问地址http://localhost:8080/JFreeChart/JFreeChart.action