转:Java+blazeds+Flex的例子 .

由于Flex不提供操作数据库的工具,所以必须和其他语言通信来操作数据库。blazeds是连接Java和Flex的工具,它是完全免费、开源的,大家可以去网上搜索下载。

要使用到的工具:

Tomcat 6.0

MyEclipse 6.5

Flex Builder 3

blazeds

打开MyEclipse,新建一个Java Web工程,工程名Test,然后建包,写一个简单的方法如下:

package com.demo;

public class HelloWorld {

    public String sayHello(String name) {
        return"hello," + name;

    }

}

解压blazeds,复制WEB-INF文件夹,覆盖掉你的Test工程下的WebRoot下的WEB-INF文件夹。

打开Test工程下的WebRoot/WEB-INF/flex/remoting-config.xml这个文件,插入以下代码:

<destination id="Hello">  
       <properties>  
           <source>com.demo.HelloWorld</source>  
       </properties>  
 </destination>
打开Flex Builder 3,新建一个web application工程,工程名FlexTest,服务器技术(server technology)选择none,然后next,在output folder(指定输出路径)里,选择刚才建立的Java工程Test的WebRoot目录。(一定要指定对)   然后Finish。

打开FlexText.mxml,代码如下:

<?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;   
      [Bindable]   
      private var helloResult:String;  
     
      private function Hello():void {   
     test.sayHello(inputText.text);    //通过RemoteObject的对象test,直接调用Java端的方法
        }   
       
       private function resultHandler(event:ResultEvent):void {   
          helloResult = event.result as String;   
       }
      
   ]]>  
</mx:Script >
<mx:RemoteObject id="test" destination="Hello" result="resultHandler(event)" endpoint="/Test/messagebroker/amf"/>
   <mx:Label text="输入文字" id="nameLabel" x="19" y="77"/>
   <mx:Label id="resultLabel" text="{helloResult}" width="205" x="78" y="30" fontSize="13" fontWeight="bold" color="#FA7A08"/>
   <mx:TextInput id="inputText" x="78" y="73"/>
   <mx:Button label="HelloWord" id="Button" click="Hello()" x="101" y="126"/>
   <mx:HBox x="10" y="10" width="305" height="221" themeColor="#6E4AF7">
   </mx:HBox>

</mx:Application>

注意:在RemoteObject标签下的destination属性的值,一定要和Java工程的remoting-config.xml中的destination的id一样。endpoint的值要和Java工程名一样,例如“/aaa//messagebroker/amf”,“/bbb//messagebroker/amf”。

保存Flex工程,Flex会自动输出到Test工程下的WebRoot文件夹下。

在MyEclipose下刷新Test工程,会在WebRoot下看到生成的新文件,打开web.xml更改启动首页,把<welcome-file-list>标签下的<welcome-file>的值改为FlexText.html(Flex生成的),如下:

    <welcome-file-list>
        <welcome-file>FlexText.html</welcome-file>
        <welcome-file>FlexText.htm</welcome-file>
    </welcome-file-list>

保存项目,发布到Tomcat,启动Tomcat服务,在IE中输入http://localhost:8080/Test/

就能看到效果了!


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/woodytf/archive/2008/10/25/3143012.aspx

原文地址:https://www.cnblogs.com/hucy/p/2599820.html