0. Project structure: springmvc as the server, flex as the client, two modules.
1. Download blazeds-spring.war, unzip it, copy the lib under WEB-INF to WEB-INF/lib in the spring project, and copy the flex folder to WEB-INF.
2. New servlet-mapping in web.xml:
<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping>
3. Modify the spring servlet file and add 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. The new controller code is as follows:
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. Add remoteObject to the flex page:
<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. Define the success callback function and the failure callback function:
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. Define the button and call the function:
private function button1_clickHandler(event:MouseEvent):void { remoteObject.getHello("flex"); }
Sample code: http://download.csdn.net/detail/kevin19900306/9796532