您的位置:首页 > 编程语言 > Java开发

Flex与Java的交互

2011-05-28 20:02 162 查看
最近做项目需要用到flex做前台,java做后台处理,看了西安云工厂旺旺老师的视频对此有了一个比较基础的了解,特来分享一下:

 

编译器:Myeclipse,Flex Builder 4(也可以将两者集成到eclipse中)

软件:Tomcat,BlazeDS

 

1.在Myeclipse中创建Web工程:FlexApp

 

新建一个包blazedsTest,该包下有一个类HelloServer,代码如下:

public class HelloServer {
public void sayHello(String msg){
System.out.println("message="+msg);
}
}
 

将工程部署到tomcat。

 

2.启动Flex Builder 4新建一个工程FlexApp,依次选择web->J2EE->BlazeDS

 



 

点击下一步,选择目标文件夹并验证配置



验证有效,选择下一步将主mxml文件改名为index.mxml

 

3.打开index.mxml文件并写入代码

拖动控件建立一个窗口



 

 

4.在index.mxml中写代码如下:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<mx:RemoteObject id="helloClientTag" destination="helloServerTag"
result="haolejiaowo(event)" endpoint="/flexApp01/messagebroker/amf"></mx:RemoteObject>
</fx:Declarations>
<fx:Script>
<!--[CDATA[
import mx.controls.*;
import mx.rpc.events.ResultEvent;

private function sendData():void{
var msg:String = txtMsg.text;

helloClientTag.sayHello(msg);
}

private function haolejiaowo(event:ResultEvent):void{
var serverReturnMsg:String = event.result as String;

Alert.show(serverReturnMsg);
}
]]-->
</fx:Script>
<s:Panel x="187" y="92" width="307" height="207" chromeColor="#1F2ABF" title="flex与java交互" fontSize="18">
<s:Button x="118" y="130" label="按钮"/>
<s:Label x="30" y="34" text="消息" width="45"/>
<s:TextInput x="83" y="34" id="txtMsg" click="sendData()"/>
</s:Panel>
</s:Application>
 

 

5.打开web工程index.jsp页面,输入:response.sendRedirect("FlexApp-debug/index.html");

 

6.用BlazeDS中tomcat包下的web-inf文件夹覆盖项目工程的web-inf文件夹,修改remoting-config.xml文件,加入:

<destination id="helloServerTag">
<properties>
<source>blazedsTest.HelloServer</source>
</properties>
</destination>
 

重新部署到tomcat,启动tomcat,在浏览器输入:http://localhost:8080/FlexApp/index.jsp

页面自动跳转到http://localhost:8080/FlexApp/FlexApp-debug/index.html

 

在页面输入字符串,点击提交,myeclipse控制台输出所提交的内容
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息