如何使用mx:RemoteObject
2014-03-06 10:27
435 查看
第一步:创建flex项目时指明项目使用远程数据调用,并指明远程服务的contextroot和服务器位置
第二步:在Flex组件中定义RemoteObject
<mx:RemoteObject id="freshQuotes" destination="Portfolio" fault="onFault(event);">
// id用于被flex中的其他方法调用的引用
//destination 与 remoting-config.xml中的目标配置一致
//fault指定远程调用错误的回调方法
<mx:method name="getQuotes" concurrency="last" mx:method指明远程对象的被调用方法
result="onResult(event);" result指定结果返回的回调方法
/>
</mx:RemoteObject>
第三步:编写Java服务端对象,简单对象
第四步:配置remoting-config.xml
<destination id="Portfolio"> id与RemoteObject标签中的destination一致
<properties>
<source>com.favzone.action.Portfolio</source> 给出服务端类路径
</properties>
</destination>
第五步:部署web应用,该web应用要含有编译好的swf文件
===============================================================================
Java代码
package com;
public class LoginDemo {
public String validate(String username,String password){
String message ="login failed!";
if(username.equals("lin")&&password.equals("lin")){
message = "login successed!";
}
return message;
}
}
2.remoting-config.xml
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService">
<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<destination id="login">
<properties>
<source>com.LoginDemo</source>
</properties>
</destination>
</service>
3.mxml
Java代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
[Bindable]
var returnValue:String;
var username1:String;
var password1:String;
function sendRequest():void{
username1=username.text;
password1=password.text;
ro.validate(username1,password1);
ro.addEventListener(ResultEvent.RESULT,results);
}
function results(event:ResultEvent):void{
returnValue=event.result as String;
}
function faultHandler(event:FaultEvent):void{
Alert.show(event.fault.toString());
}
]]>
</mx:Script>
<mx:RemoteObject id="ro" destination="login" fault="faultHandler(event)">
</mx:RemoteObject>
<mx:Panel height="400" width="400" layout="absolute" title="用户登录">
<mx:Label x="50" y="50" text="用户名" width="50"></mx:Label>
<mx:Label x="50" y="75" text="密码" width="50"></mx:Label>
<mx:TextInput id="username" x="75" y="50"/>
<mx:TextInput id="password" x="75" y="75"/>
<mx:Button x="50" y="100" label="登录" click="sendRequest()"/>
<mx:Label x="50" y="130" text="{returnValue}"/>
</mx:Panel>
</mx:Application>
第二步:在Flex组件中定义RemoteObject
<mx:RemoteObject id="freshQuotes" destination="Portfolio" fault="onFault(event);">
// id用于被flex中的其他方法调用的引用
//destination 与 remoting-config.xml中的目标配置一致
//fault指定远程调用错误的回调方法
<mx:method name="getQuotes" concurrency="last" mx:method指明远程对象的被调用方法
result="onResult(event);" result指定结果返回的回调方法
/>
</mx:RemoteObject>
第三步:编写Java服务端对象,简单对象
第四步:配置remoting-config.xml
<destination id="Portfolio"> id与RemoteObject标签中的destination一致
<properties>
<source>com.favzone.action.Portfolio</source> 给出服务端类路径
</properties>
</destination>
第五步:部署web应用,该web应用要含有编译好的swf文件
===============================================================================
Java+Flex整合应用简单示例(mx:RemoteObject)
1.javaJava代码
package com;
public class LoginDemo {
public String validate(String username,String password){
String message ="login failed!";
if(username.equals("lin")&&password.equals("lin")){
message = "login successed!";
}
return message;
}
}
2.remoting-config.xml
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService">
<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<destination id="login">
<properties>
<source>com.LoginDemo</source>
</properties>
</destination>
</service>
3.mxml
Java代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
[Bindable]
var returnValue:String;
var username1:String;
var password1:String;
function sendRequest():void{
username1=username.text;
password1=password.text;
ro.validate(username1,password1);
ro.addEventListener(ResultEvent.RESULT,results);
}
function results(event:ResultEvent):void{
returnValue=event.result as String;
}
function faultHandler(event:FaultEvent):void{
Alert.show(event.fault.toString());
}
]]>
</mx:Script>
<mx:RemoteObject id="ro" destination="login" fault="faultHandler(event)">
</mx:RemoteObject>
<mx:Panel height="400" width="400" layout="absolute" title="用户登录">
<mx:Label x="50" y="50" text="用户名" width="50"></mx:Label>
<mx:Label x="50" y="75" text="密码" width="50"></mx:Label>
<mx:TextInput id="username" x="75" y="50"/>
<mx:TextInput id="password" x="75" y="75"/>
<mx:Button x="50" y="100" label="登录" click="sendRequest()"/>
<mx:Label x="50" y="130" text="{returnValue}"/>
</mx:Panel>
</mx:Application>
相关文章推荐
- Objective-C Method Swizzling
- ArcGIS Runtime SDK for iOS开发系列教程(0)——PPT分享(iOS历史、Objective-C语法、SDK介绍)
- HTML Object收集
- 7.1 第二块,第一节 objective-c
- 7.1 例子 objective-c
- objective-c static变量的使用总结
- iOS开发(Objective-C)常用库索引
- Objective-C之Foundation中常见的结构体
- Objective-c中的NSSet的用法及沙盒机制的用法
- javafx-binding object model to the tableview
- Objective-c官方文档 怎么自定义类
- AbstractList.equals(Object o)
- 01-IOSCore - NSString、NSFileManager、NSBundle、StringAndObjectConvert
- iOS中id与NSObject* 和id<NSObject>的区别
- Java中Object的方法
- objective-c 单例模式详解
- 深入研究java.lang.Object类
- Objective-C函数速查实例手册随书黑苹果配置教程
- 关于Objective C的私有函数
- Objective C运行时(runtime)技术的几个要点总结