Flex与服务器通讯有3中方式:HTTPService 组件访问HTTP服务,WebService 组件访问WebService服务,RemoteObject 组件访问Server端对象。第三种方法是最常用最灵活的方法,这种方式通过AMF二进制形式传递数据,需要支持AMF协议的中间件,与java通讯时使用BlazeDS(免费开源)中间件。
新建项目
新建Flex项目,服务器选择J2EE-->BlazeDS,LCDS WAR文件选择blazeds.war文件(下载地址http://download.csdn.net/detail/sjepy/4464788),完成后在FlexDemo.mxml中添加如下代码:
在Servers视图中添加FlexDemo,启动服务,访问http://localhost:8080/FlexDemo/FlexDemo.html,如果出现空列表则表明项目能正常运行。
使用RemoteObject与java通讯
1.在后台新建EmployeeService,返回员工信息到前台flex
packagedemo.flex.service;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
publicclassEmployeeService {
publicList getEmpList() {
List empList = newArrayList();
Map emp = null;
for(inti =0; i <5; i++) {
emp = newHashMap();
emp.put("name","name"+ i);
emp.put("age",20+ i);
emp.put("email","email"+ i +"@flex.demo");
empList.add(emp);
}
returnempList;
}
}
package demo.flex.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class EmployeeService {
public List getEmpList() {
List empList = new ArrayList();
Map emp = null;
for (int i = 0; i < 5; i++) {
emp = new HashMap();
emp.put("name", "name" + i);
emp.put("age", 20 + i);
emp.put("email", "email" + i + "@flex.demo");
empList.add(emp);
}
return empList;
}
}
2.在WEB-INF/flex/remoting-config.xml文件中添加EmployeeService 对应的 destination
demo.flex.service.EmployeeService
demo.flex.service.EmployeeService
3.修改FlexDemo.mxml,通过RemoteObject直接调用后台EmployeeService的getEmpList()方法
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"creationComplete="init()">
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
private var empList:ArrayCollection;
protected function empRemote_resultHandler(event:ResultEvent):void
{
empList = event.result as ArrayCollection;
}
protected function empRemote_faultHandler(event:FaultEvent):void
{
Alert.show(event.toString());
}
protected function init():void
{
empRemote.getEmpList();
}
]]>
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init()">
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
private var empList:ArrayCollection;
protected function empRemote_resultHandler(event:ResultEvent):void
{
empList = event.result as ArrayCollection;
}
protected function empRemote_faultHandler(event:FaultEvent):void
{
Alert.show(event.toString());
}
protected function init():void
{
empRemote.getEmpList();
}
]]>
4.重新运行项目,界面显示后台返回的员工信息。如果弹出错误信息:HTTP:Status:url:http://localhost:8080/WebContent/messagebroker/amf....打开项目根目录,修改.flexProperties文件中serverContextRoot="/WebContent"为serverContextRoot="/FlexDemo",刷新项目重新运行。(注:我在做本测试时没有问题,但做数据推送测试时后台一直收不到前台订阅信息,浪费了很多时间,一直以为是配置问题,最后才发现是serverContextRoot的原因,建议一定修改serverContextRoot) 整合Spring
springframework及依赖包: aopalliance.jar aspectjweaver.jar cglib-nodep-2.1_3.jar(下载地址http://download.csdn.net/detail/sjepy/5516813) org.springframework.aop-3.0.5.RELEASE.jar org.springframework.asm-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE.jar org.springframework.context-3.0.5.RELEASE.jar org.springframework.core-3.0.5.RELEASE.jar org.springframework.expression-3.0.5.RELEASE.jar org.springframework.web.servlet-3.0.5.RELEASE.jar org.springframework.web-3.0.5.RELEASE.jar BlazeDS + Spring整合: org.springframework.flex-1.0.3.RELEASE.jar(下载地址http://download.csdn.net/detail/sjepy/5516755)
2.修改web.xml,去掉所有flex相关配置(如果有的话),添加如下配置:
Spring MVC Dispatcher Servlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
Spring MVC Dispatcher Servlet
/messagebroker/*
Spring MVC Dispatcher Servlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
Spring MVC Dispatcher Servlet
/messagebroker/*
3.在classpath下添加Spring的配置文件applicationContext.xml:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
4.在EmployeeService上添加注解:
@Component
@RemotingDestination// 把EmployeeService暴露给Flex
@Component
@RemotingDestination// 把EmployeeService暴露给Flex
5.删除WEB-INF/flex下除services-config.xml以外的所有配置文件,修改services-config.xml
前台代码不变,完成后重新运行项目,显示效果和前面一样,至此整合完成。