flex调用java

flex 调用java配置多个java类:
第一个remoting-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service-2" 
    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="ImageProcess">
	   <properties>
	        <source>cn.ImageProcess</source>
	    </properties>
	</destination>	
</service>

第二个remoting-config-2.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="test">
	   <properties>
	        <source>cn.com.test</source>
	    </properties>
	</destination>	
</service>

services-config.xml文件中的server部分:
 <services>
     	<service-include file-path="remoting-config-2.xml" />
        <service-include file-path="remoting-config.xml" />
        <service-include file-path="proxy-config.xml" />
        <service-include file-path="messaging-config.xml" />        
    </services>

felx调用代码:
<?xml version="1.0" encoding="utf-8"?>   
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">   
	<mx:Script>   
		<![CDATA[   
			import mx.rpc.events.FaultEvent;   
			import mx.collections.ArrayCollection;   
			import mx.rpc.remoting.mxml.RemoteObject;   
			import mx.controls.Alert;   
			import mx.rpc.events.ResultEvent;
			import mx.rpc.AbstractOperation ;
			
			public function submit():void{   
				var remote:RemoteObject = new RemoteObject();   
				//调用在J2EE端remoting-config.xml中配置的暴露出的类的名称 id   
				remote.destination = "ImageProcess";   
//				remote.destination = "test"; 
				//调用J2EE端类中的方法    
				var o:AbstractOperation = remote.getOperation("setAlphaPath");    
				o.send("E:\\workspace2\\gisImage\\flex_src\\assets\\hh-1.png","hh-4.png",4); 
//				remote.setHello();  
				
//				var o:AbstractOperation = remote.getOperation("ceshi");    
//				o.send("E:\\workspace2\\gisImage\\flex_src\\assets\\hh-1.png");
				
				//监听调用成功事件   
				o.addEventListener(ResultEvent.RESULT,result);                   
				//监听失败事件   
				o.addEventListener(FaultEvent.FAULT,fault);     
			}   
			
			private function result(evt:ResultEvent):void{   
				Alert.show(evt.result.toString());   
			}   
			
			private function fault(evt:FaultEvent):void{   
				Alert.show("调用失败!");   
			}          
		]]>   
	</mx:Script>   
	
	<mx:Button click="submit()" label="hello" horizontalCenter="0" verticalCenter="0"/>   
	
</mx:Application>


java代码test类:
package cn.com;

import java.util.*;
public class test {
	
	public String ceshi(String mm)
	{
		return "lllllllll"+mm;
	}

}

java代码,ImageProcess类
package cn;


public class ImageProcess{

	public String setAlphaPath(String srcImageFile, String descImageDir,int alpha ) {
		
		
		
		return "assetsff/"+descImageDir;
	}

}


注意: (1)要让flex掉用的java方法,则java方法必须是公有的。
       (2)虽然可以调用多个java类但是最好是在java类中定义公有的方法,传递其它的类的类名,方法名,等灵活调用,以后研究一下。
        (3)注意flex工程的配置文件:如编译器:services "E:/workspace2/gisImage/WebRoot/WEB-INF/flex/services-config.xml"   -locale en_US -source-path=locale/{locale} -keep-all-type-selectors=true
         flex服务器等,下面会有配置的截图。
flex调用java_第1张图片
flex调用java_第2张图片

你可能感兴趣的:(java,Flex,felx和java)