Flex4 + Blazeds + Java 通信

下载参考资料:

一、jar

1.

2.

 

二、安装环境(略)

FB4、MyEclipse6、Tomcat6

 

三、编写代码

1. 新建JavaWeb工程,工程名为:FlexBlazeds

 

2. package remoting; public class EchoService { public String echo(String text) { return "Server say : I received '" + text + "' from you."; } }

 

3. web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 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> <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> <welcome-file-list> <welcome-file>index.htm</welcome-file> </welcome-file-list> </web-app>

 

3.Web-inf目录下建flex目录,建配置文件

remoting-config.xml 、 services-config.xml

 

<?xml version="1.0" encoding="UTF-8"?> <services-config> <services> <service-include file-path="remoting-config.xml" /> <!-- 必要项,注入一个channel,供remoting-config里的对象应用 --> <default-channels> <channel ref="my-amf" /> </default-channels> </services> <channels> <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel"> <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint" /> <properties> <polling-enabled>false</polling-enabled> </properties> </channel-definition> </channels> </services-config>

 

<?xml version="1.0" encoding="UTF-8"?> <service id="remoting-service" class="flex.messaging.services.RemotingService"> <!-- 必要项 --> <adapters> <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true" /> </adapters> <destination id="echoServiceDestination"> <properties> <source>remoting.EchoService</source> </properties> </destination> </service>

 

4. 编写Flex

<?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="remoteObject" destination="echoServiceDestination" result="resultHandler(event);" fault="faultHandler(event);"/> </fx:Declarations> <fx:Script> <!--[CDATA[ import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; private function echo():void { var text:String = ti.text; remoteObject.echo(text); } private function resultHandler(event:ResultEvent):void { ta.text += "Server responded: "+ event.result + "/n"; } private function faultHandler(event:FaultEvent):void { ta.text += "Received fault: " + event.fault + "/n"; } ]]--> </fx:Script> <mx:Label text="Enter a text for the server to echo" x="10" y="4"/> <mx:TextInput id="ti" text="Hello World!" x="10" y="157"/> <mx:Button label="Send" click="echo();" x="182" y="158"/> <mx:TextArea id="ta" width="242" height="20%" x="10" y="29"/> </s:Application>

 

注意这个重要,需建server-config目录

 

 为tomcat发布项目后的位置,web-inf/flex下的remoting-config.xml 、 services-config.xml

 

 最后文档目录如下:

MyEcilpse

Flex4 + Blazeds + Java 通信_第1张图片

 

Flex4

 

 

你可能感兴趣的:(java,MyEclipse,Flex,application,Class,encoding)