前言

关于Struts2入门以及提高等在这里就不介绍了,但是关于Struts2的学习有以下推荐:

    1. struts2-showcase-2.0.6.war:这个是官方自带的Demo(struts-2.0.6-all.zip\struts-2.0.6\apps目录下),非常全面,直接部署就可以了(很多朋友Struts2能学很好我估计还是直接从这里学来的)。
    2. wiki-WebWork:入了门的朋友应该都知道,strust2由webwork2和struts1.x合并起来的,但主要还是以webwork2为主,所以如果找不到Struts2的资料可以找WebWork资料看看。
    3. Max On Java的博客,他的博客的资料在中文的Struts2算是比较全的了,写得很详细。
    4. The Code ProjectGoogle - CodeSearchKoders:这几个代码搜索网站在我找不到中文资料甚至英文文章的时候帮了我大忙!

关于JFreeChart入门等这里我也不打算介绍了,中文资料很多了。

正题

下面以边帖图片和代码的方式来讲解Struts2与JFreeChart的整合。
     搭建环境:首先帖一张工程的目录结构以及所需的jar包。注意:如果你不打算自己写ChartResult的话只需要引入struts2-jfreechart-plugin-2.0.6.jar(这个在struts-2.0.6-all.zip可以找到了):

1.依次帖web.xml、struts.xml、struts.properties和struts-jfreechart.xml几个配置文件的代码:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

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>
<include file="struts-jfreechart.xml" />
</struts>

struts.properties

struts.ui.theme=simple

struts-jfreechart.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<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"></param>
<param name="height"></param>
</result>
</action>
</package>
</struts>

说明:这里只需要说明下struts-jfreechart.xml,这里直接调用已经写好的类ChartResult,这个类是继承自com.opensymphony.xwork2.Result,传入生成图片大小的参数width和height就可以了。

2. 新建JFreeChartAction继承ActionSupport,生成JFreeChart对象并保存到chart中,注意这个名称是固定的。

package com.tangjun.struts2;

import com.opensymphony.xwork2.ActionSupport;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset; public class JFreeChartAction extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 5752180822913527064L; //供ChartResult调用->ActionInvocation.getStack().findValue("chart")
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;
}
}

OK!至此代码已经全部贴完。
输入访问 http://localhost:8080/Struts2JFreeChart/jfreechart/JFreeChartAction.action
显示结果如下:

补充

以上生成的图片是PNG格式的图片,如果需要自定义图片格式的话(好像只能支持JPG和PNG格式),那么自己写一个ChartResult继承自StrutsResultSupport,见代码:

package com.tangjun.struts2.chartresult;

import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart; import com.opensymphony.xwork2.ActionInvocation; public class ChartResult extends StrutsResultSupport { /**
*
*/
private static final long serialVersionUID = 4199494785336139337L; //图片宽度
private int width;
//图片高度
private int height;
//图片类型 jpg,png
private String imageType; @Override
protected void doExecute(String arg0, ActionInvocation invocation) throws Exception {
JFreeChart chart =(JFreeChart) invocation.getStack().findValue("chart");
HttpServletResponse response = ServletActionContext.getResponse();
OutputStream os = response.getOutputStream(); if("jpeg".equalsIgnoreCase(imageType) || "jpg".equalsIgnoreCase(imageType))
ChartUtilities.writeChartAsJPEG(os, chart, width, height);
else if("png".equalsIgnoreCase(imageType))
ChartUtilities.writeChartAsPNG(os, chart, width, height);
else
ChartUtilities.writeChartAsJPEG(os, chart, width, height); os.flush(); }
public void setHeight(int height) {
this.height = height;
} public void setWidth(int width) {
this.width = width;
} public void setImageType(String imageType) {
this.imageType = imageType;
} }

如此的话还需要小小的修改一下struts-jfreechart.xml:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="jFreeChartDemonstration" extends="struts-default"
namespace="/jfreechart">
<!-- 自定义返回类型 -->
<result-types>
<!--
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>
-->
<result-type name="chart" class="com.tangjun.struts2.chartresult.ChartResult"></result-type>
</result-types> <action name="JFreeChartAction" class="com.tangjun.struts2.JFreeChartAction">
<!--
<result type="chart">
<param name="width"></param>
<param name="height"></param>
</result>
-->
<result type="chart">
<param name="width"></param>
<param name="height"></param>
<param name="imageType">jpg</param>
</result>
</action>
</package>
</struts>

OK!显示的效果是一样的,只是图片格式不一样,当然这里面你可以做更多操作!

Struts2+JFreeChart的更多相关文章

  1. 投票系统开发总结struts2,jfreechart,cookie应用,以及前端技术

    struts2配置web.xml+struts.xml: <?xml version="1.0" encoding="UTF-8"?> <we ...

  2. [六]JFreeChart实践五之与Struts2整合

    1.Action,返回Chart package com.java1234.chart.bar; import java.awt.Color; import org.jfree.chart.Chart ...

  3. struts2整合JFreechart 饼图、折线图、柱形图

    struts2整合JFreechart 饼图.折线图.柱形图 上效果图: 当然可以将数据导出图片格式存储.具体下的链接里的文件有保存成图片的操作. 因为是strust2整合JFreechart,所以s ...

  4. JFreeChart与struts2整合实例

    1. 3个jar包 jcommon,jfreechart,strust2-jfreechart-plugin 2 <?xml version="1.0" encoding=& ...

  5. struts2整合jfreechart

    需要的包: struts2-jfreechart-plugin-2.2.1.1.jar jfreechart-1.0.13.jar jcommon-1.0.17.jar 前台jsp页面中可以使用ifr ...

  6. java项目中显示图表:struts2整合jfreechart

    需要的包: struts2-jfreechart-plugin-2.2.1.1.jar jfreechart-1.0.13.jar jcommon-1.0.17.jar 前台jsp页面中可以使用ifr ...

  7. struts2中 JFreeChart使用

    添加3个包 struts2-jfreechart-plugin-2.3.16.3.jar jcommon-1.0.16.jar jfreechart-1.0.13.jar struts.xml中配置 ...

  8. struts2中各个jar包作用

    Struts2.3.4 所需的Jar包及介绍 Jar包的分类 jar包名称 jar包版本 jar包 文件名 jar包 的作用 jar包内包含的主要包路径及主要类 依赖的自有jar包名称 依赖的第三方j ...

  9. 【Java EE 学习 74 下】【数据采集系统第六天】【使用Jfreechart的统计图实现】【将JFreechart整合到项目中】

    之前说了JFreechart的基本使用方法,包括生成饼图.柱状统计图和折线统计图的方法.现在需要将其整合到数据采集系统中根据调查结果生成三种不同的统计图. 一.统计模型的分析和设计 实现统计图显示的流 ...

随机推荐

  1. mybatis0207 resultType、resultMap、延迟加载使用场景总结

    延迟加载: 延迟加载实现的方法多种多样,在只查询单表就可以满足需求,为了提高数据库查询性能使用延迟加载,再查询关联信息. mybatis提供延迟加载的功能用于service层. resultType: ...

  2. JSP-标准动作标记

    JSP标准动作标记 在客户请求JSP页面时,JSP动作利用XML语法格式的标记来控制Servlet引擎的行为.利用JSP动作可以动态地插入文件.重用JavaBean组件.把用户重定向到另外的页面.为J ...

  3. collectionViewFlow的界面编写

    #import <UIKit/UIKit.h> //这边我们会创建一个scrollView的界面,这个scrollView里面有三张图片构成,我们使用下面的枚举方式来定义这三个位置 typ ...

  4. 【网络通信】服务器端Socket监听80端口,建立连接传输数据时也是使用的80端口么?

    1. 服务器端Socket监听80端口,建立连接传输数据时也是使用的80端口么? 答:对.建立连接时服务器会分配一个新的Socket,但是用的源端口号还是80端口.套接字是由协议类型.源IP.目的IP ...

  5. java取得整数部分 代码

    Pattern p1 = Pattern.compile("[0-9]*"); Matcher m1 = p1.matcher("100.0"); boolea ...

  6. Python 记录(一)

    一开始没发现3.5与2.x版本的区别,导致浪费了很多时间在导包等问题上: 如: Pyhton2中的urllib2工具包,在Python3中分拆成了urllib.request和urllib.error ...

  7. 怎样使用svn开发项目

    那么首先什么是svn呢?官方有很好的解释,我说一下个人简单的理解,svn就是开源的版本控制软件, 那么什么是版本呢?简单的说版本就是标记,比如你买了一本书,同样的书名,但是版本不一定一样, 因为里面可 ...

  8. Shell - 文件运算符

    文件运算符  文件运算符  描述 -b file  检测 file 是否为块设备文件 -c file  检测 file 是否为字符设备文件  -d file  检测 file 是否为目录 -e fil ...

  9. app包中的fragment和v4包中的fragment的使用的区别

    app包中的fragment和v4包中的fragment的使用的区别 1.尽量不要用app包中的fragment,因为这个是在3.0之后才有的,支持的版本太高,在低版本中是是用不了的 2.androi ...

  10. Android Listview with different layout for each row

    http://stackoverflow.com/questions/4777272/android-listview-with-different-layout-for-each-row 其关键在重 ...