今天做了個關于flex與動態語言通信的小例子。flex做web的前臺的確效果很cool,對web程序員來說,首先要掌握flex與jsp,php等等動態語言之間的通信細節。其實也很簡單,搞了個例子,有興趣的朋友可以看一下。
因為我的機器上只裝了php的開發環境,所以以php為例子來說明。
以下先介紹第一種通信方式:HTTPService
1.建立HttpDemo.mxml,
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FD1D06, #EAF807]" height="328" width="428">
<mx:HTTPService
showBusyCursor="true"
id="loginSrv"
result="doResult();"
method="GET"
url="http://localhost/test.php">
<mx:request>
<username>
{txtname.text}
</username>
<userpassword>
{txtpwd.text}
</userpassword>
</mx:request>
</mx:HTTPService>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
internal function doResult():void
{
var returnValue:String=loginSrv.lastResult.Result.msg;
if(returnValue=="success")
{
this.currentState="login";
}
else
{
Alert.show("您的登錄失敗了","提示信息",Alert.OK,this,null,null,Alert.YES);
}
}
]]>
</mx:Script>
<mx:states >
<mx:State id="s1" name="login">
<mx:RemoveChild target="{btnSubmit}"/>
<mx:RemoveChild target="{txtname}"/>
<mx:RemoveChild target="{txtpwd}"/>
<mx:RemoveChild target="{txtpwd}"/>
<mx:RemoveChild target="{lbname}"/>
<mx:RemoveChild target="{lbpwd}"/>
<mx:AddChild relativeTo="{loginPanel}" position="lastChild">
<mx:target>
<mx:Label text="你已經成功登陸!" x="64" y="33" fontSize="16" textAlign="center" fontStyle="normal" fontWeight="bold" textDecoration="normal" color="#1031AB"/>
</mx:target>
</mx:AddChild>
<mx:SetProperty target="{loginPanel}" name="title" value="登陸成功"/>
<mx:AddChild relativeTo="{loginPanel}" position="lastChild">
<mx:Button x="95.5" y="83" label="退出登陸" click="currentState=''"/>
</mx:AddChild>
</mx:State>
</mx:states>
<mx:Panel id="loginPanel" x="69.5" y="57" width="289" height="200" layout="absolute" title="登陸" fontSize="12">
<mx:Button x="110" y="108" label="提交" id="btnSubmit" click="loginSrv.send();"/>
<mx:TextInput x="79" y="30" fontSize="12" id="txtname"/>
<mx:TextInput x="79" y="62" id="txtpwd"/>
<mx:Label x="21" y="32" text="姓名:" id="lbname" fontSize="12" fontWeight="bold"/>
<mx:Label x="21" y="64" text="密碼:" id="lbpwd" fontSize="12" fontWeight="bold"/>
</mx:Panel>
</mx:Application>
2.建立test.php,放到php開發的根目錄下
<?php
$str="<Result><msg>success</msg></Result>";
echo $str;
?>
在flex builder3運行HttpDemo.mxml,即可以看到效果。