Flex remoteobject Rewriting

Source: Internet
Author: User

I. Basic knowledge about flex remoteobject
you can use the flex remoteobject component to call methods for ColdFusion components or Java classes.
the remoteobject component uses the AMF protocol to send and receive data, while the WebService and httpservice components use the HTTP protocol. AMF is significantly faster than HTTP, but server-side encoding and configuration are usually more complex.
similar to httpservice and WebService components, you can use the remoteobject component to display database query results in the Program application. You can also use this component to insert, update, and delete data in the database. After the query result is returned to the application, it can be displayed in one or more user interface controls.
2. Rewrite remoteobject
my working language is mainly Java, here the remoteobject background service program is written in Java, the traditional remoteobjec writing is, in the server configuration file remoting-config.xml wrote:

XMLCode:

 1     <  Destination  ID  = "Mogindemo"  >  
2 < Properties >
3 < Source > Bean. mogindemo </ Source >
4 < Scope > Application </ Scope >
5 </ Properties >
6 </ Destination >

Flex code:

 
<Mx: remoteobject id = "Ro" Destination = "logindemo" fault = "alert. Show (event. fault. tostring ()">

My framework is using Flex + spring + ibatis
After retransformation:
Add a new configuration file with the following content:

 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 >

In this way, the flex server communication configuration file is compatible with spring. The specific configuration uses the spring annotation function, which is written into a Java file, for example:

@ Transactional @ Service ("usergroupservice") @ remotingdestination (channels = {"My-AMF", "My-secure-AMF "}) public class usergroupserviceimpl implements usergroupservice {@ autowired @ qualifier ("usergroupdao") Private usergroupdao; @ autowired @ qualifier ("userdao") Private userdao; @ overridepublic 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 ;}@ overridepublic 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 ;}@ overridepublic 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 ;}@ overridepublic 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"}) add this to each service class.

In Flex, we wrote servicefactory. As, the Code is as follows:

 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), "error") ;}}

The calling method in Flex is:

ActionScript code:

 
Private var usergroupservice: remoteobject = servicefactory. getservice ('usergroupservice ');






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.