Struts2+JFreeChart
生活随笔
收集整理的這篇文章主要介紹了
Struts2+JFreeChart
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
下面以邊帖圖片和代碼的方式來(lái)講解Struts2與JFreeChart的整合。
?????搭建環(huán)境:首先帖一張工程的目錄結(jié)構(gòu)以及所需的jar包。注意:如果你不打算自己寫ChartResult的話只需要引入struts2-jfreechart-plugin-2.0.6.jar(這個(gè)在struts-2.0.6-all.zip可以找到了):
?????????
???????1.依次帖web.xml、struts.xml、struts.properties和struts-jfreechart.xml幾個(gè)配置文件的代碼:
????????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">400</param>
???????????????????<param?name="height">300</param>
????????????</result>
????????</action>
????</package>
</struts> ????????說(shuō)明:這里只需要說(shuō)明下struts-jfreechart.xml,這里直接調(diào)用已經(jīng)寫好的類ChartResult,這個(gè)類是繼承自com.opensymphony.xwork2.Result,傳入生成圖片大小的參數(shù)width和height就可以了。
???????2.?新建JFreeChartAction繼承ActionSupport,生成JFreeChart對(duì)象并保存到chart中,注意這個(gè)名稱是固定的。
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調(diào)用->ActionInvocation.getStack().findValue("chart")
????private?JFreeChart?chart;
????
????@Override
????public?String?execute()?throws?Exception?{
????????//設(shè)置數(shù)據(jù)
????????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對(duì)象
????????chart?=?ChartFactory.createPieChart("Pie?Chart",?data,?true,true,?false);
????????
????????return?SUCCESS;
????}
????public?JFreeChart?getChart()?{
????????return?chart;
????}
????public?void?setChart(JFreeChart?chart)?{
????????this.chart?=?chart;
????}
}
????以上生成的圖片是PNG格式的圖片,如果需要自定義圖片格式的話(好像只能支持JPG和PNG格式),那么自己寫一個(gè)ChartResult繼承自StrutsResultSupport,見(jiàn)代碼: 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;
????}
}
????"-//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">400</param>
???????????????????<param?name="height">300</param>
????????????</result>
????????????-->
??????????????<result?type="chart">?
???????????????????<param?name="width">400</param>
???????????????????<param?name="height">300</param>
???????????????????<param?name="imageType">jpg</param>
????????????</result>
????????</action>
????</package>
</struts>
?????搭建環(huán)境:首先帖一張工程的目錄結(jié)構(gòu)以及所需的jar包。注意:如果你不打算自己寫ChartResult的話只需要引入struts2-jfreechart-plugin-2.0.6.jar(這個(gè)在struts-2.0.6-all.zip可以找到了):
?????????
???????1.依次帖web.xml、struts.xml、struts.properties和struts-jfreechart.xml幾個(gè)配置文件的代碼:
????????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">400</param>
???????????????????<param?name="height">300</param>
????????????</result>
????????</action>
????</package>
</struts> ????????說(shuō)明:這里只需要說(shuō)明下struts-jfreechart.xml,這里直接調(diào)用已經(jīng)寫好的類ChartResult,這個(gè)類是繼承自com.opensymphony.xwork2.Result,傳入生成圖片大小的參數(shù)width和height就可以了。
???????2.?新建JFreeChartAction繼承ActionSupport,生成JFreeChart對(duì)象并保存到chart中,注意這個(gè)名稱是固定的。
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調(diào)用->ActionInvocation.getStack().findValue("chart")
????private?JFreeChart?chart;
????
????@Override
????public?String?execute()?throws?Exception?{
????????//設(shè)置數(shù)據(jù)
????????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對(duì)象
????????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!至此代碼已經(jīng)全部貼完。
輸入訪問(wèn)?http://localhost:8080/Struts2JFreeChart/jfreechart/JFreeChartAction.action
顯示結(jié)果如下:
????以上生成的圖片是PNG格式的圖片,如果需要自定義圖片格式的話(好像只能支持JPG和PNG格式),那么自己寫一個(gè)ChartResult繼承自StrutsResultSupport,見(jiàn)代碼: 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">400</param>
???????????????????<param?name="height">300</param>
????????????</result>
????????????-->
??????????????<result?type="chart">?
???????????????????<param?name="width">400</param>
???????????????????<param?name="height">300</param>
???????????????????<param?name="imageType">jpg</param>
????????????</result>
????????</action>
????</package>
</struts>
本文轉(zhuǎn)自博客園農(nóng)民伯伯的博客,原文鏈接:Struts2+JFreeChart,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原博主。
總結(jié)
以上是生活随笔為你收集整理的Struts2+JFreeChart的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: uni-app微信小程序image引入图
- 下一篇: uni.request接口封装;小程序u