前言
關于struts2入門以及提高等在這裡就不介紹了,但是關于struts2的學習有以下推薦:
struts2-showcase-2.0.6.war:這個是官方自帶的demo(struts-2.0.6-all.zip\struts-2.0.6\apps目錄下),非常全面,直接部署就可以了(很多朋友struts2能學很好我估計還是直接從這裡學來的)。
關于jfreechart入門等這裡我也不打算介紹了,中文資料很多了。
正題
下面以邊帖圖檔和代碼的方式來講解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 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>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<!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"
<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-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 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;
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
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:
<!-- 自定義傳回類型 -->
<!--
-->
<result-type name="chart" class="com.tangjun.struts2.chartresult.chartresult"></result-type>
<!--
-->
<param name="imagetype">jpg</param>
ok!顯示的效果是一樣的,隻是圖檔格式不一樣,當然這裡面你可以做更多操作!
轉載:http://www.cnblogs.com/over140/archive/2007/11/25/971663.html