JAVA—咖啡館

          ——歡迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術,交流工作經驗,分享JAVA帶來的快樂!本網站部分轉載文章,如果有版權問題請與我聯系。

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks


          前言

               關于Struts2入門以及提高等在這里就不介紹了,但是關于Struts2的學習有以下推薦:

            1. struts2-showcase-2.0.6.war:這個是官方自帶的Demo(struts-2.0.6-all.zip\struts-2.0.6\apps目錄下),非常全面,直接部署就可以了(很多朋友Struts2能學很好我估計還是直接從這里學來的)。
            2. wiki-WebWork:入了門的朋友應該都知道,strust2由webwork2和struts1.x合并起來的,但主要還是以webwork2為主,所以如果找不到Struts2的資料可以找WebWork資料看看。
            3. Max On Java的博客,他的博客的資料在中文的Struts2算是比較全的了,寫得很詳細。
            4. The Code ProjectGoogle - CodeSearchKoders:這幾個代碼搜索網站在我找不到中文資料甚至英文文章的時候幫了我大忙!

               關于JFreeChart入門等這里我也不打算介紹了,中文資料很多了。


           

           

          正題
               下面以邊帖圖片和代碼的方式來講解Struts2JFreeChart的整合。
               搭建環境:首先帖一張工程的目錄結構以及所需的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>
                  
          <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>
                  說明:這里只需要說明下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,truefalse);
                  
                  
          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.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>

          OK!顯示的效果是一樣的,只是圖片格式不一樣,當然這里面你可以做更多操作!

          posted on 2010-01-06 17:08 rogerfan 閱讀(2093) 評論(0)  編輯  收藏 所屬分類: 【開源技術】
          主站蜘蛛池模板: 神木县| 元阳县| 禄劝| 漳州市| 鱼台县| 栾川县| 昌吉市| 平阴县| 门源| 莱西市| 苍山县| 桂阳县| 夏河县| 门头沟区| 敦化市| 奉化市| 临江市| 长治县| 开封市| 双峰县| 佳木斯市| 宜春市| 敖汉旗| 商南县| 玉林市| 桂林市| 新化县| 额济纳旗| 南和县| 宁阳县| 英超| 长乐市| 镇原县| 牡丹江市| 云龙县| 离岛区| 齐河县| 四川省| 永平县| 专栏| 吴桥县|