技术开发 频道

Flex与Java中的Object交互

 修改Flex主文件:BlazObject.mxml,在其中调用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>

      在这里,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"?>  
<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>  
0
相关文章