Flex与Java中的Object进行交互,需要准备三个内容,一个是Flex主文件,也就是我们的mxml文件,两个Java 类,一个remoting-config.xml。为了减少程序的复杂性,这里只证明Flex可以调用Java中Object进行交互,不进行与数据库的交互。
新建一个Java实体类:SpeakUser.java, 如下:
package com.cx.model;
import java.io.Serializable;
public class SpeakUser implements Serializable{
private static final long serialVersionUID = -4154296753553491429L;
private String userName;
private String userPwd;
private String userTel;
private String userMail;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getUserTel() {
return userTel;
}
public void setUserTel(String userTel) {
this.userTel = userTel;
}
public String getUserMail() {
return userMail;
}
public void setUserMail(String userMail) {
this.userMail = userMail;
}
}
有的作者说这个实体类需要当做Java代码中方法的返回值传递给Flex中的AS代码,所以需要继承Serializable接口,否则将导致异常的发生。但是经过笔者测试,这个Serializable是可选的,即使不继承,也不会出现什么问题,可能是由于flex的版本所决定的吧。
再创建一Java对象:IntroduceOneself.java,供Flex调用, 类中 以SpeakUser作为参数。代码如下:
package com.cx.action;
import com.cx.model.*;
public class IntroduceOneself {
public String speak(SpeakUser user){
System.out.println("Hello, My Name is: " + user.getUserName());
System.out.println("My telephone is: " + user.getUserTel());
System.out.println("My Email is: " + user.getUserMail());
return "名字叫:" + user.getUserName() + ", 电话是: "
+ user.getUserTel() + ", 邮箱是: "+user.getUserMail()
+ "来访问您,请问您是否要接见?";
}
}
然后创建一个ActionScript类:SpeakUser.as, 用于接受实体类SpeakUser.java的返回值,这里的[Bindable][RemoteClass]是和java代码中的 SpeakUser.java类关联起来。这样利用类型转换将java对象转换成AS对象。代码如下:
package com.flex.model
{
[Bindable]
[RemoteClass(alias="com.cx.model.SpeakUser")]
public class SpeakUser
{
public var userName:String="";
public var userPwd:String ="";
public var userTel:String ="";
public var userMail:String ="";
public function SpeakUser()
{
}
}
}
修改Flex主文件:BlazObject.mxml,在其中调用java类,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"minWidth="955"minHeight="600">
<mx:Script>
<![CDATA[
import com.flex.model.SpeakUser;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
public function remotingHelloJavaFlex():void
{
var user:SpeakUser = new SpeakUser();
user.userName = nameInputTxt.text;
user.userMail = txtUserMail.text;
user.userPwd = txtPwd.text;
user.userTel = txtTel.text;
someOneCome.speak(user);
someOneCome.addEventListener(ResultEvent.RESULT,getRoHelloRes);
}
private function getRoHelloRes(e:ResultEvent):void{
Alert.show(e.result.toString());
}
private function failed():void{
Alert.show("cuowu");
}
]]>
</mx:Script>
<mx:Buttonlabel="JAVA Object + FLEX 通信"click="remotingHelloJavaFlex();"x="142"y="234"
fontSize="12"width="209"/>
<mx:Labeltext="姓名"x="121"y="55"fontSize="15"/>
<mx:TextInputid="nameInputTxt"x="170"y="55"/>
<mx:Labelx="121"y="103"fontSize="15"text="密码:"/>
<mx:TextInputid= "txtPwd"x="170"y="106"/>
<mx:Labelx="121"y="145"fontSize="15"text="电话:"/>
<mx:Labelx="121"y="187"fontSize="15"text="邮箱:"/>
<mx:TextInputid="txtTel"x="170"y="148"/>
<mx:TextInputid="txtUserMail"x="170"y="190"/>
<mx:RemoteObjectdestination="someOneComeDes"id="someOneCome"endpoint="/BlazDSObject/messagebroker/amf"fault="failed()"/>
</mx:Application>
在这里,Flex是通过AMF协议与Java对象进行交互的,其中endpoint定义了影射文件的消息协议,这个文件来自/WEB-INF/flex /services-config.xml中,影射对象的位置是通过remoting-service.xml影射的代码如下:
<channel-definition id="my-secure-amf"class="mx.messaging.channels.SecureAMFChannel">
<endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/amfsecure"class="flex.messaging.endpoints.SecureAMFEndpoint"/>
<properties>
<add-no-cache-headers>false</add-no-cache-headers>
</properties>
</channel-definition>
remoting-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<serviceid="remoting-service"
class="flex.messaging.services.RemotingService">
<adapters>
<adapter-definitionid="java-object"class="flex.messaging.services.remoting.adapters.JavaAdapter"default="true"/>
</adapters>
<default-channels>
<channelref="my-amf"/>
</default-channels>
<destinationid="someOneComeDes">
<properties>
<source>
com.cx.action.IntroduceOneself
</source>