wangzhiyun_cu 发表于 2011-12-20 09:48

struts2初步-整合JFreeChart

<DIV>首先要加入以下3个jar包:</DIV>
<DIV>jfreechart-1.0.13.jar</DIV>
<DIV>jcommon-1.0.16.jar</DIV>
<DIV>struts2-jfreechart-plugin-2.2.1.1.jar</DIV>
<DIV>当然,版本号因人而异</DIV>
<DIV>&nbsp;</DIV>
<DIV>来个测试的class:</DIV>
<DIV>&nbsp;</DIV>
<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#999999 cellSpacing=0 cellPadding=0 width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN style="COLOR: #000000"><SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> JFreeChartUtil <SPAN style="COLOR: #0000cc">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> JFreeChart createChart<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN> <SPAN style="COLOR: #0000ff">throws</SPAN> <SPAN style="COLOR: #ff0000">IOException</SPAN> <SPAN style="COLOR: #0000cc">{</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #ff9900">// 数据集 <BR></SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DefaultPieDataset dpd <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #0000ff">new</SPAN> DefaultPieDataset<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dpd<SPAN style="COLOR: #0000cc">.</SPAN><SPAN style="COLOR: #ff0000">setValue</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff00ff">"管理人员"</SPAN> <SPAN style="COLOR: #0000cc">,</SPAN> 25 <SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dpd<SPAN style="COLOR: #0000cc">.</SPAN><SPAN style="COLOR: #ff0000">setValue</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff00ff">"市场人员"</SPAN> <SPAN style="COLOR: #0000cc">,</SPAN> 25 <SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dpd<SPAN style="COLOR: #0000cc">.</SPAN><SPAN style="COLOR: #ff0000">setValue</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff00ff">"开发人员"</SPAN> <SPAN style="COLOR: #0000cc">,</SPAN> 45 <SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dpd<SPAN style="COLOR: #0000cc">.</SPAN><SPAN style="COLOR: #ff0000">setValue</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff00ff">"其它人员"</SPAN> <SPAN style="COLOR: #0000cc">,</SPAN> 10 <SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #ff9900">// 创建PieChart对象 <BR></SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JFreeChart chart <SPAN style="COLOR: #0000cc">=</SPAN> ChartFactory<SPAN style="COLOR: #0000cc">.</SPAN>createPieChart3D<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff00ff">"某公司人员组织结构图"</SPAN> <SPAN style="COLOR: #0000cc">,</SPAN> dpd<SPAN style="COLOR: #0000cc">,</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;true <SPAN style="COLOR: #0000cc">,</SPAN> true <SPAN style="COLOR: #0000cc">,</SPAN> false <SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #ff0000">setFont</SPAN><SPAN style="COLOR: #0000cc">(</SPAN>chart<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">return</SPAN> chart<SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">}</SPAN> <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> <SPAN style="COLOR: #ff0000">setFont</SPAN><SPAN style="COLOR: #0000cc">(</SPAN>JFreeChart chart<SPAN style="COLOR: #0000cc">)</SPAN> <SPAN style="COLOR: #0000cc">{</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #ff0000">Font</SPAN> <SPAN style="COLOR: #ff0000">font</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #0000ff">new</SPAN> <SPAN style="COLOR: #ff0000">Font</SPAN><SPAN style="COLOR: #0000cc">(</SPAN> <SPAN style="COLOR: #ff00ff">"宋体"</SPAN> <SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff0000">Font</SPAN><SPAN style="COLOR: #0000cc">.</SPAN>ITALIC<SPAN style="COLOR: #0000cc">,</SPAN> 12 <SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PiePlot plot <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #0000cc">(</SPAN>PiePlot<SPAN style="COLOR: #0000cc">)</SPAN> chart<SPAN style="COLOR: #0000cc">.</SPAN>getPlot<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart<SPAN style="COLOR: #0000cc">.</SPAN><SPAN style="COLOR: #ff0000">getTitle</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">.</SPAN><SPAN style="COLOR: #ff0000">setFont</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff0000">font</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;plot<SPAN style="COLOR: #0000cc">.</SPAN>setLabelFont<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff0000">font</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart<SPAN style="COLOR: #0000cc">.</SPAN>getLegend<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">.</SPAN>setItemFont<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff0000">font</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">}</SPAN><BR><BR><SPAN style="COLOR: #0000cc">}</SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE>
<P><BR>再来个action</P>
<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#999999 cellSpacing=0 cellPadding=0 width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN style="COLOR: #000000"><SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> JFreeChartAction <SPAN style="COLOR: #0000ff">extends</SPAN> ActionSupport <SPAN style="COLOR: #0000cc">{</SPAN><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #ff9900">/**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * <BR>&nbsp;&nbsp;&nbsp;&nbsp; */</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">final</SPAN> <SPAN style="COLOR: #0000ff">long</SPAN> serialVersionUID <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #0000cc">-</SPAN>1163236831212708227L<SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #ff9900">/** <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* 定义JFreeChart对象 大家请注意在这里JFreeChart对象名只能为chart <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* 不能是别的 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* 关于这点 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* 大家可以上struts2网站上面查看一下 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* http://struts.apache.org/2.x/docs/jfreechart-plugin.html <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">private</SPAN> JFreeChart chart<SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;@<SPAN style="COLOR: #ff0000">Override</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #ff0000">String</SPAN> <SPAN style="COLOR: #ff0000">execute</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN> <SPAN style="COLOR: #0000ff">throws</SPAN> <SPAN style="COLOR: #ff0000">Exception</SPAN> <SPAN style="COLOR: #0000cc">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart <SPAN style="COLOR: #0000cc">=</SPAN> JFreeChartUtil<SPAN style="COLOR: #0000cc">.</SPAN>createChart<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">return</SPAN> SUCCESS<SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">}</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">public</SPAN> JFreeChart getChart<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN> <SPAN style="COLOR: #0000cc">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">return</SPAN> chart<SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">}</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> setChart<SPAN style="COLOR: #0000cc">(</SPAN>JFreeChart chart<SPAN style="COLOR: #0000cc">)</SPAN> <SPAN style="COLOR: #0000cc">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #0000cc">.</SPAN>chart <SPAN style="COLOR: #0000cc">=</SPAN> chart<SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">}</SPAN>&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR><SPAN style="COLOR: #0000cc">}</SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE>
<P><BR>配置struts.xml</P>
<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#999999 cellSpacing=0 cellPadding=0 width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">&lt;</SPAN>!-- <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关于extends继承jfreechart-default这点请大家注意 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;因为在 struts-default这个包里并没有result-type为chart的 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart 定义在前面我们导入的struts2-jfreechart-plugin-2<SPAN style="COLOR: #0000cc">.</SPAN>2<SPAN style="COLOR: #0000cc">.</SPAN>1<SPAN style="COLOR: #0000cc">.</SPAN>1<SPAN style="COLOR: #0000cc">.</SPAN>jar<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;下面的struts-plugin<SPAN style="COLOR: #0000cc">.</SPAN>xml文件中 <BR>&nbsp;&nbsp;&nbsp;&nbsp;--<SPAN style="COLOR: #0000cc">&gt;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">&lt;</SPAN>package name <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff00ff">"JFreeChart"</SPAN> extends <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff00ff">"jfreechart-default"</SPAN> <SPAN style="COLOR: #0000cc">&gt;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">&lt;</SPAN>action name <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff00ff">"JFreeChart"</SPAN> class <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff00ff">"com.****.JFreeChartAction"</SPAN> <SPAN style="COLOR: #0000cc">&gt;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">&lt;</SPAN>result type <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff00ff">"chart"</SPAN> <SPAN style="COLOR: #0000cc">&gt;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">&lt;</SPAN>param name <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff00ff">"width"</SPAN> <SPAN style="COLOR: #0000cc">&gt;</SPAN>600<SPAN style="COLOR: #0000cc">&lt;</SPAN><SPAN style="COLOR: #0000cc">/</SPAN>param<SPAN style="COLOR: #0000cc">&gt;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">&lt;</SPAN>param name <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff00ff">"height"</SPAN> <SPAN style="COLOR: #0000cc">&gt;</SPAN>400<SPAN style="COLOR: #0000cc">&lt;</SPAN><SPAN style="COLOR: #0000cc">/</SPAN>param<SPAN style="COLOR: #0000cc">&gt;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">&lt;</SPAN><SPAN style="COLOR: #0000cc">/</SPAN>result<SPAN style="COLOR: #0000cc">&gt;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">&lt;</SPAN><SPAN style="COLOR: #0000cc">/</SPAN>action <SPAN style="COLOR: #0000cc">&gt;</SPAN> <BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">&lt;</SPAN><SPAN style="COLOR: #0000cc">/</SPAN>package <SPAN style="COLOR: #0000cc">&gt;</SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE>
<P><BR>&nbsp;</P>
<P>启动tomcat,输入网址测试一下:</P>
<P><A href="http://localhost:8080/Struts2Demo/JFreeChart.action">http://localhost:8080/Struts2Demo/JFreeChart.action</A></P>
<P>结果如图:</P>
<DIV align=center><IMG src="http://blogimg.chinaunix.net/blog/upfile2/110413100757.png" onload="javascript:if(this.width>500)this.width=500;" border=0></DIV>
页: [1]
查看完整版本: struts2初步-整合JFreeChart