Flex的RemoteObject改写

一、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代码:

1
<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文件里,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@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代码:

1
private var userGroupService:RemoteObject = ServiceFactory.getService('userGroupService');

  


  





posted @   夏天的森林  阅读(2240)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示