web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>flexweb</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
<!-- The filter for struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter>
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- Http Flex Session attribute and binding listener support --> <!-- <listener> <listener-class>flex.messaging.HttpFlexSession</listener-class> </listener> -->
<!-- MessageBroker Servlet 单独为Flex配置xml--> <servlet> <servlet-name>flex</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/flex-application-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>flex</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> <!-- The filter mapping for struts2 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/struts2/*</url-pattern> </filter-mapping>
<!-- Map all /messagbroker requests to the DispatcherServlet for handling -->
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?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"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <mx:RemoteObject id="dbRemote" destination="dataBean" endpoint="http://localhost:8080/phmis/messagebroker/amf" result="onResultHandler(event)" fault="onFaultHandler(event)"> </mx:RemoteObject> </fx:Declarations> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; private function onResultHandler(event:ResultEvent):void{ Alert.show(String('onResultHandler')); Alert.show(String(event.result));//, String(example.data)); } private function onFaultHandler(event:FaultEvent):void{ Alert.show(String(event.fault));//, "Fault!"); Alert.show(String('onFaultHandler')); } private function btn_Click1EventHandler(event:MouseEvent):void{ dbRemote.getName(); } private function btn_Click2EventHandler(event:MouseEvent):void{ dbRemote.getParams(); } ]]> </fx:Script> <mx:Button x="10" id="btn_Click1" label="RemoteClass->getName" click="btn_Click1EventHandler(event)"/> <mx:Button x="190" id="btn_Click2" label="RemoteClass->getParams" click="btn_Click2EventHandler(event)" width="203"/> </s:Application>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>flexweb</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- The filter for struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- Http Flex Session attribute and binding listener support --> <!-- <listener> <listener-class>flex.messaging.HttpFlexSession</listener-class> </listener> --> <!-- MessageBroker Servlet 单独为Flex配置xml--> <servlet> <servlet-name>flex</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/flex-application-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>flex</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> <!-- The filter mapping for struts2 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/struts2/*</url-pattern> </filter-mapping> <!-- Map all /messagbroker requests to the DispatcherServlet for handling --> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
src/flex-application-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd"> <bean id="_messageBroker" class="org.springframework.flex.core.MessageBrokerFactoryBean"> <property name="servicesConfigPath" value="/WEB-INF/flex/services-config.xml" /> </bean> <!-- Maps request paths at /* to the BlazeDS MessageBroker --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value>/*=_messageBroker</value> </property> </bean> <!-- Dispatches requests mapped to a MessageBroker --> <bean class="org.springframework.flex.servlet.MessageBrokerHandlerAdapter" />
<flex:remoting-destination ref="dataBean" /> </beans>
java
package example.fx; public class DataBean { private String name = ""; private String params = ""; public String getName() { return this.getClass().getName()+ " : " +name; } public void setName(String name) { this.name = name; } public String getParams() { return params; } public void setParams(String params) { this.params = params; } }
src/struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" namespace="" extends="struts-default"> <action name="*" class="com.opensymphony.xwork2.ActionSupport"> <result name="success">/WEB-INF/page/flex/{1}.swf</result> </action> </package> </struts>
src/application.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="dataBean" class="example.fx.DataBean"> <property name="params"> <value>Hello Flex!!!</value> </property> </bean> </beans>