Flex+Spring三种通讯方式五种情况
1.HTTPService
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
?<!--通过post请求-->
?<mx:HTTPService id="myRequest" method="POST"?
?????url="http://localhost:9090/Flex_J2EE/flexServlet"?
?????useProxy="false">
?</mx:HTTPService>
?<mx:Button id="btnSend" label="发送请求" click="myRequest.send();" x="10"?
????? y="19"/>
?
?<mx:Panel x="10" y="49" width="374" height="200" layout="absolute">
??<!--显示返回的数据-->
??<mx:DataGrid? x="10" y="8"?
????? dataProvider="{myRequest.lastResult.myxml.response}">
???<mx:columns>
????<mx:DataGridColumn headerText="i " dataField="i "/>
????<mx:DataGridColumn headerText="msg" dataField="msg"/>
???</mx:columns>
???</mx:DataGrid>
?</mx:Panel>
</mx:Application>
在web.xml文件中
? <servlet>
??????????? <servlet-name>flexServlet</servlet-name>
??????????? <servlet-class>com.bjsxt.usermgr.client.FlexServlet</servlet-class>?
??</servlet>?
??<servlet-mapping>
??????????? <servlet-name>flexServlet</servlet-name>
??????????? <url-pattern>/flexServlet</url-pattern>?
??</servlet-mapping>
2.RemoteObject
可以将对象发送给web端
可以将flex里的对象和java里的对象对应起来..
可以直接将flex里的对象发送给java端
也可以直接将java端的对象或者集合返回给flex
使用mx:RemoteObject
第一步:创建flex项目时指明项目使用远程数据调用,并指明远程服务的contextroot和服务器位置
第二步:在Flex组件中定义RemoteObject
<mx:RemoteObject id="freshQuotes" destination="Portfolio"???fault="onFault(event);"?>
id用于被flex中的其他方法调用的引用
destination 与 remoting-config.xml中的目标配置一致
fault指定远程调用错误的回调方法?
<mx:method name="getQuotes"?? concurrency="last"???result="onResult(event);"?/>
mx:method指明远程对象的被调用方法
result指定结果返回的回调方法
</mx:RemoteObject>
第三步:编写Java服务端对象,简单对象
第四步:配置remoting-config.xml
<destination id="Portfolio">?????
id与RemoteObject标签中的destination一致?
????????<properties>
??????????? <source>com.zw.action.Portfolio</source>?????? 给出服务端类路径
??????? </properties>
</destination>
第五步:部署web应用,该web应用要含有编译好的swf文件
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"?
??????layout="absolute">
?<mx:Script>
??<![CDATA[
???import mx.rpc.events.FaultEvent;
???import mx.controls.Alert;
???import mx.rpc.events.ResultEvent;?
???// 请求成功
???private function resultHandler(event:ResultEvent):void
???{
????Alert.show(event.result.toString(),"成功");
???}
???// 请求失败
???private function faultHandler(event:FaultEvent):void
???{
????Alert.show(event.fault.toString(),"失败? ");
???}
??]]>
?</mx:Script>?
?<mx:RemoteObject id="remoteObject"
????? destination="helloFlex"?
????? result="resultHandler(event)"
????? fault="faultHandler(event)"/>
?<!--endpoint="http://localhost:8080/Flex_J2EE/messagebroker/amf" 配置更加的灵活-->
?<mx:Button label="发送请求" click="remoteObject.getHello()" x="200"?
????? y="100"/>
</mx:Application>
?
remoting-config.xml文件
<service id="remoting-service"
??? + instance.toString());
return instance;
}
public Object lookup(FactoryInstance inst)
{
//这就是从spring容器中getbean了?
SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;
return factoryInstance.lookup();
}
static class SpringFactoryInstance extends FactoryInstance
{
SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties)
{
super(factory, id, properties);
}
public String toString()
{
return "SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" +
getScope();
}
public Object lookup()
{
ApplicationContext appContext =
WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());
String beanName = getSource();
try
{
return appContext.getBean(beanName);
}
catch (NoSuchBeanDefinitionException nexc)
{
ServiceException e = new ServiceException();
String msg = "Spring service named '" + beanName + "' does not exist.";
e.setMessage(msg);
e.setRootCause(nexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
}
catch (BeansException bexc)
{
ServiceException e = new ServiceException();
String msg = "Unable to create Spring service named '" + beanName + "' ";
e.setMessage(msg);
e.setRootCause(bexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
}
}
}
}
4、在services-config.xml中注册SpringFacotry,配置如下:
<factories>
<factory id="springfactory" encoding="UTF-8"?>
<web-app id="communication" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
?<!-- spring的配置文件路径 -->
?<context-param>
??<param-name>contextConfigLocation</param-name>
??<param-value>
???classpath:Application.xml,
???classpath:Application_DAO.xml,
???classpath:Application_Service.xml
??</param-value>
?</context-param>
?
?
?<!-- spring整合BlazeDS的请求分发器Servlet,
org.springframework.web.servlet.DispatcherServlet代替了flex.messaging.MessageBrokerServlet-->
?<servlet>
??<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
??<servlet-class>
???org.springframework.web.servlet.DispatcherServlet
??</servlet-class>
??<init-param>
???<param-name>contextConfigLocation</param-name>
???<param-value><!-- 配置文件路径 -->
????classpath:Application_BlazeDS.xml
???</param-value>
??</init-param>
?</servlet>
?<servlet-mapping>
??<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
??<url-pattern>/messagebroker/*</url-pattern>
?</servlet-mapping>
?
?<!-- spring容器的监听器 -->
?<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>
?
?
?<welcome-file-list>
??<welcome-file>index.html</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">
?<fx:Script>
??<![CDATA[
???import mx.collections.ArrayCollection;
???import mx.controls.Alert;
???import mx.events.FlexEvent;
???import mx.rpc.events.FaultEvent;
???import mx.rpc.events.ResultEvent;
???
???protected function helloWorld_resultHandler(event:ResultEvent):void
???{
????Alert.show(event.result.toString());
???}
???
???protected function helloWorld_faultHandler(event:FaultEvent):void
???{
????Alert.show(event.fault.toString()+"获取Hello World失败");
???}
???
???
???protected function button1_clickHandler(event:MouseEvent):void
???{
????helloWorld.sayHello();
???}
???
???
???protected function button2_clickHandler(event:MouseEvent):void
???{
????userFacade.findAllUsers();
???}
???
???private var users:ArrayCollection = new ArrayCollection();
???protected function userFacade_resultHandler(event:ResultEvent):void
???{
????users = event.result as ArrayCollection;
????Alert.show(users.length.toString());
???}
???
???
???protected function userFacade_faultHandler(event:FaultEvent):void
???{
????Alert.show(event.fault.toString()+"获取用户失败");
???}
???
??]]>
?</fx:Script>
?<fx:Declarations>
??<!-- 将非可视元素(例如服务、值对象)放在此处 -->
??<s:RemoteObject id="helloWorld" destination="helloFacade" result="helloWorld_resultHandler(event)" fault="helloWorld_faultHandler(event)" />
??<s:RemoteObject id="userFacade" destination="userFacade" result="userFacade_resultHandler(event)" fault="userFacade_faultHandler(event)" />
?</fx:Declarations>
?<s:Button x="175" y="123" label="Hello World1" click="button1_clickHandler(event)"/>
?<s:Button x="304" y="123" label="Hello World2" click="button2_clickHandler(event)"/>
</s:Application>
?
Application_BlazeDS.xml文件(不必使用remoting-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"
?xmlns:aop="http://www.springframework.org/schema/aop"
?xsi:schemaLocation="http://www.springframework.org/schema/beans
?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
?http://www.springframework.org/schema/flex
?http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
?http://www.springframework.org/schema/aop
?http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
?<!-- 此文件用来配置facade,即flex跟后台交互destination -->
?<flex:message-broker />
?<bean id="helloFacade" ref="userDAO"></property>
?</bean>
?
</beans>
3.WebServices
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
?<mx:WebService id="ws" wsdl="http://localhost:8080/webService/services/CompanyService?wsdl" useProxy="false" showBusyCursor="true">
??<mx:operation name="putCompany" result="Alert.show('恭喜!添加成功','返回框',3)" fault="Alert.show('添加失败','失败',3)">
???
??</mx:operation>
?<mx:operation name="getCompanyCEO" result="showInfo(event)" fault="Alert.show('失败','tile',2)">
???
??</mx:operation>?
?</mx:WebService>
?<mx:XML>
??
?</mx:XML>
?<mx:Script>
??<![CDATA[
???import mx.rpc.events.ResultEvent;
???import mx.controls.Alert;
???private var com:String ;
???private var cio:String ;
???
???public function addCompany():void {
????com = company.text ;
????cio = ceo.text ;
????ws.putCompany(com,cio);
???}
???
???public function seeCeo():void {
????var str:String;
????str = company.text;
????ws.getCompanyCEO(str);
???}
???
???public function showInfo(event:ResultEvent):void {
????var str:String;
????str = event.result.toString();
????
????Alert.show(str,"凯旋",2);
???}
??]]>
?</mx:Script>
?
?<mx:Panel title="DataGrid Control Example" height="100%" width="100%"
??????? paddingTop="10" paddingLeft="10" paddingRight="10">
??<mx:Label x="10" y="10" text="公司" height="39" width="55" fontSize="21" fontWeight="bold"/>
??<mx:TextInput x="61" y="10" height="31" id="company" fontSize="20"/>
??<mx:Label x="10" y="66" text="老大" width="55" height="49" fontSize="21" fontWeight="bold"/>
??<mx:TextInput x="61" y="75" height="31" id="ceo" fontSize="20"/>
??<mx:Button label="添加" height="37" fontSize="17" id="add" click="addCompany();"/>
??<mx:Button? label="查看(输入公司查老大)" height="29" fontSize="17" id="see" click="seeCeo();"/>
??
?</mx:Panel>
</mx:Application>
?
不需要进行remoting-config.xml配置。