BlazeDS 是一个基于服务器Java 远程控制 (remoting) Web 消息传递 (messaging) 技术,它能够使得后端的 Java 应用程序和运行在浏览器上的 Adobe Flex 应用程序相互通信。这篇文章中,我讲述一种方法(也许不是最好的)使得我能够成功地利用 BlazeDS Flex 建立一个简单的程序。使用的 IDE eclipse, 而并非 Flex Builder.
    下面介绍一个简单的实例来引大家入门
    <?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” fontSize=”15″ layout=”absolute” initialize=”init()”>
<mx:Script>
  <![CDATA[
   import mx.rpc.events.ResultEvent;
   import mx.controls.Alert;
   private function init():void{
    btn.addEventListener(MouseEvent.CLICK,btn_click);
   }
   
   private function btn_click(evt:MouseEvent):void{
    //Alert.show(“你好”);
    srv.send();
   }
   
   private function httpService_fault(evt:ResultEvent):void{
    //这是一个XML文档不能这么解析
    Alert.show(“”+evt.result);
   
   }
  ]]>
</mx:Script>
<!– 这种数据绑定还是比较简单的
<mx:HTTPService id=”srv” destination=”catalog” useProxy=”true”/>

<mx:DataGrid dataProvider=”{srv.lastResult.catalog.product}” width=”100%” height=”100%”/>

<mx:Button label=”Get Data” click=”srv.send()”/>
  –>
<!– 使用了配置文件来访问服务器端的jsp文件 –>
<mx:HTTPService id=”srv” destination=”catalog” useProxy=”true” result=”httpService_fault(event);”/>
<mx:DataGrid dataProvider=”{srv.lastResult.catalog.product}” width=”100%” height=”100%”/>
<mx:Button label=”Get data” id=”btn”/>

</mx:Application>


在proxy_config.xml文件中的配置如下
<?xml version=”1.0″ encoding=”UTF-8″?>
<service id=”proxy-service”
    class=”flex.messaging.services.HTTPProxyService”>

    <properties>
        <connection-manager>
            <max-total-connections>100</max-total-connections>
            <default-max-connections-per-host>2</default-max-connections-per-host>
        </connection-manager>
        <allow-lax-ssl>true</allow-lax-ssl>
    </properties>

    <adapters>
        <adapter-definition id=”http-proxy” class=”flex.messaging.services.http.HTTPProxyAdapter” default=”true”/>
        <adapter-definition id=”soap-proxy” class=”flex.messaging.services.http.SOAPProxyAdapter”/>
    </adapters>

    <default-channels>
        <channel ref=”my-amf”/>
    </default-channels>

    <destination id=”DefaultHTTP”>
    </destination>
    <destination id=”catalog”>
  <properties>
   <url>/{context.root}/categet.jsp</url>
  </properties>
    </destination>
</service>


其中的categet.jsp 文件放在工程目录下 <url>/{context.root}/categet.jsp</url>是用于访问该文件。
categet.jsp


<%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%>
<?xml version=”1.0″ encoding=”utf-8″?>
<catalog>
    <product productId=”1″>
        <name>Good1</name>
        <description>itsme</description>
        <price>17173</price>
        <image>nihao</image>
        <category>niasd</category>
        <qtyInStock>woasdasd</qtyInStock>
    </product>
    <product productId=”2″>
        <name>Good1</name>
        <description>itsme</description>
        <price>17173</price>
        <image>nihao</image>
        <category>niasd</category>
        <qtyInStock>woasdasd</qtyInStock>
    </product>
    <product productId=”3″>
        <name>Good1</name>
        <description>itsme</description>
        <price>17173</price>
        <image>nihao</image>
        <category>niasd</category>
        <qtyInStock>woasdasd</qtyInStock>
    </product>
    <product productId=”4″>
        <name>Good1</name>
        <description>itsme</description>
        <price>17173</price>
        <image>nihao</image>
        <category>niasd</category>
        <qtyInStock>woasdasd</qtyInStock>
    </product>
</catalog>

返回给客户端的是一个xml文件,用于填充datagrid组建

版权声明:本文为magic0123原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/magic0123/archive/2010/01/18/1650701.html