---
title: "JFreeChart  与 Struts2 整合小例子"
author: "mywebcode"
date: "2010-05-12T22:23:00Z"
tags: []
slug: "jfreechart-struts2"
draft: false
lastmod: "2013-05-08T10:39:09Z"
id: 1863315
url: "https://mywebcode.iteye.com/blog/1863315"
---

## JSP页面

&lt;%@ page language="java" contentType="text/html; charset=UTF-8"  
pageEncoding="UTF-8"%&gt;  
&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;  
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[http://www.w3.org/TR/html4/loose.dtd](http://www.w3.org/TR/html4/loose.dtd)"&gt;  
&lt;html&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;  
&lt;title&gt;Insert title here&lt;/title&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;s:form action="viewResult.action"&gt;  
&lt;s:checkbox name="interest" label="足球" fieldValue="football" labelposition="left"&gt;&lt;/s:checkbox&gt;  
&lt;s:checkbox name="interest" label="篮球" fieldValue="basketball" labelposition="left"&gt;&lt;/s:checkbox&gt;  
&lt;s:checkbox name="interest" label="排球" fieldValue="volleyball" labelposition="left"&gt;&lt;/s:checkbox&gt;  
&lt;s:checkbox name="interest" label="羽毛球" fieldValue="badminton" labelposition="left"&gt;&lt;/s:checkbox&gt;  
&lt;!--  
&lt;s:checkboxlist list="#{'computer' : '计算机' , 'math' : '数学'}" name="interest" labelposition="top" label="陈学港"&gt;  
&lt;/s:checkboxlist&gt;  
\--&gt;  
&lt;s:submit&gt;&lt;/s:submit&gt;  
&lt;/s:form&gt;  
&lt;/body&gt;  
&lt;/html&gt;

## Action类(ViewResultAction)

package org.cxg.struts.action;

import java.awt.Font;  
import java.util.List;  
import java.util.Map;

import org.jfree.chart.ChartFactory;  
import org.jfree.chart.JFreeChart;  
import org.jfree.chart.axis.CategoryAxis;  
import org.jfree.chart.axis.CategoryLabelPosition;  
import org.jfree.chart.axis.CategoryLabelPositions;  
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.ActionContext;  
import com.opensymphony.xwork2.ActionSupport;

public class ViewResultAction extends ActionSupport {

private JFreeChart chart;  
private List&lt;String&gt; interest;

@SuppressWarnings("unchecked")  
private void increaseResult(List&lt;String&gt; list){  
ActionContext context = ActionContext.getContext();  
Map map = context.getApplication();  
for(String string : list){  
System.out.println("8888"+string);  
if(null == map.get(string))  
{

map.put(string, 1);  
}  
else  
{  
map.put(string, (Integer)map.get(string) + 1);  
}  
}  
}

private CategoryDataset getDataset(){  
DefaultCategoryDataset dataset = new DefaultCategoryDataset();

this.increaseResult(this.interest);

ActionContext context = ActionContext.getContext();  
Map map = context.getApplication();

dataset.setValue((Integer)map.get("football"), "", "足球");  
dataset.setValue((Integer)map.get("basketball"), "", "篮球");  
dataset.setValue((Integer)map.get("volleyball"), "", "排球");  
dataset.setValue((Integer)map.get("badminton"), "", "羽毛球");

return dataset;

}

@Override  
public String execute() throws Exception {

return super.SUCCESS;

}

public List&lt;String&gt; getInterest() {  
return interest;  
}

public void setInterest(List&lt;String&gt; interest) {  
this.interest = interest;  
}

public JFreeChart getChart() {

chart = ChartFactory.createBarChart("兴趣统计结果", "项目", "结果", this.getDataset(), PlotOrientation.VERTICAL, false, false, false);

chart.setTitle(new TextTitle("兴趣统计结果", new Font("黑体",Font.BOLD,22)));

CategoryPlot plot = (CategoryPlot)chart.getPlot();

//设置横轴s  
CategoryAxis categoryAxis = plot.getDomainAxis();

categoryAxis.setLabelFont(new Font("宋体",Font.BOLD,12));

categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP\_45);

//设总走  
NumberAxis numberAxis = new NumberAxis();  
numberAxis.setLabelFont(new Font("宋体",Font.BOLD,12));

return chart;  
}

public void setChart(JFreeChart chart) {  
this.chart = chart;  
}

}

## web.xml配置

&lt;servlet&gt;  
&lt;servlet-name&gt;DisplayChart&lt;/servlet-name&gt;  
&lt;servlet-class&gt;org.jfree.chart.servlet.DisplayChart&lt;/servlet-class&gt;  
&lt;/servlet&gt;

## Struts.xml配置(注意:struts必须继承jfreechart-default)

&lt;action name="viewResult" class="org.cxg.struts.action.ViewResultAction"&gt;  
&lt;result name="success" type="chart"&gt;  
&lt;param name="height"&gt;800&lt;/param&gt;  
&lt;param name="width"&gt;600&lt;/param&gt;  
&lt;/result&gt;  
&lt;/action&gt;

基本思路:  
在Action中定义private JFreeChart chart; private List&lt;String&gt; interest;两个属性,并写出get,set方法.(注意JFreeChart对象的属性值能为chart)  
从JSP页面中提交数据到Action中,在Action中通过List&lt;String&gt; interest 属性接受值,并将值通过increaseResult()方法放入到Applicatoin属性范围中,然后在getDataset()方法中取出Applicatoin属性范围的值并生成树状图数据集,当execute()方法返回结果,然后Struts会调用Action中的chart属性的getChart()方法,当调用getChart()方法时,会产生一个JFreeChart对象,返回到页面会生成树状图图标