Flex4 blazeds Spring JAVA 进行交互

下载 安装,不多说,

一、搭建好JAVA WEB SSH 项目,将下载好的 blazeds 解压,

找到       拷贝到  WebRoot > WEB-INF 下

blazeds 下载地址 http://download.csdn.net/detail/justmaybe_/4851512

注: 直接拷贝过去,会覆盖原有的 web.xml , 最好是 将里面的内容 粘贴到 java 项目里面的 web.xml;


二 、配置 Xml 文件 

1、services-config  添加 springFactory    FlexSpringFactory 类代码后面给出

	<factories>
		<factory id="springFactory" class="flex.factory.FlexSpringFactory" />
	</factories>

2、创建 FlexServices类 配置 remoting-config.xml将FlexServices 的Bean 配置到 applicationContext.xml 

FlexServices类  代码:

public class FlexServices {
	 public String sayHello(String name) {
		 System.out.println("Hello" + name);
		 return "Hello" + name;
	 }
 }
remoting-config.xml 代码:
	<destination id="helloworld">
		<properties>
			<factory>springFactory</factory>
			<source>flexServices</source>
		</properties>
	</destination>

      applicationContext.xml 配置

 <bean id="flexServices" class="com.winoa.flex.factory.FlexServices.java"></bean>

将Java Web项目发布到 Tomcat 上去并启动服务

至此 JAVA 配置完成

现在Flex ,我安装的是 FB4.6, 跟前面的版本应该有点不同吧。

一、 新建Flex 项目 > 下一步 

 

( 选择 BlazeDs  这里需要配置服务器位置 我用的是MyEclipe 8.5 自带的Tomcat ) >  完成



二、在该项目创建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="955" minHeight="600">
	<s:layout>
		<s:HorizontalLayout verticalAlign="middle" horizontalAlign="center"/>
	</s:layout>
	
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
		
		
			//调用成功返回结果处理
			protected function remote_resultHandler(event:ResultEvent):void
			{
				var result:String = event.result.toString();  //将返回结果 转换成字符串
				Alert.show(result,"提示")
			}
		
			//点击调用事件
			protected function button1_clickHandler(event:MouseEvent):void
			{
				remote.sayHello(input.text);  //调用方法  sayHello 是 FlexServices.java 类型的一个方法
			}
			
			//调用失败返回结果处理
			protected function remote_faultHandler(event:FaultEvent):void
			{
				Alert.show("调用失败","提示")  
			}
			
		]]>
	</fx:Script>
	
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
		<!--
			RemoteObject	标记来表示 HTTPService 对象。此标记使您可以访问使用 Action Message Format (AMF) 编码的 Java 对象的方法。
			destination 	服务的目标。该值应与 services-config.xml 文件中的目标条目匹配
			result		当服务调用成功返回并且操作自身不处理时,将分派 result 事件。
			fault		当服务调用失败并且操作自身不处理时,将分派 fault 事件。
			showBusyCursor	如果为 true,在执行服务时会显示忙状态光标。默认值为 false。
		-->
		<s:RemoteObject id="remote" destination="helloworld" result="remote_resultHandler(event)" fault="remote_faultHandler(event)" showBusyCursor="true">
			
		</s:RemoteObject>
	</fx:Declarations>
	<s:TextInput id="input" />  <!---->
	<s:Button  label="提交" click="button1_clickHandler(event)" />
</s:Application>


MXML 注释写的很清楚, 就不再多说了

  右键 选择运行方式  , 或则 调试方式运行

然后我们打开 tomcat 服务起安装路径看看

>

最后的路径  :http://localhost:8080/flex_java/Flex_Test-debug/Flex_Test.html

FlexSpringFactory.java

import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;

public class FlexSpringFactory implements FlexFactory {

	public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
		SpringFactoryInstance instance = new SpringFactoryInstance(this, id,
				properties);
		instance.setSource(properties.getPropertyAsString(SOURCE, instance
				.getId()));
		return instance;
	}

	public Object lookup(FactoryInstance ins) {
		SpringFactoryInstance factoryInstance = (SpringFactoryInstance) ins;
		return factoryInstance.lookup();
	}

	public void initialize(String arg0, ConfigMap arg1) {
		// TODO Auto-generated method stub

	}

}

SpringFactoryInstance.java

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.BeansException;

import flex.messaging.FactoryInstance;
import flex.messaging.config.ConfigMap;
import flex.messaging.FlexContext;
import flex.messaging.services.ServiceException;

public class SpringFactoryInstance extends FactoryInstance {
	public SpringFactoryInstance(FlexSpringFactory factory, String id,
			ConfigMap properties) {
		super(factory, id, properties);
	}

	public Object lookup() {
		ApplicationContext appContext = WebApplicationContextUtils
				.getWebApplicationContext(FlexContext.getServletConfig()
						.getServletContext());
		String beanName = this.getSource();
		try {
			return appContext.getBean(beanName);
		} catch (NoSuchBeanDefinitionException ec) {
			ServiceException e = new ServiceException();
			throw e;
		} catch (BeansException bexc) {
			ServiceException e = new ServiceException();
			throw e;
		}
	}

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值