BlazeDS, Flex与Java通信

1. 开发工具 : MyEclipse5.5, FlexBuilder3, Tomcat6.

2. 开发过程 :

    第一步 : 下载BlazeDS, 地址:http://opensource.adobe.com/wiki/display/blazeds/Release+Builds, 解压缩。

    第二步 : 在MyEclipse中新建WEB工程,导入依赖库:

                   (1) backport-util-concurrent.jar

                   (2) cfgatewayadapter.jar

                   (3) commons-codec-1.3.jar

                   (4) commons-httpclient-3.0.1.jar

                   (5) commons-logging.jar

                   (6) concurrent.jar

                   (7) flex-messaging-common.jar

                   (8) flex-messaging-core.jar

                   (9) flex-messaging-opt.jar

                   (10) flex-messaging-proxy.jar

                   (11) flex-messaging-remoting.jar

                   (12) xalan.jar

                   这些Jar文件都会在刚才下载的BlazeDS二进制包中存在。

    第三步 : 编写如下两个Java类:

                   (1) package com.fishme.flex;

                         public class FishValueBean {

                                   private String name; 
                                   private Integer length;

 

                                   public String getName() {
                                              return name;
                                   }

                                   public void setName(String name) {
                                              this.name = name;
                                   }

                                   public Integer getLength() {
                                              return length;
                                   }

                                   public void setLength(Integer length) {
                                              this.length = length;
                                   }

                          }

                     (2) package com.fishme.flex;

                           public class ObtainFish { 
                                   public FishValueBean obtainData() {
                                               FishValueBean fish = new FishValueBean();
                                               fish.setName("Fish.....");
                                               fish.setLength(10);
                                               return fish;
                                    } 
                            }

                            第一个Java类是一个简单的实体Bean在第二个类中会用到,第二个Java类就是Flex程序要调用的RemoteObject.

    第四步 : 在WEB-INF目录中建立一个Config文件夹,将如下四个文件考入此文件夹中:

                   (1) messaging-config.xml

                   (2) proxy-config.xml

                   (3) remoting-config.xml

                   (4) services-config.xml

    第五步 : 修改remoting-config.xml文件,在其中的<service>标签中加入如下的配置:                   
                   <destination id="FishMe">
                               <properties>
                                            <source>com.fishme.flex.ObtainFish</source>
                               </properties>
                   </destination>

                   这个配置项主要是配置前面编写好的将在后面被Flex程序调用到的Java类,目地ID为FishMe。

    第六步 : 配置web.xml文件:

                   <?xml version="1.0" encoding="UTF-8"?>
                   <web-app version="2.4"
                                     xmlns="http://java.sun.com/xml/ns/j2ee
                                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                                     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
                            <servlet>
                                        <servlet-name>MessageBrokerServlet</servlet-name>
                                        <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
                                        <init-param>
                                                        <param-name>services.configuration.file</param-name>
                                                        <param-value>/WEB-INF/Config/services-config.xml</param-value>
                                         </init-param>
                                         <load-on-startup>1</load-on-startup>
                            </servlet>

                            <servlet-mapping>
                                         <servlet-name>MessageBrokerServlet</servlet-name>
                                         <url-pattern>/messagebroker/*</url-pattern>
                            </servlet-mapping>

 

                             <welcome-file-list>
                                          <welcome-file>index.jsp</welcome-file>
                            </welcome-file-list> 
                   </web-app>

                   配置Flex应用请求Java类入口。

    第七步 : 在FlexBuilder3中开发如下代码:

                   <?xml version="1.0" encoding="utf-8"?>
                   <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

                                <mx:Script>
                                            <![CDATA[
   
                                                           import mx.rpc.events.FaultEvent;
                                                           import mx.rpc.events.ResultEvent;
   
                                                           import mx.controls.Alert;
   
                                                           private function sendRequest(clickEvent : Event) : void {
                                                                           FishMeRequest.obtainData();
                                                           }
   
                                                           private function requestSuccess(resultEvent : ResultEvent) : void {
                                                                           var searchResult : Object = resultEvent.result;
                                                                            FishMeLabel.text = searchResult.name;
                                                           }
   
                                                           private function requestFailure(faultEvent : FaultEvent) : void {
                                                                           Alert.show("请求服务器失败!");
                                                            }
   
                                            ]]>
                                </mx:Script>
 
                                <mx:RemoteObject

                                                     id="FishMeRequest"

                                                     endpoint="http://localhost:8080/FishMePrj/messagebroker/amf"

                                                     destination="FishMe"

                                                     result="requestSuccess(event)"

                                                     fault="requestFailure(event)">

                                </mx:RemoteObject>
 
                                 <mx:Label id="FishMeLabel" x="314.5" y="167" width="76" fontSize="12"/>
 
                                 <mx:Button id="FishMeButton" x="293" y="211" label="FishMe" width="119"

                                                     height="29"  fontSize="12" click="sendRequest(event)"/> 


                     </mx:Application>
                     这个Flex的主要功能是:单击FishMe按钮请求远程Java对象返回数据显示在Label标签中。

 

                     单击FishMe按钮会调用sendRequest方法,在sendRequest方法中能过RemoteObject组

                     件(ID为FishMeRequest)的endpoint和destination属性还有方法名 obtainData()(FishMeRequest.obtainData();

                     这个方法名与ObtainFish类中的方法名相对应)来请求TOMCAT服务器,请求路径是:

                     http://localhost:8080/FishMePrj/messagebroker/amf,这个路径首先找到前面所建的WEB工程的web.xml文件

                     因为在web.xml文件中配置了以/messagebroker/*结尾的请求路全部交由MessageBrokerServlet来处理,他的配

                     置文件是services-config.xml,在services-config.xml文件中导入了其他三个配置文件:remoting-config.xml,

                     messaging-config.xml和proxy-config.xml,这时会根据destination属性,他的值为“FishMe”查找

                     remoting-config.xml文件中是否有与之相同的目地ID,正好找到了,找到之后他请会调用这个目地ID所对应的Java类也

                     就是ObtainFish类,请求这个obtainData()方法返回FishValueBean对象,请求成功后会调用requestSuccess方法

                     解析返回结果将FishValueBean的name属性的值赋给FishMeLabel标签。

     第八步 : 将由上面的Flex代码生成的SWF文件拷贝到WEB工程中的WebRoot目录中的common目录下,common目录手工建成。

     第九步 : 修改WEB工程的index.jsp文件如下:

                    <%@ page language="java" contentType="text/html; charset=UTF-8"%>

                    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                    <html>
                              <head>
                                      <script type="text/javascript">
                                                 function init() {
                                                           var FishMeSWF = '<object classid = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id = "FishMeSWF" width = "700" height = "500" codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
          + ' <param name = "movie" value = "common/FishMe.swf" />'
          + ' <param name = "quality" value = "high" />'
          + ' <param name = "allowScriptAccess" value = "sameDomain" />'
          + ' <param name = "allowFullScreen" value = "true" />'
          + '</object>';
                                                           document.getElementById("FishMeContainer").innerHTML = FishMeSWF;   
                                                }
                                     </script>
                            </head>

                            <body οnlοad="init()">
                                      <div id="FishMeContainer"></div>
                            </body>
                  </html>

    第十步 : 能过  http://localhost:8080/工程名   来请求index.jsp文件,界面显示SWF文件,单击FishMe按钮Label标签会显示

                   “Fish.....”字符串。

      

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值