---
title: "Flex4系列二：Flex4和Java通信的简单应用"
author: "HelloTommy"
date: "2011-03-06T15:46:37Z"
tags: []
categories: ["Web前端"]
slug: "flex4-flex4-java"
draft: false
lastmod: "2011-03-06T15:46:37Z"
id: 942671
url: "https://hellotommy.iteye.com/blog/942671"
---

搞了好久，感觉是插件安装的问题，因为我的问题比较奇怪，下一篇文章写写问题的解决方案。

        这篇写一个简单的demo。flex4和java通信是必须的。

        一：java类：

             

```
package test;

//flex4调用的java类
public class SayHello {
	public String sayHelloToYou(String name){
		return "hello,"+name;
	}
}
```

         二：webRoot目录下导入blazeds的war解压以后的文件，导入后如图。当然可以手动加入，不过最好是能满足这个结构，方便另外一些配置。

          
![](http://dl.iteye.com/upload/attachment/430383/3aeb49f3-cfa0-39aa-b0d0-a09eb751b5a9.jpg)

 

        三：部署到服务器，然后启动服务器，这个很重要，因为在添加flex支持的时候需要检测服务器路径

              然后添加flex的支持,选择BlazeDS

               
![](http://dl.iteye.com/upload/attachment/430385/7b536510-4eae-3aee-8a82-9ec9181a9668.jpg)

 

          点击next，进行BlaszeDS服务器的配置

     
           
![](http://dl.iteye.com/upload/attachment/430399/d6cba0a9-dee4-3c89-b8bd-637dc9739cf4.jpg)  
   
        四：在Blazeds.war中有四个配置文件，demo中只用2个remoting-config.xml和services-config.xml

         remoting-config.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>
    //以下为与java通信    
    <destination id="mytest">
    <properties>
        <source>test.SayHello</source>
    </properties>
 	</destination>

</service>
```

    services-config.xml

       
   

```
<?xml version="1.0" encoding="UTF-8"?>
<services-config>

    <services>
        <service-include file-path="remoting-config.xml" />
    </services>


    <channels>

        <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" 
            	class="flex.messaging.endpoints.AMFEndpoint"/>
            <properties>  
				<polling-enabled>false</polling-enabled>  
			</properties>  
        </channel-definition>

    </channels>

    

</services-config>
```

   

    Flex4Java.mxml

   

```
<?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"  minWidth="1024" minHeight="768">
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			
			protected function myFlex_resultHandler(event:ResultEvent):void
			{
				var name:String=event.result as String;
				Alert.show(name);
			}
			
			
			protected function btnSend_clickHandler(event:MouseEvent):void
			{
				myFlex.sayHelloToYou(txtName.text);
			}
			
		]]>
	</fx:Script>
	<fx:Declarations>
		<mx:RemoteObject id="myFlex" destination="mytest" result="myFlex_resultHandler(event)"/>
	</fx:Declarations>
	<s:Panel x="87" y="20" width="250" height="200" title="hello,everybody!">
		<s:TextInput x="16" y="37" id="txtName"/>
		<s:Button x="168" y="38" label="发送" id="btnSend" click="btnSend_clickHandler(event)"/>
	</s:Panel>
	
</s:Application>
```

   
    五：以上步骤都是在tomcat开着的情况下进行的。

          但是，可能是我插件的问题吧。总是无法自动编译。所以我手动编译了

          编译好以后在项目中会有bin-debug文件夹，里面有与项目同名的flash文件，双击即可运行。

         

        
![](http://dl.iteye.com/upload/attachment/430409/05e3b44b-577c-3771-b902-4e50ec3824d9.jpg)  
        

 

     如果一切顺利的话，最后效果就是：

   

     
![](http://dl.iteye.com/upload/attachment/430415/aaffdf9c-f8b9-3ee9-a409-f9bdedbce744.jpg)

 

 

ok。就这样吧。还有些比较奇怪的问题，请看下回分解！