Flex与Java通讯之RemoteObject方式

在学习了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/

相关文章
相关标签/搜索