java对象不能直接转换为flex对象,所以需要创建一个as对象对应java对象,并且以[RemoteClass(alias="hello.User")]
来指定对应的java对象类。下面我就以一个简单的例子来诠释交互
此例子在上一个例子中稍作改动即可应用
java类
- package hello;
- public class User {
- private long id;
- private String name;
- private String password;
- private long age;
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public long getAge() {
- return age;
- }
- public void setAge(long age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
package hello; public class User { private long id; private String name; private String password; private long age; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public long getAge() { return age; } public void setAge(long age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
as
- package
- {
- [RemoteClass(alias="hello.User")]
- public class User
- {
- public var name:String;
- public var password:String;
- public var age:int;
- public var id:int;
- public function User()
- {
- }
- }
- }
package { [RemoteClass(alias="hello.User")] public class User { public var name:String; public var password:String; public var age:int; public var id:int; public function User() { } } }
mxml
- <?xml version = "1.0" encoding = "utf-8" ?>
- <mx:Application xmlns:mx = "http://www.adobe.com/2006/mxml" >
- <mx:Script >
- <![CDATA[
- import mx.rpc.events.FaultEvent;
- import mx.rpc.events.ResultEvent;
- import mx.controls.Alert;
- [Bindable]
- private var helloResult:String;
- [Bindable]
- private var user:User = new User();
- private function sayHelloTo():void {
- user.name = inputText.text;
- ro.sayHelloTos(user);
- ro.addEventListener(ResultEvent.RESULT,resultHandler);
- }
- private function resultHandler(event:ResultEvent):void {
- user = event.result as User;
- if(null!=user){
- inputName.text=user.name;
- helloResult = user.name;
- }else{
- Alert.show("空值");
- }
- } ]]>
- </mx:Script >
- <mx:RemoteObject id="ro" destination="helloworld" />
- <mx:RemoteObject id="users" destination="user">
- <mx:method name="user">
- </mx:method>
- </mx:RemoteObject>
- <mx:HBox width = "100%">
- <mx:TextInput id = "inputText" name="user.name"/>
- <mx:TextInput id="inputName" name="user.name"/>
- <mx:Button label = "Submit" click = "sayHelloTo()" />
- </mx:HBox >
- <mx:Label text = "{helloResult}" />
- </mx:Application>
<?xml version = "1.0" encoding = "utf-8" ?> <mx:Application xmlns:mx = "http://www.adobe.com/2006/mxml" > <mx:Script > <![CDATA[ import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.controls.Alert; [Bindable] private var helloResult:String; [Bindable] private var user:User = new User(); private function sayHelloTo():void { user.name = inputText.text; ro.sayHelloTos(user); ro.addEventListener(ResultEvent.RESULT,resultHandler); } private function resultHandler(event:ResultEvent):void { user = event.result as User; if(null!=user){ inputName.text=user.name; helloResult = user.name; }else{ Alert.show("空值"); } } ]]> </mx:Script > <mx:RemoteObject id="ro" destination="helloworld" /> <mx:RemoteObject id="users" destination="user"> <mx:method name="user"> </mx:method> </mx:RemoteObject> <mx:HBox width = "100%"> <mx:TextInput id = "inputText" name="user.name"/> <mx:TextInput id="inputName" name="user.name"/> <mx:Button label = "Submit" click = "sayHelloTo()" /> </mx:HBox > <mx:Label text = "{helloResult}" /> </mx:Application>
java服务类
- package hello;
- public class HelloWorld {
- public HelloWorld(){
- }
- public User sayHelloTos(User user) {
- System.out.println(user.getName());
- return user;
- }
- }
package hello;
public class HelloWorld {
public HelloWorld(){
}
public User sayHelloTos(User user) {
System.out.println(user.getName());
return user;
}
}
通信配置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="helloworld">
- <properties>
- <source>hello.HelloWorld</source>
- </properties>
- </destination >
- </service>