Java與Flex學習筆記(21)—-Flex與Java通訊方式之WebService

來源:互聯網
上載者:User

       今天終於把Flex與Java通訊的第三種方式給整出來了。之前之所以沒有弄是因為本人不知道怎麼建立WebService服務。而網上找到的都是用的免費的天氣預報服務。好吧,大家學習這種通訊式只是為了做天氣預報吧,只是我可不敢苟同。好在今天終於弄出來了。


      前面我們知道Flex與Java有3種通訊方式:


      1.      Flex通過RemoteObject方式與普通Java類通訊。


       2.      Flex通過HttpService方式與伺服器類通訊,如servlet。


       3.      Flex通過WebService與Java通訊.


       WebService的知識我就不說了,但是Flex通過WebService與Java通訊方式卻是很簡單的。


      下面先建立一個WebService地址。


        在Eclipse裡建立一個web工程webserviceDemo,在裡面建立一個類LoginService.java,代碼如下所示:


package com.ldfsoft.service; public classLoginService {    public boolean login(Stringusername,String passworld){       boolean result=false;       if("admin".equals(username)&&"123".equals(passworld)){           result=true;       }       return result;    }}

 

        這個類裡有個方法login(),很簡單。


        接著對著這個類右鍵,選擇WebServiceà  --->  Creata Web Service,如所示:




         選擇預設的配置點擊“Finish”即可,eclipse會自動幫我們建立WebService地址。


        下面我們測試我們剛才建立的WebService服務。在地址欄輸入http://localhost:8000/webserviceDemo/services/LoginService?wsdl,如果頁面如下所示,則說明配置成功:




       接著在Flex中用WebService通訊。


       建立一個flex頁面webserviceDemo.mxml,代碼如下所示:


<?xml version="1.0"encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"              xmlns:s="library://ns.adobe.com/flex/spark"              xmlns:mx="library://ns.adobe.com/flex/mx"minWidth="955"minHeight="600"pageTitle="WebService測試">    <fx:Style>       @namespace s "library://ns.adobe.com/flex/spark";       @namespace mx "library://ns.adobe.com/flex/mx";        .txt{           fontSize:18;       }    </fx:Style>       <fx:Script>       <![CDATA[           import mx.controls.Alert;           importmx.rpc.events.FaultEvent;           importmx.rpc.events.ResultEvent;           protected function operation1_resultHandler(event:ResultEvent):void           {              // TODO Auto-generated method stub              varresult:Boolean=event.result as Boolean;              if(result){              Alert.show("登陸成功~","提示");              }else{                  Alert.show("登陸失敗~","提示");              }           }           protected functionoperation1_faultHandler(event:FaultEvent):void           {              // TODO Auto-generated method stub              Alert.show(event.fault.message,"提示");           }                     protected functionlogin_clickHandler(event:MouseEvent):void           {              // TODO Auto-generated method stub              //發送請求              webSer.login.send();           }                 ]]>    </fx:Script>    <fx:Declarations>       <!--注意WebService中的參數個數與順序要與發布地址中的參數一樣-->       <s:WebService id="webSer" wsdl="http://localhost:8000/webserviceDemo/services/LoginService?wsdl">           <s:operation name="login" result="operation1_resultHandler(event)"fault="operation1_faultHandler(event)">              <s:request>                  <username>{username.text}</username>                  <passworld>{password.text}</passworld>              </s:request>           </s:operation>       </s:WebService>    </fx:Declarations>    <s:Label x="351" y="158" text="賬戶:" fontSize="18"  styleName="txt"/>    <s:TextInput x="439" y="154" id="username" styleName="txt"/>    <s:Label x="351" y="201" text="密碼:" fontSize="18" id="password_txt" styleName="txt"/>    <s:TextInput x="441" y="196" id="password" styleName="txt"displayAsPassword="true"/>    <s:Button x="461" y="273" height="34" label="登陸" fontSize="18"  id="login" styleName="txt"click="login_clickHandler(event)"/>   </s:Application>

          接著運行這個頁面,如果輸入的是使用者名稱為“admin”與密碼為“123”登陸成功反之則失敗的話,則表明通訊成功。




       注意,如果你瞭解WebService的話你就會明白他們是通過將資料群組裝成xml傳送資料的。本例子中建立WebService服務在webserviceDemo工程中,而Flex頁面則是在另一個工程中,由此可以知道:WebService可以是不同工程間通訊的橋樑!


       原創文章,轉載請註明出處:http://www.dianfusoft.com/




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.