在學習了flash中的事件機制後,咱們就開始學習flex與Java中的3種通訊方式。Flex與Java通訊有3中方式: web
●flex訪問Java普通類使用RemoteObject方式,這也是用的最多的一種方式。 服務器
●flex訪問Java服務器類(如servlet)用HttpService方式。 學習
●Flex與WebService交互用WebService方式。 flex
今天就先來學習flex訪問普通Java類。在學習以前咱們須要考慮這麼一個問題:因爲咱們這3個例子都是以登錄爲例子進行的,因此儘可能讓登錄界面分離出來能夠重複使用,這用到Flex中的module。咱們怎麼將module中的數值傳到父窗口去呢?咱們又想到上節中學習的自定義事件。好了,既然想通了,就開始今天的學習。 ui
將MyEclipse切換到MyEclipse視圖,新建一個與flex交互的普通Java類,代碼以下所示: this
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.it161.test;
public class RemoteObjectDemo {
public boolean login(String username,String passworld ){
if(username.equals("admin")&&passworld.equals("123")){
returntrue;
}else{
returnfalse;
}
}
}
|
在WebRoot/WEB-INF/flex目錄下的remoting-config.xml文件中添加以下所示代碼: spa
1
2
3
4
5
|
<destination id="remoteObjectDemo">
<properties>
<source>com.yqsn.test.RemoteObjectDemo</source>
</properties>
</destination>
|
將MyEclipse切換到Flash視圖,首先自定義一個事件類LoginEvent.as,爲之後傳值服務,代碼以下所示: component
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.it161.ases
{
import flash.events.Event;
public class LoginEvent extends Event
{
public static const LOGIN_EVENT:String="LOGIN_EVENT";
private var _loginMess:Object;
public function LoginEvent(type:String,loginMess:Object=null, bubbles:Boolean=false,cancelable:Boolean=false)
{
this._loginMess=loginMess;
super(type, bubbles, cancelable);
}
public functionget loginMess():Object
{
return _loginMess;
}
public functionset loginMess(value:Object):void
{
_loginMess = value;
}
}
}
|
在這個類中咱們定義了一個事件類型LOGIN_EVENT,定義了一個Object類型的變量,用於存值。 orm
接着新建一個登錄信息的VO類LoginMess.as,爲之後存貯用戶信息服務,代碼以下所示: xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package com.it161.ases
{
publicclass LoginMess
{
private var _username:String;
privatevar _passworld:String;
publicfunction LoginMess()
{
}
publicfunctionget passworld():String
{
return _passworld;
}
publicfunctionset passworld(value:String):void
{
_passworld = value;
}
publicfunctionget username():String
{
return _username;
}
publicfunctionset username(value:String):void
{
_username = value;
}
}
}
|
新建一個登錄界面,新建一個MXMLModule文件LoginModule.mxml,代碼以下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
<?xmlversion="1.0" encoding="utf-8"?>
<s:Module xmlns:fx="
http://ns.adobe.com/mxml/2009"
xmlns:s="
library://ns.adobe.com/flex/spark"
xmlns:mx="
library://ns.adobe.com/flex/mx" width="256" height="213">
<fx:Script>
<![CDATA[
import com.flex.ases.LoginEvent;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
protectedfunction login_clickHandler(event:MouseEvent):void
{
// TODOAuto-generated method stub
var loginMess:Object=new Object;
loginMess.username=userName.text;
loginMess.passworld=passworld.text;
if(userName.text=="" ||passworld.text==""){
Alert.show("用戶名或密碼不能爲空!");
return;
}
this.dispatchEvent(newLoginEvent(LoginEvent.LOGIN_EVENT,loginMess));
userName.text="";
passworld.text="";
PopUpManager.removePopUp(this);
}
protectedfunction loginTitleWindow_closeHandler(event:CloseEvent):void
{
// TODO Auto-generatedmethod stub
userName.text="";
passworld.text="";
PopUpManager.removePopUp(this);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visualelements (e.g., services, value objects) here -->
</fx:Declarations>
<s:TitleWindow x="1" y="1" width="256"height="213" title="登錄"id="loginTitleWindow" close="loginTitleWindow_closeHandler(event)" >
<s:Form width="100%" height="183" >
<s:FormItem left="60" height="39" width="224" label="用戶名" required="true" >
<s:TextInput id="userName" />
</s:FormItem>
<s:FormItem required="true" width="224" label="密碼" >
<s:TextInput id="passworld" displayAsPassword="true" />
</s:FormItem>
<s:FormItem width="227">
<s:Button id="login" label="登錄" click="login_clickHandler(event)"/>
</s:FormItem>
</s:Form>
</s:TitleWindow>
</s:Module>
|
這個頁面之後咱們反覆使用,這就是module文件的優勢之一。在這個頁面中咱們不處理與Java交互的部分,由於既然是公共頁面,咱們應該將於Java交互的部分放在相應引用的文件中。
接着建立主頁面RemoteObjectDemo.mxml,代碼以下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
<?xml version="1.0"encoding="utf-8"?>
<s:Application xmlns:fx="
http://ns.adobe.com/mxml/2009"
xmlns:s="
library://ns.adobe.com/flex/spark"
xmlns:mx="
library://ns.adobe.com/flex/mx" width="100%" height="100%">
<fx:Script>
<![CDATA[
import com.flex.ases.LoginEvent;
import com.flex.ases.LoginMess;
import com.flex.component.LoginTitleWindow;
import com.flex.module.LoginModule;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
privatevar loginMess:LoginMess=new LoginMess();
privatevar loginModule:LoginModule=new LoginModule();
protectedfunction login_clickHandler(event:MouseEvent):void
{
PopUpManager.addPopUp(loginModule,this,true);
PopUpManager.centerPopUp(loginModule);
loginModule.addEventListener(LoginEvent.LOGIN_EVENT,getLoginMess);
}
publicfunction getLoginMess(event:LoginEvent):void{
var username:String=event.loginMess['username'];
var passworld:String=event.loginMess['passworld'];
loginMess.username=username;
remoteObj.login(username,passworld);
}
protectedfunction remoteObj_resultHandler(event:ResultEvent):void
{
// TODOAuto-generated method stub
var str:Boolean=event.result as Boolean;
if(str){
Alert.show(loginMess.username+",歡迎您回來...","提示");
aaa.text=loginMess.username+",歡迎歸來...";
bbb.text="";
login.label="";
}else{
Alert.show("登陸失敗,您輸入的用戶名或者密碼不存在!","提示");
}
}
protectedfunction remoteObj_faultHandler(event:FaultEvent):void
{
// TODOAuto-generated method stub
Alert.show(event.fault.message,"出錯了");
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visualelements (e.g., services, value objects) here -->
<s:RemoteObject id="remoteObj" destination="remoteObjectDemo"result="remoteObj_resultHandler(event)"fault="remoteObj_faultHandler(event)" />
</fx:Declarations>
<s:Label x="219" y="150" width="182" height="27" fontSize="18" id="aaa" text="您尚未登錄,如今就" verticalAlign="middle"/>
<mx:LinkButton x="409" y="150" width="57" height="27" label="登錄" id="login" fontSize="18"click="login_clickHandler(event)"/>
<s:Label x="478" y="150" width="37" height="27" id="bbb" fontSize="18" text="吧!" verticalAlign="middle"/>
</s:Application>
|
好了,頁面與類算是處理完了,打開服務器並部署項目,運行felx頁面RemoteObjectDemo.mxml,以下所示:
當咱們點擊「登錄」按鈕後,彈出module頁面,以下所示:
當咱們輸入的用戶名和密碼都正確時則提示你登錄正確:
輸入錯誤則提示你輸入不正確:
能夠看出,咱們輸入的用戶名與密碼與Java中的login方法進行了交互。
好了,就學習這麼多,下節將學習HttpService方式。
原創文章,轉載請註明出處:http://www.it161.com/article/webDetail?articleid=140111224840
更多原創內容,請訪問:http://www.it161.com/