flex4出来一段时间了,去年看了一段时间flex3,后来由于暂时没有项目需求.所以flex遗忘了很多.这次使用flex4 myeclipse8.5录制了一个视频.以免时间长了遗忘.
软件环境:windows7 flex4 myeclipse8.5 blazeds
功能描述:分别用代码实现了三种flex4与java通信
三种方式:
1 flex 与普通java类通信remoteobject
2 flex 与服务器交互httpservice
3 flex与webservice交互webservice
1 flex 与普通java类通信remoteobject
普通java类:
package com.flex.demo;
/** *//**
* 功能描述:该类用来实现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.simpleservicesource>
properties>
destination>
调用
<s:remoteobject id="serv" destination="myservice" fault="serv_faulthandler(event)" result="serv_resulthandler(event)">
s:remoteobject>
2flex 与服务器交互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进行交互
* @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("我是服务器");
}
}
调用
<s:httpservice id="service" fault="service_faulthandler(event)" result="service_resulthandler(event)" url="http://localhost:8080/flexdemo/simpleserviceservlet">
s:httpservice>
3flex与webservice交互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">
<s:operation name="getweatherbycityname">
<s:request>
<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>
import mx.controls.alert;
import mx.rpc.events.faultevent;
import mx.rpc.events.resultevent;
//错误处理函数
protected function serv_faulthandler(event:faultevent):void
{
alert.show("调用失败了:" event.fault.message as string,"提示");
}
//成功调用函数
protected function serv_resulthandler(event:resultevent):void
{
alert.show("调用成功了:" event.result as string,"提示");
}
protected function service_faulthandler(event:faultevent):void
{
alert.show("调用失败了:" event.fault.message as string,"提示");
}
protected function service_resulthandler(event:resultevent):void
{
alert.show("调用成功了:" event.result as string,"提示");
}
protected function ws_faulthandler(event:faultevent):void
{
alert.show("调用失败了:" event.fault.message as string,"提示");
}
//调用成功了
protected function ws_resulthandler(event:resultevent):void
{
trace(event.result);
lbl.text=(string)(event.result);
}
//第一种调用webservice的方法
//此方法调用webservice
protected function btn_clickhandler(event:mouseevent):void
{
//ws.getweatherbycityname(city.text);
//第二中调用webservice的方法
ws.getweatherbycityname.send();
}
]]>
fx:script>
<fx:declarations>
<s:remoteobject id="serv" destination="myservice" fault="serv_faulthandler(event)" result="serv_resulthandler(event)">
s:remoteobject>
<s:httpservice id="service" fault="service_faulthandler(event)" result="service_resulthandler(event)" url="http://localhost:8080/flexdemo/simpleserviceservlet">
s:httpservice>
<s:webservice id="ws"
wsdl="http://www.webxml.com.cn/webservices/weatherwebservice.asmx?wsdl"
fault="ws_faulthandler(event)"
result="ws_resulthandler(event)"
showbusycursor="true">
<s:operation name="getweatherbycityname">
<s:request>
<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的朋友,看上面的肯定很迷惑,因此这里录制了视频教程,视频的质量是蛮好的,该视频从头至尾演示了flex4整合java的过程.为了方便在windows平台下播放,该视频默认导出格式为exe,在此说明该文件并非病毒,实乃视频文件,大家可放心观看.
视频截图: