struts2整合jfreechart

我用的是struts2.1.6,和jfreechart1.0.12

大家可以去

http://struts.apache.org/download.cgi#struts216 (下 载struts2相关jar文件)

 

http://sourceforge.net/projects/jfreechart/files/ (下 载jfreechart和jcommon和相关jar文件)

 

好了,当你都下载完了之后,里面的jar不是必须的,我们只需要导入一部分即可

 

  • struts2-core-2.1.6.jar
  • freemarker-2.3.13.jar
  • xwork-2.1.2.jar
  • ognl-2.6.11.jar
  • commons-fileupload-1.2.1.jar
  • commons-logging-1.0.4.jar
  • struts2-jfreechart-plugin-2.1.6.jar
  • jcommon-1.0.15.jar
  • jfreechart-1.0.12.jar

前6个是struts2所需要的jar文件,如果大家用的不是struts2.1.6的话,commons-fileupload- 1.2.1.jar可以不用导入

 

我们要实现的效果就是,在一个页面上面用一个img标签显示jfreechart生成的图片

1.jsp页面代码非常简单

Html代码
  1. < body >   
  2.     < img   alt = "jfreechart"   src = "jfreechart.action" />   
  3. </ body >   

 就是一个img标签,src连接一个action

2.我们就写jfreechart代码,在这里我也是写一个非常简单的例子,就画一个饼图

Java代码
  1. public   class  JfreeChartTest {  
  2.   
  3.     public   static  JFreeChart createChart()  throws  IOException {  
  4.         // 数据集   
  5.         DefaultPieDataset dpd = new  DefaultPieDataset();  
  6.         dpd.setValue("管理人员" 25 );  
  7.         dpd.setValue("市场人员" 25 );  
  8.         dpd.setValue("开发人员" 45 );  
  9.         dpd.setValue("其它人员" 10 );  
  10.         // 创建PieChart对象   
  11.         JFreeChart chart = ChartFactory.createPieChart3D("某公司人员组织结构图" , dpd,  
  12.                 true true false );  
  13.         utils.setFont(chart);  
  14.         return  chart;  
  15.     }  
  16. }  
  17.   
  18. /**  
  19.  * 设 置字体  
  20.  *   
  21.  * @author zyong  
  22.  *   
  23.  */   
  24. class  utils {  
  25.     public   static   void  setFont(JFreeChart chart) {  
  26.         Font font = new  Font( "宋体" , Font.ITALIC,  12 );  
  27.         PiePlot plot = (PiePlot) chart.getPlot();  
  28.         chart.getTitle().setFont(font);  
  29.         plot.setLabelFont(font);  
  30.         chart.getLegend().setItemFont(font);  
  31.     }  
  32. }  

 

 

3.编写action

Java代码
  1. @SuppressWarnings ( "serial" )  
  2. public   class  JfreeCharAction  extends  ActionSupport {  
  3.   
  4.     /**  
  5.      * 定 义JFreeChart对象 大家请注意在这里JFreeChart对象名只能为chart   
  6.      * 不能是别的   
  7.      * 关于这点  
  8.      * 大家可以上struts2网站上面查看一下  
  9.      *   
  10.      * http://struts.apache.org/2.x/docs/jfreechart-plugin.html  
  11.      */   
  12.     private  JFreeChart chart;  
  13.   
  14.     public  JFreeChart getChart() {  
  15.         return  chart;  
  16.     }  
  17.   
  18.     public   void  setChart(JFreeChart chart) {  
  19.         this .chart = chart;  
  20.     }  
  21.   
  22.     @Override   
  23.     public  String execute()  throws  Exception {  
  24.         // 调用方法   
  25.         this .chart = JfreeChartTest.createChart();  
  26.         return  SUCCESS;  
  27.     }  
  28. }  

 

4.编写struts.xml

Xml代码
  1. < struts >   
  2.     <!--   
  3.         关 于extends继承jfreechart-default这点请大家注意  
  4.         因为在 struts-default这个包里并没有result-type为chart的  
  5.         chart 定 义在前面我们导入的struts2-jfreechart-plugin-2.1.6.jar  
  6.         下 面的struts-plugin.xml文件中  
  7.     -->   
  8.     < package   name = "jfreechart"   extends = "jfreechart-default" >   
  9.         < action   name = "jfreechart"   class = "jfreechartAction" >   
  10.             < result   name = "success"   type = "chart" >   
  11.                 < param   name = "width" > 600 </ param >   
  12.                 < param   name = "height" > 400 </ param >   
  13.             </ result >   
  14.         </ action >   
  15.     </ package >   
  16. </ struts >   

 5.大家把上面的工作都做好了之后,就可以开始服务器,运行一下就可以看见自己的成果

 

原文地址:http://wind-sing.javaeye.com/blog/443980

你可能感兴趣的:(exception,struts,jfreechart,jar,Class,plot)