一、Flex的RemoteObject的基础知识
可以使用 Flex RemoteObject 组件来针对 ColdFusion 组件或 Java 类调用方法。
RemoteObject 组件使用 AMF 协议发送和接收数据,而 WebService 和 HTTPService 组件使用 HTTP 协议。AMF 显著快于 HTTP,但服务器端编码和配置通常更复杂。
与 HTTPService 和 WebService 组件一样,您可以使用 RemoteObject 组件在应用程序中显示数据库查询结果。也可以使用该组件在数据库中插入、更新和删除数据。在将查询结果返回到应用程序之后,可以将其显示在一个或多个用户界面控件中。
二、对RemoteObject的改写
我的工作语言主要是java,这里使用RemoteObject的后台服务程序就是用java编写,传统的RemoteObjec的写法是,在服务端里的配置文件remoting-config.xml里写道:
<destination id="MoginDemo">
<properties>
<source>bean.MoginDemo </source>
<scope>application</scope>
</properties>
</destination>
<mx:RemoteObject id="ro" destination="LoginDemo" fault="Alert.show(event.fault.toString())">
我的框架是使用flex+spring+ibatis
重新改造后:
添加一个新的配置文件,内容如下:
<?xml version="1.0" encoding="GB2312" ?>
<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">
<!-- Custom exception translator configured as a Spring bean -->
<bean id="myExceptionTranslator" class="common.MyServerException" />
<!-- Bootstraps and exposes the BlazeDS MessageBroker simplest form -->
<flex:message-broker id="_messageBroker"
services-config-path="/WEB-INF/flex/services-config.xml">
<flex:mapping pattern="/messagebroker/*" />
<flex:exception-translator ref="myExceptionTranslator" />
</flex:message-broker>
<!--
another configuration that Bootstraps and exposes the BlazeDS
MessageBroker <bean id="mySpringManagedMessageBroker"
class="org.springframework.flex.core.MessageBrokerFactoryBean">
<property name="servicesConfigPath"
value="classpath*: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>
</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里的调用方法是:
private var userGroupService:RemoteObject = ServiceFactory.getService('userGroupService');