JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="viewResult.action">
<s:checkbox name="interest" label="足球" fieldValue="football" labelposition="left"></s:checkbox>
<s:checkbox name="interest" label="篮球" fieldValue="basketball" labelposition="left"></s:checkbox>
<s:checkbox name="interest" label="排球" fieldValue="volleyball" labelposition="left"></s:checkbox>
<s:checkbox name="interest" label="羽毛球" fieldValue="badminton" labelposition="left"></s:checkbox>
<!--
<s:checkboxlist list="#{'computer' : '计算机' , 'math' : '数学'}" name="interest" labelposition="top" label="陈学港">
</s:checkboxlist>
-->
<s:submit></s:submit>
</s:form>
</body>
</html>
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<String> interest;
@SuppressWarnings("unchecked")
private void increaseResult(List<String> 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<String> getInterest() {
return interest;
}
public void setInterest(List<String> 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配置
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
Struts.xml配置(注意:struts必须继承jfreechart-default)
<action name="viewResult" class="org.cxg.struts.action.ViewResultAction">
<result name="success" type="chart">
<param name="height">800</param>
<param name="width">600</param>
</result>
</action>
基本思路:
在Action中定义private JFreeChart chart; private List<String> interest;两个属性,并写出get,set方法.(注意JFreeChart对象的属性值能为chart)
从JSP页面中提交数据到Action中,在Action中通过List<String> interest 属性接受值,并将值通过increaseResult()方法放入到Applicatoin属性范围中,然后在getDataset()方法中取出Applicatoin属性范围的值并生成树状图数据集,当execute()方法返回结果,然后Struts会调用Action中的chart属性的getChart()方法,当调用getChart()方法时,会产生一个JFreeChart对象,返回到页面会生成树状图图标
分享到:
相关推荐
该资源详细介绍了JFreeChart结合Struts2的五种图像实践方法例子。
struts和jfreecha和整合已经使用的说明!
jfreeChart与Struts2结合使用,简单的环境搭建附有详细的代码,可以很快入手Jfreechart的使用
struts2整合jfreechart的文档,其实很简单的。
jfreechart和struts2的例子,很完整的例子
struts2整合 jfreechart 实现报表组件
jfreechart 与struts2的柱状图以及折线图例子,很给力呀,可以用大家请下载。
jfreechart整合struts2的压缩包 其中包括相关jar包和struts2教程文档说明
JFreeChart与Struts结合生成多维3D柱状图的实例 report.jsp -- JSP文件 ReportAction.java -- Action类 JFreeChartUtils.java -- 工具类
struts2整合JFreechart: jcommon-1.0.16.jar jfreechart-1.0.13.jar struts2-jfreechart-plugin-2.0.11.jar 导成图片操作说明
已经修改过的整合插件,在struts2中生成图表使用
一个完整的JSP struts2整合jfreechart的例子
NULL 博文链接:https://jackaney.iteye.com/blog/1041025
对于struts1和JFreeChart开发,灵活从jsp页面转向Action到页面
Struts2整合JFreeChart 实现柱状图表、Struts2整合JFreeChart 实现柱状图表、Struts2整合JFreeChart 实现柱状图表。
jfreechart与struts2结合,导入excel数据生成图形报表
包括struts2整合JFreeChart的jcommom、jfreechart、struts2-jfreechart-plugin这3个组件,方便制作静态表格
帮助理解jfreechart+struts2
Struts2应用开发系列Struts2整合JFreeChart