Java軟件報表軟件技術博客

          java報表軟件技術匯總 java報表軟件制作 報表軟件新聞
          posts - 355, comments - 100, trackbacks - 0, articles - 3
             :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

          在使用WebService作為項目的數據源時,希望報表中也是直接調用這個WebService數據源,而不是定義數據連接調用對應的數據庫表,這樣要怎么實現呢?

          在程序中訪問WebService應用服務,將WebService返回的數據轉為程序數據集,然后在設計器中調用。

          axis1.4上的WebService為例,介紹其取數方式以及將數據轉換為WebService的程序數據集的過程。

          WebService應用服務

          如在Tomcat下的Axis工程中以jwa方式發布了一個WebService應用服務下TestWS2TDClient.jws,返回一個數組數據,如下:

          public class TestWS2TDClient {

          public String[][] getTD() {

          String[][] a = {{"城市", "銷售員", "銷售額"},{"江蘇", "Anna", "230"},{"江蘇", "Alex", "190"},{"江蘇","Jack","320"},{"江蘇","Apple","210"},{"浙江","Faye","150"},{"浙江","Sammi","280"}};

          return a;

          }

          }

          jws方式發布WebService:將寫好的TestWS2TDClient.java文件重命名TestWS2TDClient.jws,放在Tomcat\webapps\axis即可。

          java類中訪問WebService數據源

          Java中發布一個soap請求,訪問TestWS2TDClient.java,得到返回的數據,代碼如下:

          try {
              String endpoint 
          = "http://localhost:8080/axis/TestWS2TDClient.jws";
              Service service 
          = new Service(); //創建一個服務(service)調用(call)
              Call call = (Call) service.createCall(); // 通過service創建call對象
              call.setTargetEndpointAddress(new java.net.URL(endpoint)); // 設置service所在URL
              call.setOperationName(new QName("http://localhost:8080/axis/TestWS2TDClient.jws","getTD"));  // 調用service中的getTD方法
              String[][] ret = (String[][])call.invoke(new Object[] {}); // getTD方法沒有參數,因此傳一個空的對象,得到service中getTD返回的數據
              return ret;
          }
           catch (Exception e) {
              e.printStackTrace();
          }

          Soap即簡單對象訪問協議,客戶端發送一個請求,調用相應的對象,然后服務器返回結果。這些消息是XML格式的,并且封裝成符合HTTP協議的消息。

          在此期間需要引入axis.jar、commons-discovery-0.2.jar、commons-logging-1.0.4.jar、wadl4j-a.5.a.jar、log4j-1.2.8.jar、jaxrpc.jar六個包,可以在zxis\WEB-INF\lib下找到。

          將獲得數據轉為程序數據集

          訪問WebService后,該服務會返回數據給客戶端,該例中返回一個字符串數組。定義WebServiceTableData.java類,擴展AbstractTableData,將獲得的數組數據轉為程序數據集。完整代碼如下:

          package com.fr.data;

          import javax.xml.namespace.QName;
          import org.apache.axis.client.Call;
          import org.apache.axis.client.Service;
          import com.fr.data.AbstractTableData;
          import com.fr.general.data.TableDataException;

          public class WebServiceTableData extends AbstractTableData{
              
          private String[][] data;
              
              
          public WebServiceTableData() {
                  
          this.data = this.getData();
              }


              
          //獲取列數
              public int getColumnCount() throws TableDataException {
                  
          return data[0].length;
              }


              
          //獲取列的名稱為數組中第一行的值
              public String getColumnName(int columnIndex) throws TableDataException {
                  
          return data[0][columnIndex];
              }


              
          //獲取行數為數據的長度-1
              public int getRowCount() throws TableDataException {
                  
          return data.length - 1;
              }


              
          //獲取值
              public Object getValueAt(int rowIndex, int columnIndex) {
                  
          return data[rowIndex + 1][columnIndex];
              }


              
          public String[][] getData() {
                  
          try {
                      String endpoint 
          = "http://localhost:8080/axis/TestWS2TDClient.jws";
                      Service service 
          = new Service();
                      Call call 
          = (Call) service.createCall();
                      call.setTargetEndpointAddress(
          new java.net.URL(endpoint));
                      call.setOperationName(
          new QName("http://localhost:8080/axis/TestWS2TDClient.jws",
                              
          "getTD"));
                      String[][] ret 
          = (String[][])call.invoke(new Object[] {});
                      
          return ret;
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

                  
          return new String[][] {};
              }

          }

          將編譯好的class文件拷貝到%FR_HOME%/WebReport/WEB-INF/classes/com/fr/data文件夾下。

          設計器中調用程序數據集

          新建報表,定義數據集,選擇程序數據集,選擇定義好的程序數據集WebServiceTableData.class,自定義數據集的名稱為ds1,啟動tomcat服務器,點擊預覽,效果如下:




          主站蜘蛛池模板: 丹凤县| 比如县| 凤阳县| 行唐县| 会宁县| 琼结县| 虎林市| 竹山县| 两当县| 吴桥县| 威海市| 驻马店市| 临西县| 瓦房店市| 泉州市| 涡阳县| 德化县| 石家庄市| 新丰县| 莱芜市| 建昌县| 沁源县| 个旧市| 漳平市| 白城市| 海晏县| 河池市| 巢湖市| 普兰县| 鄢陵县| 鄯善县| 息烽县| 阜阳市| 衡山县| 尉犁县| 兴隆县| 靖西县| 米易县| 武威市| 凉城县| 玉田县|