彩票走势图

Flex与Java中的Object交互

转帖|其它|编辑:郝浩|2011-08-24 15:51:36.000|阅读 392 次

概述:Flex与Java中的Object进行交互,需要准备三个内容,一个是Flex主文件,也就是我们的mxml文件,两个Java类,一个 remoting-config.xml.为了减少程序的复杂性,这里只证明Flex可以调用 Java中Object进行交互,不进行与数据库的交互.

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

  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:Application xmlns:mx="//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:Button label="JAVA Object + FLEX 通信"
 click="remotingHelloJavaFlex();" x="142" y="234"

  fontSize="12" width="209"/>

  <mx:Label text=&quot;姓名" x="121" y="55" fontSize="15"/>

  <mx:TextInput id="nameInputTxt" x="170" y="55"/>

  <mx:Label x="121" y="103&quot; fontSize="15" text="密码:"/>

  <mx:TextInput id = "txtPwd" x="170" y="106"/>

  <mx:Label x="121" y="145" fontSize="15" text="电话:"/>

  <mx:Label x="121" y="187" fontSize="15" text="邮箱:"/>

  <mx:TextInput id="txtTel" x="170" y="148"/>

  <mx:TextInput id="txtUserMail" x="170" y="190"/>

<mx:RemoteObject destination="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&quot;>

  <endpoint url="//{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";?> <service id="remoting-service"

  class="flex.messaging.services.RemotingService">

  <adapters>

  <adapter-definition id="java-object&quot;

class="flex.messaging.services.remoting.adapters.JavaAdapter" default=";true"/>

  </adapters>

  <default-channels>

  <channel ref="my-amf"/>

  </default-channels>

  <destination id=&quot;someOneComeDes">

  <properties>

  <source>

  com.cx.action.IntroduceOneself

  </source>

  </properties>

  </destination>

  </service>


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP