1.首先要有一个安装了Flex插件的Eclipse,并连接上Tomcat。
2.新建DynamicWeb Project.
3.右击WebContent--->import--->General--->FileSystem--->next--->Browse;然后选择BlazeDS的路径(我的是在Tomcat下的webapps下),然后确定,Finish。
4.重启Tomcat。
5.工程右键---》添加/更改项目类型---》Flex项目类型---》应用程序服务类型:选择J2EE;底下选择BlazeDS---》next---》根文件夹:选择自己Tomcat下的webapps下的自己个工程的路径;根URL:写成Http://localhost:8080/工程名;上下文根目录:写成?/demo--->点击“验证配置”---》如果验证成功,点击finish。
测试代码:
1.右击工程---》属性---》Flex构建路径---》将主源文件夹的src改为flex_src用来存储Flex文件。
2.新建Java类HelloWorld,具体代码如下:
package com.miapsoft.zzy;
public class HelloWorld {
??????
?????? ?publicString getHelloWorld(String name) {
???????????????????? return"Hello World! 欢迎您:"+name;
????????????? }
}
3.新建mxml文件,具体代码如下:
<?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">
?????? <!--~~~~~~~~~~~~~~~~~~~~Script~~~~~~~~~~~~~~~~~~~~-->
?????? <fx:Script>
????????????? <![CDATA[
???????????????????? importmx.rpc.events.ResultEvent;
???????????????????? publicfunction remotingSayHello(event:Event):void{
??????????????????????????? variname:String=tName.text;
??????????????????????????? remoteSay.getHelloWorld(iname);
???????????????????? }
????????????? ]]>
?????? </fx:Script>
?????? <!--~~~~~~~~~~~~~~~~~~~~Declarations~~~~~~~~~~~~~~~~~~~~-->
?????? <fx:Declarations>
????????????? <s:RemoteObjectid="remoteSay" destination="helloWorld">
????????????? </s:RemoteObject>
?????? </fx:Declarations>
?????? <!--~~~~~~~~~~~~~~~~~~~~UIComponents~~~~~~~~~~~~~~~~~~~~-->
?????? <s:VGrouptop="100" left="300" fontSize="18"horizontalAlign="right" >
????????????? <s:HGroup>
???????????????????? <mx:Labeltext="Your Name:" />
???????????????????? <mx:TextInputid="tName" />
????????????? </s:HGroup>
????????????? <mx:Buttonlabel=" 提交"click="remotingSayHello(event);" />
?????????????
?????? </s:VGroup>
?????? <s:VGrouptop="180" left="300" fontSize="20"fontWeight="bold">
????????????? <s:Labeltext="返回结果:"/>
????????????? <mx:Labeltext="{remoteSay.getHelloWorld.lastResult}"
??????????????????????????? ??id="lblView"color="#0000ff"/>
?????? </s:VGroup>
</s:Application>
4.修改WebContent下的WEB-INF下的flex下的remoting-config.xml.
default-channels标签后面添加如下内容:
?????? <destinationid="helloWorld">
????????????? <properties>
???????????????????? <source>com.miapsoft.zzy.HelloWorld</source>
????????????? </properties>
?????? </destination>
5.重启tomcat。运行MXMXL文件。