一、Java服务端类源码
1、User类源码
package com.cjm.flex; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2、RemoteService类源码
package com.cjm.flex; public class RemoteService { //将Java Bean传递到Flex客户端 public User getUser(){ User user = new User(); user.setUsername("cjm11111"); user.setPassword("pwd11111"); return user; } //获取从Flex客户端传递过来的Bean对象 public void saveUser(User user){ System.out.println("显示:" + user.getUsername() + ", " + user.getPassword()); } }
二、配置信息
1、在applicationContext.xml文件中配置Spring Bean
<bean id="remoteService" class="com.cjm.flex.RemoteService"/>
2、在remoting-config.xml文件中配置接收点
<destination id="remoteService"> <properties> <factory>spring</factory> <source>remoteService</source> </properties> </destination>
三、AS类的源码
1、User类的源码。
1)文件结构必须要与Java服务端的User类一致,即都要包含username、password两个属性的getter、setter方法。
2)在AS的User类中,必须通过metadata关键字RemoteClass指定其对应的服务端Java类。
package com.cjm.flex{ [RemoteClass(alias="com.cjm.flex.User")] public class User{ private var _username:String; private var _password:String; public function get username():String{ return this._username; } public function set username(uid:String):void{ this._username = uid; } public function get password():String{ return this._password; } public function set password(pwd:String):void{ this._password = pwd; } } }
四、mxml文件部分源码
1、从服务端获取Bean对象
private function call():void{ var o:Operation = ro.getOperation("getUser") as Operation; o.addEventListener(ResultEvent.RESULT, resultEvent); o.send(); } private function resultEvent(event:ResultEvent):void{ var user:User = event.result as User; try{ txt1.text = user.username + ", " + user.password; }catch(e:Error){ txt1.text = e.message; } }
2、将AS对象传递到服务端
private function call():void{ var user:User = new User(); user.username = "jm"; user.password = "123"; var o:Operation = ro.getOperation("saveUser") as Operation; o.addEventListener(ResultEvent.RESULT, resultEvent); o.send(user); }