一、Flex的RemoteObject的基础知识
可以使用 Flex RemoteObject 组件来针对 ColdFusion 组件或 Java 类调用方法。
RemoteObject 组件使用 AMF 协议发送和接收数据,而 WebService 和 HTTPService 组件使用 HTTP 协议。AMF 显著快于 HTTP,但服务器端编码和配置通常更复杂。
与 HTTPService 和 WebService 组件一样,您可以使用 RemoteObject 组件在应用程序中显示数据库查询结果。也可以使用该组件在数据库中插入、更新和删除数据。在将查询结果返回到应用程序之后,可以将其显示在一个或多个用户界面控件中。
二、对RemoteObject的改写
我的工作语言主要是java,这里使用RemoteObject的后台服务程序就是用java编写,传统的RemoteObjec的写法是,在服务端里的配置文件remoting-config.xml里写道:
Xml代码 :
1 <destination id="MoginDemo">
2 <properties>
3 <source>bean.MoginDemo </source>
4 <scope>application</scope>
5 </properties>
6 </destination>
Flex代码:
<mx:RemoteObject id="ro" destination="LoginDemo" fault="Alert.show(event.fault.toString())">
我的框架是使用flex+spring+ibatis
重新改造后:
添加一个新的配置文件,内容如下:
1 <?xml version="1.0" encoding="GB2312" ?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="
5 http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7 http://www.springframework.org/schema/flex
8 http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">
9
10 <!-- Custom exception translator configured as a Spring bean -->
11 <bean id="myExceptionTranslator" class="common.MyServerException" />
12
13
14 <!-- Bootstraps and exposes the BlazeDS MessageBroker simplest form -->
15 <flex:message-broker id="_messageBroker"
16 services-config-path="/WEB-INF/flex/services-config.xml">
17 <flex:mapping pattern="/messagebroker/*" />
18 <flex:exception-translator ref="myExceptionTranslator" />
19 </flex:message-broker>
20
21 <!--
22 another configuration that Bootstraps and exposes the BlazeDS
23 MessageBroker <bean id="mySpringManagedMessageBroker"
24 class="org.springframework.flex.core.MessageBrokerFactoryBean">
25 <property name="servicesConfigPath"
26 value="classpath*:flex/services-config.xml" /> </bean>
27 -->
28 <!-- Maps request paths at /* to the BlazeDS MessageBroker -->
29 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
30 <property name="mappings">
31 <value>
32 /*=_messageBroker
33 </value>
34 </property>
35 </bean>
36 </beans>
这样就把flex的服务器通讯的配置文件和spring兼容。具体的配置使用spring注解的功能,军写入到java文件里,例如:
@Transactional @Service("userGroupService") @RemotingDestination(channels={"my-amf","my-secure-amf"}) public class UserGroupServiceImpl implements UserGroupService { @Autowired @Qualifier("userGroupDao") private UserGroupDao userGroupDao; @Autowired @Qualifier("userDao") private UserDao userDao; @Override public Boolean delUserGroup(Map<String, String> params) { Boolean isSuccess = true; try { userDao.delete(params); userGroupDao.delete(params); } catch (Exception e) { isSuccess = false; e.printStackTrace(); } return isSuccess; } @Override public Map<String, String> getUserGroup(Map<String, String> params) { Map<String, String> userGroup = null; try { userGroup = userGroupDao.get(params); } catch (Exception e) { e.printStackTrace(); } return userGroup; } @Override public List<Map<String, String>> getUserGroupList(Map<String, String> params) { List<Map<String, String>> list = null; try { list = userGroupDao.getList(params); } catch (Exception e) { e.printStackTrace(); } return list; } @Override public Boolean updateUserGroup(Map<String, String> params) { Boolean isSuccess = true; Object userGroupId = params.get("userGroupId"); try { if(userGroupId == null){ userGroupDao.add(params); }else{ userGroupDao.update(params); } } catch (Exception e) { isSuccess = false; e.printStackTrace(); } return isSuccess; } }
@RemotingDestination(channels={"my-amf","my-secure-amf"})每个service类里添加这个即可。
到了Flex这块,我们写了个ServiceFactory.as,代码如下:
package common { import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.messaging.Channel; import mx.messaging.ChannelSet; import mx.messaging.channels.AMFChannel; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.remoting.RemoteObject; import mx.utils.ObjectProxy; public class ServiceFactory { private static var channel:Channel; public static function getService(serviceName:String):RemoteObject { if(channel == null) { channel = new AMFChannel("my-amf", "http://localhost:8080/dispatcher/messagebroker/amf"); } var service:RemoteObject = new RemoteObject(); var cs:ChannelSet = new ChannelSet(); cs.addChannel(channel); service.channelSet = cs; service.destination = serviceName; service.addEventListener(FaultEvent.FAULT,showError); return service; } private static function showError(e:FaultEvent):void { Alert.show(String(e.fault.message), "错误"); } } }
flex里的调用方法是:
Actionscript代码:
private var userGroupService:RemoteObject = ServiceFactory.getService('userGroupService');