Flex-rpc-services
フレックス-RPCサービス
Flexは、サーバー側のデータをクライアント側に提供するRPCサービスを提供します。 Flexは、サーバー側のデータをかなり制御します。
- Flex RPCサービスを使用して、サーバー側で実行されるユーザーアクションを定義できます。
- Flex RPC Sservicesは、あらゆるサーバー側テクノロジーと統合できます。 *Flex RPCサービスの1つは、圧縮されたバイナリデータをネットワーク経由で転送するための組み込みサポートを提供し、非常に高速です。
Flexは、次の3種類のRPCサービスを提供します
Sr.No | RPC Service & Description |
---|---|
1 |
<mx:HTTPService>タグは、MXMLファイル内のHTTPServiceオブジェクトを表すために使用されます。 HTTPServiceオブジェクトのsend()メソッドを呼び出すと、指定されたURLに対してHTTPリクエストが行われ、HTTPレスポンスが返されます。HTTPHEAD、OPTIONS、TRACE、およびDELETEメソッドも使用できます。 |
2 |
WebService <mx:WebService>は、SOAP準拠のWebサービスの操作へのアクセスを取得するために使用されます。 |
3 |
RemoteObject <mx:RemoteObject>タグは、MXMLファイル内のHTTPServiceオブジェクトを表すために使用されます。 このタグにより、Action Message Format(AMF)エンコードを使用してJavaオブジェクトのメソッドにアクセスできます。 |
HTTPサービスについて詳しく説明します。 サーバーに配置されたXMLソースファイルを使用し、HTTPサービス経由でクライアント側でアクセスします。
Items.xml
<items>
<item name = "Book" description = "History of France"></item>
<item name = "Pen" description = "Parker Pen"></item>
<item name = "Pencil" description = "Stationary"></item>
<items>
HTTPService宣言
HTTPServiceを宣言し、上記のファイルのURLを渡します
<fx:Declarations>
<mx:HTTPService id = "itemRequest"
url = "http://www.finddevguides.com/flex/Items.xml"/>
</fx:Declarations>
RPCコール
itemRequest.send()メソッドを呼び出し、itemRequest WebサービスのlastResultオブジェクトの値をFlex UIコンポーネントにバインドします。
...
itemRequest.send();
...
<mx:DataGrid id = "dgItems" height = "80%" width = "75%"
dataProvider = "{itemRequest.lastResult.items.item}">
<mx:columns>
<mx:DataGridColumn headerText = "Name" dataField = "name"/>
<mx:DataGridColumn headerText = "Description" dataField = "description"/>
</mx:columns>
</mx:DataGrid>
RPCサービスコールの例
次に、FlexアプリケーションでRPCサービスをテストする手順に従います-
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a package com.finddevguides.client as explained in the Flex - Create Application chapter. |
2 | Modify HelloWorld.mxml as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to make sure business logic is working as per the requirements. |
以下は、変更されたmxmlファイル src/com.finddevguides/HelloWorld.mxml の内容です。
<?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 = "500" minHeight = "500" creationComplete = "init(event)">
<fx:Style source = "/com/finddevguides/client/Style.css"/>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function init(event:FlexEvent):void {
itemRequest.send();
}
]]>
</fx:Script>
<fx:Declarations>
<mx:HTTPService id = "itemRequest"
url = "http://www.finddevguides.com/flex/Items.xml"/>
</fx:Declarations>
<s:BorderContainer width = "630" height = "480" id = "mainContainer"
styleName = "container">
<s:VGroup width = "100%" height = "100%" gap = "10"
horizontalAlign = "center" verticalAlign = "middle">
<s:Label id = "lblHeader" text = "RPC Service Demonstration"
fontSize = "40" color = "0x777777" styleName = "heading"/>
<s:Panel id = "parentPanel" title = "Using RPC Services"
width = "500" height = "200" >
<s:layout>
<s:VerticalLayout gap = "10"
verticalAlign = "middle" horizontalAlign = "center"/>
</s:layout>
<mx:DataGrid id = "dgItems" height = "80%" width = "75%"
dataProvider = "{itemRequest.lastResult.items.item}">
<mx:columns>
<mx:DataGridColumn headerText = "Name" dataField = "name"/>
<mx:DataGridColumn headerText = "Description"
dataField = "description"/>
</mx:columns>
</mx:DataGrid>
</s:Panel>
</s:VGroup>
</s:BorderContainer>
</s:Application>
すべての変更が完了したら、link:/flex/flex_create_application [Flex-アプリケーションの作成]の章で行ったように、アプリケーションを通常モードでコンパイルして実行します。 アプリケーションに問題がなければ、次の結果が得られます。[link:/flex/samples/RPCApplicationl [オンラインで試す]]