0、项目结构:springmvc作为服务端,flex作为客户端,两个模块。
1、下载blazeds-spring.war,解压,把WEB-INF下的lib复制到spring项目中的WEB-INF/lib中,把flex文件夹复制到WEB-INF下。
2、web.xml中新增servlet-mapping:
<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping>
3、修改spring的servlet文件,增加messagebroker:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd"> <context:annotation-config/> <context:component-scan base-package="com.ztesoft"></context:component-scan> <flex:message-broker/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/view/" p:suffix=".jsp"/> </beans>
4、新建controller代码如下:
package com.ztesoft.hj;import org.springframework.flex.remoting.RemotingDestination; import org.springframework.flex.remoting.RemotingInclude; import org.springframework.stereotype.Controller;@Controller("flexController") @RemotingDestination(value = "flexController", channels = "my-amf") public class FlexController { @RemotingInclude public String getHello(String str) { return "hello, " + str; } }
5、flex端页面加入romoteObject:
<s:RemoteObject id="remoteObject" destination="flexController" endpoint="http://127.0.0.1:8080/java-server/messagebroker/amf" fault="remoteObject_faultHandler(event)"> <s:method name="getHello" result="remoteObject_resultHandler(event)"/> </s:RemoteObject>
6、定义成功回调函数和失败回调函数:
private function remoteObject_resultHandler(event:ResultEvent):void { Alert.show(event.result.toString()); } private function remoteObject_faultHandler(event:FaultEvent):void { Alert.show(event.fault.toString()); }
7、定义按钮,调用函数:
private function button1_clickHandler(event:MouseEvent):void { remoteObject.getHello("flex"); }
示例代码:http://download.csdn.net/detail/kevin19900306/9796532