Flashbuilder4.5 + eclipse for j2ee + Blazeds 简单实例(适合新手)

1. 下载 Blazeds文件:

         a. http://sourceforge.net/adobe/blazeds/wiki/Downloads/  下拉网页找到  Release Builds 说明下的  Download the latest BlazeDS Release builds

            点击此链接

         b. 这个时间会弹出Adobe登陆用户界面,如果有用户就登陆一下,没有就点击边上的创建用户(Create an Adobe Account)

         c. 中转到 BlazeDS Terms of Use 页面,这个时候只能 选中 “I Agree” ,下一步

         d.新页面中将页面下拉到下图位置:(如果只是使用的话,建议下载binary Distribution这个版本的)

        

       e.将下载后的文件解压待用。。。。

2.新建JAVA web项目

      a. “文件”---“新建”------“其他”---Dynamic Web Project (动态WEb项目)                    

        

      b.新建项目BlazedsDemo参数如下(只供参考,只要能新建出项目即可)             

          

       c.在新项目下的Src文件中新增一个HelloWorld类              

HelloWorld
 1 package cn.riahome.java;
 2 
 3 public class HelloWorld
 4 {
 5 
 6     public HelloWorld() {  
 7     } 
 8     public String getHelloWorld(String name) {  
 9         return "Hello World!"+name;  
10     }
11 }

       d.将Blazeds刚解压出来的 blazeds\WEB-INF\lib 下的文件复制到 本项目下的WebContent下的WEB-INF\lib下

       e. blazeds\WEB-INF\flex 下的文件复制到本项目下的WebContent下的WEB-INF下

       f.  blazeds\WEB-INF 下的web.xml 替换本项目下的WebContent下的WEB-INF中的web.xml

      g. 在WEB-INF\flex\remoting-config.xml 中新增如下代码:                    

          <destination id="helloWorld">  
                <properties>
                    <source>cn.riahome.java.HelloWorld</source>
                </properties>
          </destination>

          修改后文件如下:               

Remoting-config文件
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <service id="remoting-service" 
 3     class="flex.messaging.services.RemotingService">
 4 
 5     <adapters>
 6         <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
 7     </adapters>
 8 
 9     <default-channels>
10         <channel ref="my-amf"/>
11     </default-channels>
12     <destination id="helloWorld">  
13         <properties>  
14             <source>cn.riahome.java.HelloWorld</source>  
15         </properties>  
16       </destination>
17 </service>

        注:  a.  destination节点的id名称是要被Flex端直接调用的

                       b. <source>cn.riahome.java.HelloWorld</source>  指定所对应的类

      h. 修改“构建路径”

            选中项目右击“属性”--在弹出框中选择“java构建路径”---选择"源代码"子项 如下图            

      

            注:将输出路径修改为:WebContent/WEB-INF/classes 系统会自动到Web-inf /classes 中找文件编译后的类文件

                           1.这里一定要设置,要不然项目运行时会提醒找不到此类的错误

3.新建Flex项目

                 a. “文件”---“新建”--“Flex项目” 填写信息如图:                     

      

          b.点击下一步                   

          

                  注:a. 根文件夹: 填写TomCat下的你程序发布的本地地址 如: C:\tomcat\webapps\demo\

                       b.根URL:  填写你程序运行起来的网页地址(如图)

                       c.上下文根目录: 填写网页地址中端口号后的名称  如上图中 “根URL” 端口号后的 BlazedsDemo

                       d.输出文件夹:与“根文件夹”相同

                      e.点击“完成” 建立项目

           c.配置新建项目的编译参数

                      a.右击项目--“属性” --- “Flex编译器” ---- 在右侧找到 “附加的编译器参数” 在下方框中 增加一段

                          "{你的tomcat发布地址下的}\WebContent\WEB-INF\flex\services-config.xml" (根据实际情况配置此文件所在位置)

   3.新建文件名index.mxml的文件其内容参考下面代码                       

index.mxml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
 3                xmlns:s="library://ns.adobe.com/flex/spark" 
 4                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
 5     
 6     <fx:Declarations>
 7         <!-- 将非可视元素(例如服务、值对象)放在此处 -->
 8         <s:RemoteObject id="remoteObject"
 9             destination="helloWorld"  
10             result="resultHandler(event)" fault="faultHandler(event)"  
11             />
12     </fx:Declarations>
13     
14     <fx:Script>
15         <![CDATA[
16             import mx.controls.Alert;
17             import mx.rpc.events.FaultEvent;
18             import mx.rpc.events.ResultEvent;     
19             
20             private function resultHandler(event:ResultEvent):void {     
21                 Alert.show(event.result.toString(), "成功");     
22             }     
23             
24             private function faultHandler(event:FaultEvent):void {     
25                 Alert.show(event.fault.toString(), "失败");     
26             }    
27             
28             protected function button1_clickHandler(event:MouseEvent):void
29             {
30                 remoteObject.getHelloWorld("zhaoyashan");        
31             }
32         ]]>
33     </fx:Script>
34     <s:Button label="发送消息" click="button1_clickHandler(event)" x="256.5" y="197"/>   
35 </s:Application>

注:    

   a. <s:RemoteObject id="remoteObject"
     destination="helloWorld"          //对应Blazeds中配置 remoting-config.xml的Destination 名称
     result="resultHandler(event)"   // 执行成功执行

              fault="faultHandler(event)"      //执行失败执行
    />

        b. remoteObject.getHelloWorld("zhaoyashan");  //对应Java类中的getHelloWorld方法,有参数时可以直接传入

总结:将详细过程写在这里了,还有什么不懂的请留言,如有错误请多多指出,我会更加完善让其他新手,少走一点弯路。

                                                                  2012-5-24

            

 

 

原文地址:https://www.cnblogs.com/enum/p/2515458.html