java+struct+2.0_Struts2+JFreeChart

edcb627dc65241e04c8f4c6c137fbe7e.png

补充

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

packagecom.tangjun.struts2.chartresult;importjava.io.OutputStream;importjavax.servlet.http.HttpServletResponse;importorg.apache.struts2.ServletActionContext;importorg.apache.struts2.dispatcher.StrutsResultSupport;importorg.jfree.chart.ChartUtilities;importorg.jfree.chart.JFreeChart;importcom.opensymphony.xwork2.ActionInvocation;publicclassChartResultextendsStrutsResultSupport {/****/privatestaticfinallongserialVersionUID=4199494785336139337L;//图片宽度privateintwidth;//图片高度privateintheight;//图片类型 jpg,pngprivateString p_w_picpathType;

@OverrideprotectedvoiddoExecute(String arg0, ActionInvocation invocation)throwsException {

JFreeChart chart=(JFreeChart) invocation.getStack().findValue("chart");

HttpServletResponse response=ServletActionContext.getResponse();

OutputStream os=response.getOutputStream();if("jpeg".equalsIgnoreCase(p_w_picpathType)||"jpg".equalsIgnoreCase(p_w_picpathType))

ChartUtilities.writeChartAsJPEG(os, chart, width, height);elseif("png".equalsIgnoreCase(p_w_picpathType))

ChartUtilities.writeChartAsPNG(os, chart, width, height);elseChartUtilities.writeChartAsJPEG(os, chart, width, height);

os.flush();

}publicvoidsetHeight(intheight) {this.height=height;

}publicvoidsetWidth(intwidth) {this.width=width;

}publicvoidsetImageType(String p_w_picpathType) {this.p_w_picpathType=p_w_picpathType;

}

}

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

struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">400300jpg

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

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

struts2-showcase-2.0.6.war:这个是官方自带的Demo(struts-2.0.6-all.zip\struts-2.0.6\apps目录下),非常全面,直接部署就可以了(很多朋友Struts2能学很好我估计还是直接从这里学来的)。

wiki-WebWork:入了门的朋友应该都知道,strust2由webwork2和struts1.x合并起来的,但主要还是以webwork2为主,所以如果找不到Struts2的资料可以找WebWork资料看看。

Max On Java的博客,他的博客的资料在中文的Struts2算是比较全的了,写得很详细。

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

正题

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

2007-11-25_1.JPG

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

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

<?xml  version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">struts2org.apache.struts2.dispatcher.FilterDispatcherstruts2/*

struts.xml

<?xml  version="1.0" encoding="UTF-8"?>struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

struts.properties

struts.ui.theme=simple

struts-jfreechart.xml

struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">400300

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

2.

packagecom.tangjun.struts2;importcom.opensymphony.xwork2.ActionSupport;importorg.jfree.chart.ChartFactory;importorg.jfree.chart.JFreeChart;importorg.jfree.data.general.DefaultPieDataset;publicclassJFreeChartActionextendsActionSupport {/****/privatestaticfinallongserialVersionUID=5752180822913527064L;//供ChartResult调用->ActionInvocation.getStack().findValue("chart")privateJFreeChart chart;

@OverridepublicString execute()throwsException {//设置数据DefaultPieDataset data=newDefaultPieDataset();

data.setValue("Java",newDouble(43.2));

data.setValue("Visual Basic",newDouble(1.0));

data.setValue("C/C++",newDouble(17.5));

data.setValue("tangjun",newDouble(60.0));//生成JFreeChart对象chart=ChartFactory.createPieChart("Pie Chart", data,true,true,false);returnSUCCESS;

}publicJFreeChart getChart() {returnchart;

}publicvoidsetChart(JFreeChart chart) {this.chart=chart;

}

}

OK!至此代码已经全部贴完。

输入访问 http://localhost:8080/Struts2JFreeChart/jfreechart/JFreeChartAction.action

显示结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值