最近看了一些关于flex3的一些学习资料,算是初级的接触了flex3,我介绍flex3的两种通信方式,一个是HTTPService,一个是LCDS(RemoteObject),其中网络说RemoteObject这种方式的通信是最快的, 我并没有去测试,我相信网络资源不会错的,还有一种通信方式是WebService,这个需要指定你操作的方法,我感觉和HTTPService区别也不是很大,也就没写。
那先介绍一下HTTPService方式通信:
后台代码:
public class Flex3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control","no-cache");
PrintWriter out = response.getWriter();
Integer num1 = Integer.parseInt(request.getParameter("num1"));
Integer num2 = Integer.parseInt(request.getParameter("num2"));
out.print(num1+num2);
out.close();
}
}
前台flex的代码:
引用
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="this.initDataSource()">
<mx:HTTPService id="flexWebservice1" url="flexServlet" method="POST" resultFormat="text" result="flexServiceHandler(event)">
<mx:request xmlns="">
<num1> {this.num1.text}</num1>
<num2> {this.num2.text}</num2>
</mx:request>
</mx:HTTPService>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.collections.ArrayCollection ;
[Bindable]
private var dataSource:Array = new Array();
private function initDataSource():void
{
this.dataSource.push("+");
this.dataSource.push("-");
this.dataSource.push("*");
this.dataSource.push("/");
this.list.dataProvider = dataSource;
}
private function requestServer():void
{
//Alert.show("nihao","flex第一次测试");
this.flexWebservice1.send();
}
private function flexServiceHandler(event:ResultEvent):void
{
var returnData:String = String(event.result);
this.result.text = event.result.toString();
}
]]>
</mx:Script>
<mx:VBox>
<mx:HBox>
<mx:TextInput id="num1" name="num1" width="100">
</mx:TextInput>
<mx:Label text="+" width="100">
</mx:Label>
<mx:List x="126" y="-2" width="59" height="46" id="list" dropEnabled="true" allowMultipleSelection="false" >
</mx:List>
<mx:TextInput id="num2" name="num2" width="100">
</mx:TextInput>
<mx:Label text="=" width="100">
</mx:Label>
<mx:TextInput id="result" name="result" width="100">
</mx:TextInput>
<mx:Button label="submit" click="this.requestServer()" width="100">
</mx:Button>
</mx:HBox>
</mx:VBox>
</mx:Application>
web.xml的配置我就不写了,我用到 是servlet来写的这么一个小程序,只是简单的介绍一下flex的通信方式
--------------------------------------下面谈谈lcds远程对象这种通信 方式,因为lcds是免费的,所以介绍 这个,还有一个是 blanzed ,那个是收费的
前台代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import com.test.model.Person;
private function buttonClick():void
{
var value:String = this.username.text;
this.testService.sayHello(value);
}
private function clickHandler(event:ResultEvent):void
{
var str:String = String(event.result);
Alert.show(str);
}
]]>
</mx:Script>
<mx:RemoteObject id="testService" destination="testService">
<mx:method name="sayHello" result="clickHandler(event);" />
</mx:RemoteObject>
<mx:HBox>
<mx:TextInput id="username" width="120"/>
<mx:Button label="click me" width="120" click="buttonClick();"/>
</mx:HBox>
</mx:Application>
后台代码:
public class TestService
{
public String sayHello(String name)
{
System.out.println(name);
return "Hello: " + name;
}
}
这里有一个最重要的配置文件,就是 web-info 目录先 felx下的remoting-config.xml这个文件
我的配置是:
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService">
<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<destination id="testService">
<properties>
<source>com.test.service.TestService</source>
</properties>
</destination>
</service>
如何安装lcds 我在另外一讲中在具体的说