今天做了个关于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,即可以看到效果。