flex4出來一段時(shí)間了,去年看了一段時(shí)間flex3,后來由于暫時(shí)沒有項(xiàng)目需求.所以flex遺忘了很多.這次使用flex4+myeclipse8.5錄制了一個(gè)視頻.以免時(shí)間長了遺忘.
軟件環(huán)境:windows7+flex4+myeclipse8.5+blazeds
功能描述:分別用代碼實(shí)現(xiàn)了三種flex4與Java通信
三種方式:
1 flex 與普通java類通信RemoteObject
2 flex 與服務(wù)器交互HTTPService
3 flex與webservice交互WebService
1 flex 與普通java類通信RemoteObject
普通Java類:
package com.flex.demo;


/** *//**
* 功能描述:該類用來實(shí)現(xiàn)flex與普通java類中的方法通信
* @author Administrator
*
*/

public class SimpleService
{


public String sayHello(String name)
{
return "Hello, "+name;
}
}

配置說明:配置remoting-config.xml
<destination id="myservice">
<properties>
<source>com.flex.demo.SimpleService</source>
</properties>
</destination>
調(diào)用
<!--flex 與普通java類通信-->
<s:RemoteObject id="serv" destination="myservice" fault="serv_faultHandler(event)" result="serv_resultHandler(event)">
</s:RemoteObject>
2flex 與服務(wù)器交互HTTPService
servlet代碼
package com.flex.demo;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/** *//**
* 功能描述:該servlet用來與flex進(jìn)行交互
* @author Administrator
*
*/
@SuppressWarnings("serial")

public class SimpleServiceServlet extends HttpServlet
{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException
{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.getWriter().write("我是服務(wù)器");
}
}

調(diào)用
<!-- flex 與服務(wù)器交互-->
<s:HTTPService id="service" fault="service_faultHandler(event)" result="service_resultHandler(event)" url="http://localhost:8080/flexdemo/simpleServiceServlet">
</s:HTTPService>
3flex與webservice交互WebService
<!--flex與webservice交互這里調(diào)用一個(gè)天氣預(yù)報(bào)的webservice-->
<s:WebService id="ws"
wsdl="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"
fault="ws_faultHandler(event)"
result="ws_resultHandler(event)"
showBusyCursor="true">
<!-- 第二種調(diào)用webservice的方法<s:operation>-->
<!-- <s:operation name="getWeatherbyCityName"> webservice中的方法名-->
<s:operation name="getWeatherbyCityName">
<!--傳遞的參數(shù)-->
<s:request>
<!--參數(shù)名稱必須與webservice中定義的參數(shù)名一致否則調(diào)用不成功報(bào)錯(cuò)-->
<theCityName>
{city.text}
</theCityName>
</s:request>
</s:operation>
</s:WebService>
三種方式完整的配置例子
<?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"
creationComplete="service.send()"
>
<fx:Script>
<
//錯(cuò)誤處理函數(shù)
protected function serv_faultHandler(event:FaultEvent):void
{
Alert.show("調(diào)用失敗了:"+event.fault.message as String,"提示");
}

//成功調(diào)用函數(shù)
protected function serv_resultHandler(event:ResultEvent):void
{
Alert.show("調(diào)用成功了:"+event.result as String,"提示");
}


protected function service_faultHandler(event:FaultEvent):void
{
Alert.show("調(diào)用失敗了:"+event.fault.message as String,"提示");
}


protected function service_resultHandler(event:ResultEvent):void
{
Alert.show("調(diào)用成功了:"+event.result as String,"提示");
}


protected function ws_faultHandler(event:FaultEvent):void
{
Alert.show("調(diào)用失敗了:"+event.fault.message as String,"提示");
}
//調(diào)用成功了
protected function ws_resultHandler(event:ResultEvent):void
{
trace(event.result);
lbl.text=(String)(event.result);
}

//第一種調(diào)用webservice的方法
//此方法調(diào)用webservice
protected function btn_clickHandler(event:MouseEvent):void
{
//ws.getWeatherbyCityName(city.text);
//第二中調(diào)用webservice的方法
ws.getWeatherbyCityName.send();
}

]]>
</fx:Script>
<fx:Declarations>
<!-- 將非可視元素(例如服務(wù)、值對象)放在此處 -->
<!--flex 與普通java類通信-->
<s:RemoteObject id="serv" destination="myservice" fault="serv_faultHandler(event)" result="serv_resultHandler(event)">
</s:RemoteObject>
<!-- flex 與服務(wù)器交互-->
<s:HTTPService id="service" fault="service_faultHandler(event)" result="service_resultHandler(event)" url="http://localhost:8080/flexdemo/simpleServiceServlet">
</s:HTTPService>
<!--flex與webservice交互這里調(diào)用一個(gè)天氣預(yù)報(bào)的webservice-->
<s:WebService id="ws"
wsdl="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"
fault="ws_faultHandler(event)"
result="ws_resultHandler(event)"
showBusyCursor="true">
<!-- 第二種調(diào)用webservice的方法<s:operation>-->
<!-- <s:operation name="getWeatherbyCityName"> webservice中的方法名-->
<s:operation name="getWeatherbyCityName">
<!--傳遞的參數(shù)-->
<s:request>
<!--參數(shù)名稱必須與webservice中定義的參數(shù)名一致否則調(diào)用不成功報(bào)錯(cuò)-->
<theCityName>
{city.text}
</theCityName>
</s:request>
</s:operation>
</s:WebService>
</fx:Declarations>
<s:TextInput x="488" y="72" id="city"/>
<s:Button x="633" y="72" label="查看" id="btn" click="btn_clickHandler(event)"/>
<s:Label x="224" y="128" id="lbl" width="820" height="376"/>
</s:Application>

初次接觸flex的朋友,看上面的肯定很迷惑,因此這里錄制了視頻教程,視頻的質(zhì)量是蠻好的,該視頻從頭至尾演示了flex4整合Java的過程.為了方便在windows平臺下播放,該視頻默認(rèn)導(dǎo)出格式為exe,在此說明該文件并非病毒,實(shí)乃視頻文件,大家可放心觀看.
點(diǎn)我下載flex4與Java交互演示視頻
視頻截圖:
點(diǎn)我下載完整的項(xiàng)目文件