1.关于Flex+java整合有三种方式配置,其原理大致相同 首先需要blazeds从官网上可以下载http://www.adobe.com
第一种:
myeclipse 工具中新建web项目. Flex_Service 作为后台服务项目
右击WebRoot --import --- file System 选择你的blazeds.war解压成一个文件夹 完成
新建类 src 下 simpleService
public class SimpleService {
public String sayHello(String name)
{
return "Hello"+name;
}
public int add(int a,int b)
{
return a+b;
}
}
然后在WebRoot --WEB-INFO-flex - remoting-config.xml
<destination id="service">
<properties>
<source>SimpleService</source>
</properties>
</destination>
然后运行发布在Tomcat中,这时你会发现在Tomcat 的webapps下多了个应用项目 Flex_Service
不要关闭后台的运行项目
新建客户端项目 flash Builder4.6 工具
建一个Flex项目 Flex_Client 应用服务器选择java 勾选 远程访问服务Blazeds(免费)
很重要的一步:java服务器位置
根文件夹:Tomcat 下的Flex_service目录 E:\Tomact\apache-tomcat-6.0.20\webapps\Flex_Service
根URl:http://localhost:8080/Flex_Service
上下文根目录:/Flex_Service
已编译的Flex文件会在Tomcat下有个这样的目录,
E:\Tomact\apache-tomcat-6.0.20\webapps\Flex_Service\Flex_Client-debug
点击完成OK
写Flex代码
<?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">
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="middle">
</s:VerticalLayout>
</s:layout>
<fx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function button1_clickHandler(event:MouseEvent):void
{
remote.sayHello(a.text);
}
protected function remote_resultHandler(event:ResultEvent):void
{
// TODO Auto-generated method stub
result.text = event.result.toString();
}
protected function remote_resultHandler2(event:ResultEvent):void
{
// TODO Auto-generated method stub
re.text = event.result.toString();
}
protected function button2_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
remotes.add(int(int1.text),int(int2.text));
}
]]>
</fx:Script>
<fx:Declarations>
<s:RemoteObject id="remote" destination="service" result="remote_resultHandler(event)"
showBusyCursor="true">
</s:RemoteObject>
<s:RemoteObject id="remotes" destination="service" result="remote_resultHandler2(event)"
showBusyCursor="true">
</s:RemoteObject>
</fx:Declarations>
<s:HGroup width="400" height="30" verticalAlign="middle" paddingLeft="2">
<s:TextInput id="a">
</s:TextInput>
<s:Button label="提交" click="button1_clickHandler(event)">
</s:Button>
<s:TextInput width="100%" id="result">
</s:TextInput>
</s:HGroup>
<s:HGroup width="400" height="30" verticalAlign="middle" paddingLeft="2">
<s:TextInput width="63" id="int1">
</s:TextInput>
<s:Label text="+">
</s:Label>
<s:TextInput width="63" id="int2">
</s:TextInput>
<s:Button label="=" width="40" click="button2_clickHandler(event)">
</s:Button>
<s:Label id="re">
</s:Label>
</s:HGroup>
</s:Application>
右击 运行,能成功哦,
由于晚上时间的关系,未贴图片, 但只要按照步骤来,一定可以成功的。