制作网站需要什么成本,网站建设模板,苏州保洁公司钟点工,百度关键词排名点下面以边帖图片和代码的方式来讲解Struts2与JFreeChart的整合。搭建环境#xff1a;首先帖一张工程的目录结构以及所需的jar包。注意:如果你不打算自己写ChartResult的话只需要引入struts2-jfreechart-plugin-2.0.6.jar(这个在struts-2.0.6-all.zip可以找到了): … 下面以边帖图片和代码的方式来讲解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 version1.0 encodingUTF-8?web-app version2.4 xmlnshttp://java.sun.com/xml/ns/j2ee xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd filter filter-namestruts2/filter-name filter-class org.apache.struts2.dispatcher.FilterDispatcher /filter-class /filter filter-mapping filter-namestruts2/filter-name url-pattern/*/url-pattern /filter-mapping/web-app struts.xml ?xml version1.0 encodingUTF-8?!DOCTYPE struts PUBLIC -//Apache Software Foundation//DTD Struts Configuration 2.0//EN http://struts.apache.org/dtds/struts-2.0.dtdstruts include filestruts-jfreechart.xml //struts struts.properties struts.ui.themesimple struts-jfreechart.xml !DOCTYPE struts PUBLIC -//Apache Software Foundation//DTD Struts Configuration 2.0//EN http://struts.apache.org/dtds/struts-2.0.dtdstruts package namejFreeChartDemonstration extendsstruts-default namespace/jfreechart result-types result-type namechart classorg.apache.struts2.dispatcher.ChartResult/result-type /result-types action nameJFreeChartAction classcom.tangjun.struts2.JFreeChartAction result typechart param namewidth400/param param nameheight300/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.dtdstruts package namejFreeChartDemonstration extendsstruts-default namespace/jfreechart !-- 自定义返回类型 -- result-types !-- result-type namechart classorg.apache.struts2.dispatcher.ChartResult/result-type -- result-type namechart classcom.tangjun.struts2.chartresult.ChartResult/result-type /result-types action nameJFreeChartAction classcom.tangjun.struts2.JFreeChartAction !-- result typechart param namewidth400/param param nameheight300/param /result -- result typechart param namewidth400/param param nameheight300/param param nameimageTypejpg/param /result /action /package/struts 本文转自博客园农民伯伯的博客原文链接Struts2JFreeChart如需转载请自行联系原博主。