Flex與Java中的Object交互
Flex與Java中的Object進行交互,需要準備三個內(nèi)容,一個是Flex主文件,也就是我們的mxml文件,兩個Java類,一個remoting-config.xml。為了減少程序的復雜性,這里只證明Flex可以調(diào)用 Java中Object進行交互,不進行與數(shù)據(jù)庫的交互。
新建一個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接口,否則將導致異常的發(fā)生。但是經(jīng)過筆者測試,這個Serializable是可選的,即使不繼承,也不會出現(xiàn)什么問題,可能是由于flex的版本所決定的吧。
再創(chuàng)建一Java對象:IntroduceOneself.java,供Flex調(diào)用, 類中 以SpeakUser作為參數(shù)。代碼如下:
- 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()
- + "來訪問您,請問您是否要接見?";
- }
- }
然后創(chuàng)建一個ActionScript類:SpeakUser.as, 用于接受實體類SpeakUser.java的返回值,這里的[Bindable][RemoteClass]是和java代碼中的 SpeakUser.java類關(guān)聯(lián)起來。這樣利用類型轉(zhuǎn)換將java對象轉(zhuǎn)換成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,在其中調(diào)用java類,代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns: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:Button label="JAVA Object + FLEX 通信" click="remotingHelloJavaFlex();" x="142" y="234"
- fontSize="12" width="209"/>
- <mx:Label text="姓名" x="121" y="55" fontSize="15"/>
- <mx:TextInput id="nameInputTxt" x="170" y="55"/>
- <mx:Label x="121" y="103" 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>
在這里,F(xiàn)lex是通過AMF協(xié)議與Java對象進行交互的,其中endpoint定義了影射文件的消息協(xié)議,這個文件來自/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"?>
- <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="someOneComeDes">
- <properties>
- <source>
- com.cx.action.IntroduceOneself
- </source>
- </properties>
- </destination>
- </service>
原文鏈接:http://blog.csdn.net/shehun1/article/details/6691462
【編輯推薦】
- Java 8整裝待發(fā) 圖謀云計算
- Java 7是蜜糖還是毒藥?
- 選用Ibatis和Hibernate的區(qū)別
- JRuby和Java 7 我們可以期待什么
- Flex前端與Java服務端交互,反射機制挑大旗