FLEX与JAVA交互

准备工作:

1.下载FLEX的插件

step 1.下载 flex 3.0 plugin 插件 装在 我的myeclipse 6.0 上

   1) 下载地址:http://trials.adobe.com/Applications/Flex/FlexBuilder/3/FB3_WWEJ_Plugin.exe

   2)安装插件

    将flex plugin 安装目录里面的com.adobe.flexbuilder.update.site 文件夹下面的features 文件夹,plugins 文件夹 和 site.xml 文件 添加在myeclipse 文件夹的eclipse文件夹内.

注意:或者你如果安装了FLEXBUILD的话,你也可以找到他的安装目录将他目录下的features 和plugins 来覆盖eclipse下的该两个文件夹

              将 flex plugin 安装目录里面的eclipse 文件夹下面的features 文件夹,plugins 文件夹  覆盖在myeclipse 文件夹的eclipse文件夹内.(防止更新)

step1 完

 

step 2.下载 blazeds  

http://download.macromedia.com/pub/opensource/blazeds/blazeds_bin_3-0-0-544.zip

由Web project反向加入Flex,也就是Java+Flex


1、 先建立一个web工程:flexweb。(略)
2、 向flexweb工程手工添加Flex需要的元素。
1)首先将BlazeDS需要的jar文件拷到工程的lib目录下。可以将blazed应用程序中的lib下的jar文件拷到该工程下的lib目录下。
2)然后要加入Flex BlazeDS需要的配置文件。在WEB-INF下新建一个名为flex的文件夹,然后将我们blazed应用程序下的firstFlex该文件夹下的四个xml文件拷到该文件夹下。
3)最后,修改web.xml文件,加入Flex的配置。做法一个简单的把上面我们新建的那个flex工程的web.xml的部分代码拷过来。


<context-param>
<param-name>flex.class.path</param-name>
<param-value>/WEB-INF/flex/hotfixes,/WEB-INF/flex/jars</param-value>
</context-param>
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>

</listener>
<!-- MessageBroker Servlet -->
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<display-name>MessageBrokerServlet</display-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/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>


3、 将该工程发布到tomcat下,并启动tomcat。(注:一定要启动tomcat,因为在后面的设置中,它要验证工程的路径)


4、 然后在该工程上右键Flex Project NatureAdd Flex Project Nature

注意,其中有一个选择war文件的地方,我们就选择blazed.war所在的文件路径

5、 设置完成后,会发现web工程的目录结构已经发生了改变
flex的mxml文件默认的放在src文件夹中,和java文件共用一个目录。
6、 配置flex默认的sdk。这样配置完,还不行,程序可能还不能正常地运行,还需要配置他使用的sdk。
 在你的工程名点右键,然后选择FLEX Compile选择第一个use default sdk,而不是选择use server's sdk,如果不选择这个选择项,mxml文件将不会编译.

8、 马上就大功告成了,让我们来写个程序测试一下吧。


1)新建一个java类:Hello.java
package com;
public class Hello {
public String hello(String name){
System.out.println("flex调用我了,真好~~~~");
return "hello "+name;
}
}


2)为flex配置这个要调用的对象,修改WEB-INF/flex下remoting-config.xml
加入:
<destination id="hello">

//hello是别名,他会和mxml文件中mx:RemoteObject destination="hello"的destination对应
<properties>
<source> com.Hello </source>

//com.Hello为类的全路径名
</properties>
</destination>
配置services-config.xml
 <channels>

        <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://127.0.0.1:8080/flexweb/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
        </channel-definition>

        <channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel">
            <endpoint url="https://127.0.0.1:8080/flexweb/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/>
            <properties>
                <add-no-cache-headers>false</add-no-cache-headers>
            </properties>
        </channel-definition>

        <channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://127.0.0.1:8080/flexweb/messagebroker/amfpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
            <properties>
                <polling-enabled>true</polling-enabled>
                <polling-interval-seconds>4</polling-interval-seconds>
            </properties>
        </channel-definition>

         </channels>

//兰色字体为固定写法

////http://localhost:8080/flexweb/messagebroker/amf中的含义为,http://主机地址:断口号/应用程序名/messagebroker/amf

3)编写一个Flex程序
<?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.ResultEvent;
function gg(evnet:ResultEvent):void{
var ff:String = evnet.result as String;
ggg.text = ff;
}
function remotingSayHello():void{
var sname:String = nameInput.text;
h.hello(sname);
}
]]>
</mx:Script>
<mx:RemoteObject destination="hello" id="h"
result="gg(event)" endpoint="http://localhost:8080/flexweb/messagebroker/amf" >

//endpoint=http://localhost:8080/flexweb/messagebroker/amf中的含义为,http://主机地址:断口号/应用程序名/messagebroker/amf
</mx:RemoteObject>
<mx:TextArea id="ggg" x="109" y="122"/>
<mx:Button label="say hello" click="remotingSayHello();" x="144" y="193"/>
<mx:TextInput id="nameInput" x="109" y="73"/>
<mx:Label text="name" x="47" y="75"/>
</mx:Application>


4)重启tomcat,运行flexweb.mxml.
运行完成以后会出现一个flexweb.swf的文件(如果我们没有配置第6步,该文件是编译不出来的)

在编译完后,访问网页会出现404异常。具体原因不详。但是它仍会编译出一个swf文件,访问这个swf即可。

解压之后是个 blazeds. war包,把它放在tomcat 的webapp里面 部署一下就会释放出来一个类似与工程文件的东西

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值