第一步:创建flex项目时指明项目使用远程数据调用,并指明远程服务的contextroot和服务器位置
第二步:在Flex组件中定义RemoteObject
<mx:RemoteObject id="freshQuotes" destination="Portfolio" fault="on
// id用于被flex中的其他方法调用的引用
//destination 与 remoting-config.xml中的目标配置一致
//fault指定远程调用错误的回调方法
<mx:method name="getQuotes" concurrency="last" mx:method指明远程对象的被调用方法
result="on
/>
</mx:RemoteObject>
第三步:编写Java服务端对象,简单对象
第四步:配置remoting-config.xml
<destination id="Portfolio"> id与RemoteObject标签中的destination一致
<properties>
<source>com.favzone.act
</properties>
</destination>
第五步:部署web应用,该web应用要含有编译好的swf文件
===============================================================================
Java+Flex整合应用简单示例(mx:RemoteObject)
1.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
- <?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
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
- <mx:Script>
- <![CDATA[
- imp
ort mx.rpc.events.FaultEvent; - imp
ort mx.rpc.events.ResultEvent; - imp
ort 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>