Adobe Flex是一套创建富客户端应用(RIAs)的框架.Flex生成的swf文件可以直接运行在Flash Player之中。相比较基于时间轴的Flash开发,Flex框架更适合那些用传统方式开发应用程序的开发人员。Flex应用可以使用Flex builder来开发。这套IDE是基于Eclipse平台开发的。Action Script3语言是用来访问数据和创建用户接口组件的。Flex框架也用到了一种XML语言叫做MXML,它是用来简化Flex开发和布局的。 Spring是目前最受欢迎的创建企业级应用的Java框架。不像传统的J2EE开发,Spring提供了轻量级的容器。使用Spring会使应用 的测试和开发更为简单。虽然Spring依赖注入的功能最出名,但是它也提供了其他服务端企业程序所需要的功能。如安全和事务处理。 Flex技术本身和Java就有很强的关联性,它提供了一个基于Eclipse的IDE和BlazeDS.BlazeDS是个基于服务端的Java 远程调用和Web消息的一个开源的技术。有许多应用都是以Java为后端处理的。Flex用于前端。由于Java和Flex一起频繁的使用。人们很容易就 想到Flex和Spring的整合。有许多企业和组织已经着手开始使用Flex作为用户接口了。在2008年末,Spring社区已经着手Spring BlazeDS整合的项目。为Java和Spring添加更好的Flex支持。
默认的情况下BlazeDS创建了一个服务端Java对象的实例,用它们来完成远程对象的请求。但是这种方法并不适用于Spring.因为整个框架 使用的服务的对象都是用Spring容器所创建的。Spring和BlazeDS的整合,Flex就可以使用Spring中的对象了。 为了使用BlazeDS,服务端的Java应用应打包为WAR文件。这部分的代码实例是使用Eclipse来创建和编辑的。步骤如下 设置服务端的BlazeDS Java工程以及Spring框架。
在BlazeDS中配置Spring的bean
写一个Flex应用去使用Spring/BlazeDS的服务。
Eclipse3.4(J2EE版本):http://www.eclipse.org/downloads/
Flex Builder3:http://www.adobe.com/cfusion/entitlement/index.cfm ?e=flex3email&sdid=EOZPI
Tomcat 6:http://tomcat.apache.org/
BlazeDS:http://opensource.adobe.com/wiki/display/blazeds/B lazeDS/
Spring框架:http://www.springsource.org/download
Spring BlazeDS整合:http://www.springsource.org/spring-flex
ANTLR3.0:http://www.antlr.org/download.html
首先设置服务端的JAVA工程,用blazeds.war(在blazeds的压缩包中)创建一个WEB工程。步骤如下
Choose File>import
选择WAR选项。指定blazedsWAR文件的位置。输入工程名test-server
点击完成
现在就可以创建一个服务器来运行这个WEB应用。
File>New>Other
选择Server>Server
点击Next
选择Apache>Tomcat6 Server
点击Next
指定Tomcat的安装位置以及JRE(5以上版本)
点击Next
在Availble Projects list中选择test-server
点击Add添加到Configured Project list
点击Finish
接下来就可以创建Java类了。这个类在Java和Flex之间传输
Java代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20MyEntity%20% 7B%0A%20private%20String%20frstName%3B%0A%20private %20String%20lastName%3B%0A%20private%20String%20ema ilAddress%3B%0A%20%0A%20public%20String%20getFirstN ame()%20%7B%0A%20%20return%20frstName%3B%0A%20%7D%0 A%20public%20void%20setFirstName(String%20frstName) %20%7B%0A%20%20this.frstName%20%3D%20frstName%3B%0A %20%7D%0A%20public%20String%20getLastName()%20%7B%0 A%20%20return%20lastName%3B%0A%20%7D%0A%20public%20 void%20setLastName(String%20lastName)%20%7B%0A%20%2 0this.lastName%20%3D%20lastName%3B%0A%20%7D%0A%20pu blic%20String%20getEmailAddress()%20%7B%0A%20%20ret urn%20emailAddress%3B%0A%20%7D%0A%20public%20void%2 0setEmailAddress(String%20emailAddress)%20%7B%0A%20 %20this.emailAddress%20%3D%20emailAddress%3B%0A%20% 7D%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
public class MyEntity {
private String frstName;
private String lastName;
private String emailAddress;
public String getFirstName() {
return frstName;
}
public void setFirstName(String frstName) {
this.frstName = frstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
} }
public class MyEntity { private String frstName; private String lastName; private String emailAddress; public String getFirstName() { return frstName; } public void setFirstName(String frstName) { this.frstName = frstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } }
Java代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=import%20java.util.List%3B%0A public%20interface%20MyService%20%7B%0A%20public%20 List%3CMyEntity%3E%20getMyEntities()%3B%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
Java代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=import%20java.util.ArrayList% 3B%20%20%0Aimport%20java.util.List%3B%20%20%0Apubli c%20class%20MyServiceImpl%20implements%20MyService% 20%7B%20%20%0A%20public%20List%3CMyEntity%3E%20getM yEntities()%20%7B%20%20%0A%20%20List%3CMyEntity%3E% 20list%20%3D%20new%20ArrayList%3CMyEntity%3E()%3B%2 0%20%0A%20%20MyEntity%20entity%20%3D%20new%20MyEnti ty()%3B%20%20%0A%20%20entity.setFirstName(%22Hello% 22)%3B%20%20%0A%20%20entity.setLastName(%22World%22 )%3B%20%20%0A%20%20entity.setEmailAddress(%22hello% 40world.com%22)%3B%20%20%0A%20%20list.add(entity)%3 B%20%20%0A%20%20MyEntity%20entity2%20%3D%20new%20My Entity()%3B%20%20%0A%20%20entity2.setFirstName(%22H ello%22)%3B%20%20%0A%20%20entity2.setLastName(%22Sp ace%22)%3B%20%20%0A%20%20entity2.setEmailAddress(%2 2hello%40space.com%22)%3B%20%20%0A%20%20list.add(en tity2)%3B%20%20%0A%20%20MyEntity%20entity3%20%3D%20 new%20MyEntity()%3B%20%20%0A%20%20entity3.setFirstN ame(%22Hello%22)%3B%20%20%0A%20%20entity3.setLastNa me(%22Neighbor%22)%3B%20%20%0A%20%20entity3.setEmai lAddress(%22hello%40neighbor.com%22)%3B%20%20%0A%20 %20list.add(entity3)%3B%20%20%0A%20%20return%20list %3B%20%20%0A%20%7D%20%20%0A%7D%20%20%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
import java.util.ArrayList;
import java.util.List;
public class MyServiceImpl implements MyService {
public List getMyEntities() {
List list = new ArrayList();
MyEntity entity = new MyEntity();
entity.setFirstName("Hello");
entity.setLastName("World");
entity.setEmailAddress("hello@world.com");
list.add(entity);
MyEntity entity2 = new MyEntity();
entity2.setFirstName("Hello");
entity2.setLastName("Space");
entity2.setEmailAddress("hello@space.com");
list.add(entity2);
MyEntity entity3 = new MyEntity();
entity3.setFirstName("Hello");
entity3.setLastName("Neighbor");
entity3.setEmailAddress("hello@neighbor.com");
list.add(entity3);
return list;
} }
import java.util.ArrayList; import java.util.List; public class MyServiceImpl implements MyService { public List getMyEntities() { List list = new ArrayList(); MyEntity entity = new MyEntity(); entity.setFirstName("Hello"); entity.setLastName("World"); entity.setEmailAddress("hello@world.com"); list.add(entity); MyEntity entity2 = new MyEntity(); entity2.setFirstName("Hello"); entity2.setLastName("Space"); entity2.setEmailAddress("hello@space.com"); list.add(entity2); MyEntity entity3 = new MyEntity(); entity3.setFirstName("Hello"); entity3.setLastName("Neighbor"); entity3.setEmailAddress("hello@neighbor.com"); list.add(entity3); return list; } }
这三个类对于例子足够了。在实战中,这个服务类可能要连接到数据库。为了方便我们学习,这个例子中就返回的是个list的硬编码了。
基本的java工程算是完工了。。
接下来我们要做Spring的配置了。
把Spring的库以及Spring BlazeDS整合的库,还有ANTLR库文件放到项目/WEB-INF/lib下。
创建一个Spring配置文件。鼠标右键点击WebContent/WEB-INF以及选择New>File,文件名输入application-config.xml.点击完成。配置文件内容如下。
Xml代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%20%20%0A%3Cbeans% 20xmlns%3D%22http%3A%2F%2Fwww.springframework.org%2 Fschema%2Fbeans%22%20%20%0A%20xmlns%3Axsi%3D%22http %3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22% 20%20%0A%20xsi%3AschemaLocation%3D%22%20%20%0A%20ht tp%3A%2F%2Fwww.springframework.org%2Fschema%2Fbeans %20%20%0A%20http%3A%2F%2Fwww.springframework.org%2F schema%2Fbeans%2Fspring-beans-2.5.xsd%22%3E%20%20%0 A%20%3C!--%20Spring%20Beans%E2%80%99s%20--%3E%20%20 %0A%20%3Cbean%20id%3D%22myService%22%20class%3D%22M yServiceImpl%22%20%2F%3E%20%20%0A%3C%2Fbeans%3E%20% 20%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
懂Spring的人看这配置最熟悉不过了。。
通过这一步,已经有了一个BlazeDS默认配置的Java web工程。下面我们就更改BlazeDS默认配置,去使用新创建的Spring中的bean.
为了配置Spring BlazeDS的整合,更新web.xml。
Xml代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cweb-app%20xm lns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXML Schema-instance%22%0A%09xmlns%3D%22http%3A%2F%2Fjav a.sun.com%2Fxml%2Fns%2Fjavaee%22%20xmlns%3Aweb%3D%2 2http%3A%2F%2Fjava.sun.com%2Fxml%2Fns%2Fjavaee%2Fwe b-app_2_5.xsd%22%0A%09xsi%3AschemaLocation%3D%22htt p%3A%2F%2Fjava.sun.com%2Fxml%2Fns%2Fjavaee%20http%3 A%2F%2Fjava.sun.com%2Fxml%2Fns%2Fjavaee%2Fweb-app_2 _5.xsd%22%0A%09id%3D%22WebApp_ID%22%20version%3D%22 2.5%22%3E%0A%09%3Cdisplay-name%3Etest-server%3C%2Fd isplay-name%3E%0A%09%3Cservlet%3E%0A%09%09%3Cservle t-name%3ESpring%20MVC%20Dispatcher%20Servlet%3C%2Fs ervlet-name%3E%0A%09%09%3Cservlet-class%3Eorg.sprin gframework.web.servlet.DispatcherServlet%3C%2Fservl et-class%3E%0A%09%09%3Cinit-param%3E%0A%09%09%09%3C param-name%3EcontextConfigLocation%3C%2Fparam-name% 3E%0A%09%09%09%3Cparam-value%3E%2FWEB-INF%2Fapplica tion-config.xml%3C%2Fparam-value%3E%0A%09%09%3C%2Fi nit-param%3E%0A%09%09%3Cload-on-startup%3E1%3C%2Flo ad-on-startup%3E%0A%09%3C%2Fservlet%3E%0A%09%3C!--% 20Map%20%2Fspring%2F*%20requests%20to%20the%20Dispa tcherServlet%20--%3E%0A%09%3Cservlet-mapping%3E%0A% 09%09%3Cservlet-name%3ESpring%20MVC%20Dispatcher%20 Servlet%3C%2Fservlet-name%3E%0A%09%09%3Curl-pattern %3E%2Fspring%2F*%3C%2Furl-pattern%3E%0A%09%3C%2Fser vlet-mapping%3E%0A%3C%2Fweb-app%3E%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
test-server
Spring MVC Dispatcher Servlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/application-config.xml
1
Spring MVC Dispatcher Servlet
/spring/*
test-server Spring MVC Dispatcher Servlet org.springframework.web.servlet.Dispa tcherServlet contextConfigLocation /WEB-INF/application-config.xml 1 Spring MVC Dispatcher Servlet /spring/*
创建的Servlet可以处理这个请求,http://localhost:8080/test-server/spring
这是访问BlazeDS的基本的URL。当然这也是Spring标准的DispatcherServlet.
现在已经把Spring整合到Java web工程中了。要整合BlazeDS,就要修改下Spring的配置文件。
application-config.xml文件如下
Xml代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cbeans%20xmln s%3D%22http%3A%2F%2Fwww.springframework.org%2Fschem a%2Fbeans%22%0A%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww .w3.org%2F2001%2FXMLSchema-instance%22%0A%20xmlns%3 Aflex%3D%22http%3A%2F%2Fwww.springframework.org%2Fs chema%2Fflex%22%0A%20xsi%3AschemaLocation%3D%22%0A% 20http%3A%2F%2Fwww.springframework.org%2Fschema%2Fb eans%0A%20http%3A%2F%2Fwww.springframework.org%2Fsc hema%2Fbeans%2Fspring-beans-2.5.xsd%0A%20http%3A%2F %2Fwww.springframework.org%2Fschema%2Fflex%0A%20htt p%3A%2F%2Fwww.springframework.org%2Fschema%2Fflex%2 Fspring-flex-1.0.xsd%22%3E%20%20%0A%20%3C!--%20Spri ng%20Beans%E2%80%99s%20--%3E%0A%20%3Cbean%20id%3D%2 2myService%22%20class%3D%22MyServiceImpl%22%20%2F%3 E%0A%20%3C!--%20Simplest%20possible%20message%20bro ker%20--%3E%20%0A%20%3Cflex%3Amessage-broker%2F%3E% 0A%20%20%3C!--%20exposes%20myService%20as%20BlazeDS %20destination%20--%3E%0A%20%3Cflex%3Aremoting-dest ination%20ref%3D%22myService%22%20%2F%3E%0A%3C%2Fbe ans%3E%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
flex="http://www.springframework.org/schema/flex"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring -beans-2.5.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring- flex-1.0.xsd">
flex:message-broker/>
flex:remoting-destination ref="myService" />
flex="http://www.springframework.org/schema/ flex" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring -beans-2.5.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring- flex-1.0.xsd"> flex:message-broker/> flex:remoting-destination ref="myService" />
通过配置,使BlazeDS接口开放。首先要添加Flex的namespace。添加之后,使用message-broker标签创建 MessageBrokerFactoryBean。看下配置只是个简单标签。配置是默认的。要确保WEB-INF/flex 下有service-config.xml这个配置文件。remoting-destination标签使Spring bean变为远程目标。
接下来修改默认的BlazeDS service-config.xml文件。代码如下
Xml代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cservices-con fig%3E%0A%09%3Cservices%3E%0A%09%09%3Cdefault-chann els%3E%0A%09%09%09%3Cchannel%20ref%3D%22my-amf%22%2 0%2F%3E%0A%09%09%3C%2Fdefault-channels%3E%0A%09%3C% 2Fservices%3E%0A%09%3Cchannels%3E%0A%09%09%3Cchanne l-definition%20id%3D%22my-amf%22%0A%09%09%09class%3 D%22mx.messaging.channels.AMFChannel%22%3E%0A%09%09 %09%3Cendpoint%0A%09%09%09%09url%3D%22http%3A%2F%2F %7Bserver.name%7D%3A%7Bserver.port%7D%2F%7Bcontext. root%7D%2Fspring%2Fmessagebroker%2Famf%22%0A%09%09% 09%09class%3D%22flex.messaging.endpoints.AMFEndpoin t%22%20%2F%3E%0A%09%09%3C%2Fchannel-definition%3E%0 A%09%09%3Cchannel-definition%20id%3D%22my-polling-a mf%22%0A%09%09%09class%3D%22mx.messaging.channels.A MFChannel%22%3E%0A%09%09%09%3Cendpoint%0A%09%09%09% 09url%3D%22http%3A%2F%2F%7Bserver.name%7D%3A%7Bserv er.port%7D%2F%7Bcontext.root%7D%2Fspring%2Fmessageb roker%2Famfpolling%22%0A%09%09%09%09class%3D%22flex .messaging.endpoints.AMFEndpoint%22%20%2F%3E%0A%09% 09%09%3Cproperties%3E%0A%09%09%09%09%3Cpolling-enab led%3Etrue%3C%2Fpolling-enabled%3E%0A%09%09%09%09%3 Cpolling-interval-seconds%3E4%3C%2Fpolling-interval -seconds%3E%0A%09%09%09%3C%2Fproperties%3E%0A%09%09 %3C%2Fchannel-definition%3E%0A%09%3C%2Fchannels%3E% 0A%3C%2Fservices-config%3E%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
true
4
true 4
看一下 endpoint标签的的url.唯一可以修改的就是content.root之后的spring.所有远程目标配置都应该配置在application-config.xml文件中。
File>New>Other
选择Flex Project
填写工程名称test-flex
用默认的地址
选择Web application(运行在Flash player)
Application Type 选择None
点击Next
指定Output的文件夹。如 C:\workspace\test-server\WebContent\
点击Finish
Xml代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22utf-8%22%3F%3E%0A%3Cmx%3AApplica tion%20xmlns%3Amx%3D%22http%3A%2F%2Fwww.adobe.com%2 F2006%2Fmxml%22%20%0A%09layout%3D%22absolute%22%20% 0A%09creationComplete%3D%22srv.getMyEntities()%22%3 E%0A%09%3Cmx%3AAMFChannel%20id%3D%22myamf%22%20uri% 3D%22%2Ftest-server%2Fspring%2Fmessagebroker%2Famf% 22%2F%3E%20%20%0A%20%3Cmx%3AChannelSet%20id%3D%22ch annelSet%22%20channels%3D%22%7B%5Bmyamf%5D%7D%22%2F %3E%20%20%0A%20%3Cmx%3ARemoteObject%20id%3D%22srv%2 2%20%0A%20%20%20destination%3D%22myService%22%20cha nnelSet%3D%22%7BchannelSet%7D%22%2F%3E%20%20%0A%20% 3Cmx%3ADataGrid%20dataProvider%3D%22%7Bsrv.getMyEnt ities.lastResult%7D%22%2F%3E%0A%3C%2Fmx%3AApplicati on%3E%0A%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
RemoteObject id="srv"
destination="myService" channelSet="{channelSet}"/>
RemoteObject id="srv" destination="myService" channelSet="{channelSet}"/>
以上代码的AMFChannel访问了Spring的服务。
要注意的是RemoteObject标签中的destination的destination要和spring的application-config.xml中remote-service标签的ref所设置的值保持一致。通过代码可以看出Flex并没有包含关于Spring的东西。编写Flex的
开发人员不必知道关于Spring的知识。
要更新test-server的程序。可以Refresh这个工程。
下面测试一下。启动Tomcat.http://localhost:8080/test-server/main.html
为了在Flex builder中调试运行。可以做如下设置 之后直接运行就可以了。整合完成。
默认的情况下BlazeDS创建了一个服务端Java对象的实例,用它们来完成远程对象的请求。但是这种方法并不适用于Spring.因为整个框架 使用的服务的对象都是用Spring容器所创建的。Spring和BlazeDS的整合,Flex就可以使用Spring中的对象了。 为了使用BlazeDS,服务端的Java应用应打包为WAR文件。这部分的代码实例是使用Eclipse来创建和编辑的。步骤如下 设置服务端的BlazeDS Java工程以及Spring框架。
在BlazeDS中配置Spring的bean
写一个Flex应用去使用Spring/BlazeDS的服务。
Eclipse3.4(J2EE版本):http://www.eclipse.org/downloads/
Flex Builder3:http://www.adobe.com/cfusion/entitlement/index.cfm ?e=flex3email&sdid=EOZPI
Tomcat 6:http://tomcat.apache.org/
BlazeDS:http://opensource.adobe.com/wiki/display/blazeds/B lazeDS/
Spring框架:http://www.springsource.org/download
Spring BlazeDS整合:http://www.springsource.org/spring-flex
ANTLR3.0:http://www.antlr.org/download.html
首先设置服务端的JAVA工程,用blazeds.war(在blazeds的压缩包中)创建一个WEB工程。步骤如下
Choose File>import
选择WAR选项。指定blazedsWAR文件的位置。输入工程名test-server
点击完成
现在就可以创建一个服务器来运行这个WEB应用。
File>New>Other
选择Server>Server
点击Next
选择Apache>Tomcat6 Server
点击Next
指定Tomcat的安装位置以及JRE(5以上版本)
点击Next
在Availble Projects list中选择test-server
点击Add添加到Configured Project list
点击Finish
接下来就可以创建Java类了。这个类在Java和Flex之间传输
Java代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20MyEntity%20% 7B%0A%20private%20String%20frstName%3B%0A%20private %20String%20lastName%3B%0A%20private%20String%20ema ilAddress%3B%0A%20%0A%20public%20String%20getFirstN ame()%20%7B%0A%20%20return%20frstName%3B%0A%20%7D%0 A%20public%20void%20setFirstName(String%20frstName) %20%7B%0A%20%20this.frstName%20%3D%20frstName%3B%0A %20%7D%0A%20public%20String%20getLastName()%20%7B%0 A%20%20return%20lastName%3B%0A%20%7D%0A%20public%20 void%20setLastName(String%20lastName)%20%7B%0A%20%2 0this.lastName%20%3D%20lastName%3B%0A%20%7D%0A%20pu blic%20String%20getEmailAddress()%20%7B%0A%20%20ret urn%20emailAddress%3B%0A%20%7D%0A%20public%20void%2 0setEmailAddress(String%20emailAddress)%20%7B%0A%20 %20this.emailAddress%20%3D%20emailAddress%3B%0A%20% 7D%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
public class MyEntity {
private String frstName;
private String lastName;
private String emailAddress;
public String getFirstName() {
return frstName;
}
public void setFirstName(String frstName) {
this.frstName = frstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
} }
public class MyEntity { private String frstName; private String lastName; private String emailAddress; public String getFirstName() { return frstName; } public void setFirstName(String frstName) { this.frstName = frstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } }
Java代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=import%20java.util.List%3B%0A public%20interface%20MyService%20%7B%0A%20public%20 List%3CMyEntity%3E%20getMyEntities()%3B%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
Java代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=import%20java.util.ArrayList% 3B%20%20%0Aimport%20java.util.List%3B%20%20%0Apubli c%20class%20MyServiceImpl%20implements%20MyService% 20%7B%20%20%0A%20public%20List%3CMyEntity%3E%20getM yEntities()%20%7B%20%20%0A%20%20List%3CMyEntity%3E% 20list%20%3D%20new%20ArrayList%3CMyEntity%3E()%3B%2 0%20%0A%20%20MyEntity%20entity%20%3D%20new%20MyEnti ty()%3B%20%20%0A%20%20entity.setFirstName(%22Hello% 22)%3B%20%20%0A%20%20entity.setLastName(%22World%22 )%3B%20%20%0A%20%20entity.setEmailAddress(%22hello% 40world.com%22)%3B%20%20%0A%20%20list.add(entity)%3 B%20%20%0A%20%20MyEntity%20entity2%20%3D%20new%20My Entity()%3B%20%20%0A%20%20entity2.setFirstName(%22H ello%22)%3B%20%20%0A%20%20entity2.setLastName(%22Sp ace%22)%3B%20%20%0A%20%20entity2.setEmailAddress(%2 2hello%40space.com%22)%3B%20%20%0A%20%20list.add(en tity2)%3B%20%20%0A%20%20MyEntity%20entity3%20%3D%20 new%20MyEntity()%3B%20%20%0A%20%20entity3.setFirstN ame(%22Hello%22)%3B%20%20%0A%20%20entity3.setLastNa me(%22Neighbor%22)%3B%20%20%0A%20%20entity3.setEmai lAddress(%22hello%40neighbor.com%22)%3B%20%20%0A%20 %20list.add(entity3)%3B%20%20%0A%20%20return%20list %3B%20%20%0A%20%7D%20%20%0A%7D%20%20%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
import java.util.ArrayList;
import java.util.List;
public class MyServiceImpl implements MyService {
public List getMyEntities() {
List list = new ArrayList();
MyEntity entity = new MyEntity();
entity.setFirstName("Hello");
entity.setLastName("World");
entity.setEmailAddress("hello@world.com");
list.add(entity);
MyEntity entity2 = new MyEntity();
entity2.setFirstName("Hello");
entity2.setLastName("Space");
entity2.setEmailAddress("hello@space.com");
list.add(entity2);
MyEntity entity3 = new MyEntity();
entity3.setFirstName("Hello");
entity3.setLastName("Neighbor");
entity3.setEmailAddress("hello@neighbor.com");
list.add(entity3);
return list;
} }
import java.util.ArrayList; import java.util.List; public class MyServiceImpl implements MyService { public List getMyEntities() { List list = new ArrayList(); MyEntity entity = new MyEntity(); entity.setFirstName("Hello"); entity.setLastName("World"); entity.setEmailAddress("hello@world.com"); list.add(entity); MyEntity entity2 = new MyEntity(); entity2.setFirstName("Hello"); entity2.setLastName("Space"); entity2.setEmailAddress("hello@space.com"); list.add(entity2); MyEntity entity3 = new MyEntity(); entity3.setFirstName("Hello"); entity3.setLastName("Neighbor"); entity3.setEmailAddress("hello@neighbor.com"); list.add(entity3); return list; } }
这三个类对于例子足够了。在实战中,这个服务类可能要连接到数据库。为了方便我们学习,这个例子中就返回的是个list的硬编码了。
基本的java工程算是完工了。。
接下来我们要做Spring的配置了。
把Spring的库以及Spring BlazeDS整合的库,还有ANTLR库文件放到项目/WEB-INF/lib下。
创建一个Spring配置文件。鼠标右键点击WebContent/WEB-INF以及选择New>File,文件名输入application-config.xml.点击完成。配置文件内容如下。
Xml代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%20%20%0A%3Cbeans% 20xmlns%3D%22http%3A%2F%2Fwww.springframework.org%2 Fschema%2Fbeans%22%20%20%0A%20xmlns%3Axsi%3D%22http %3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22% 20%20%0A%20xsi%3AschemaLocation%3D%22%20%20%0A%20ht tp%3A%2F%2Fwww.springframework.org%2Fschema%2Fbeans %20%20%0A%20http%3A%2F%2Fwww.springframework.org%2F schema%2Fbeans%2Fspring-beans-2.5.xsd%22%3E%20%20%0 A%20%3C!--%20Spring%20Beans%E2%80%99s%20--%3E%20%20 %0A%20%3Cbean%20id%3D%22myService%22%20class%3D%22M yServiceImpl%22%20%2F%3E%20%20%0A%3C%2Fbeans%3E%20% 20%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
懂Spring的人看这配置最熟悉不过了。。
通过这一步,已经有了一个BlazeDS默认配置的Java web工程。下面我们就更改BlazeDS默认配置,去使用新创建的Spring中的bean.
为了配置Spring BlazeDS的整合,更新web.xml。
Xml代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cweb-app%20xm lns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXML Schema-instance%22%0A%09xmlns%3D%22http%3A%2F%2Fjav a.sun.com%2Fxml%2Fns%2Fjavaee%22%20xmlns%3Aweb%3D%2 2http%3A%2F%2Fjava.sun.com%2Fxml%2Fns%2Fjavaee%2Fwe b-app_2_5.xsd%22%0A%09xsi%3AschemaLocation%3D%22htt p%3A%2F%2Fjava.sun.com%2Fxml%2Fns%2Fjavaee%20http%3 A%2F%2Fjava.sun.com%2Fxml%2Fns%2Fjavaee%2Fweb-app_2 _5.xsd%22%0A%09id%3D%22WebApp_ID%22%20version%3D%22 2.5%22%3E%0A%09%3Cdisplay-name%3Etest-server%3C%2Fd isplay-name%3E%0A%09%3Cservlet%3E%0A%09%09%3Cservle t-name%3ESpring%20MVC%20Dispatcher%20Servlet%3C%2Fs ervlet-name%3E%0A%09%09%3Cservlet-class%3Eorg.sprin gframework.web.servlet.DispatcherServlet%3C%2Fservl et-class%3E%0A%09%09%3Cinit-param%3E%0A%09%09%09%3C param-name%3EcontextConfigLocation%3C%2Fparam-name% 3E%0A%09%09%09%3Cparam-value%3E%2FWEB-INF%2Fapplica tion-config.xml%3C%2Fparam-value%3E%0A%09%09%3C%2Fi nit-param%3E%0A%09%09%3Cload-on-startup%3E1%3C%2Flo ad-on-startup%3E%0A%09%3C%2Fservlet%3E%0A%09%3C!--% 20Map%20%2Fspring%2F*%20requests%20to%20the%20Dispa tcherServlet%20--%3E%0A%09%3Cservlet-mapping%3E%0A% 09%09%3Cservlet-name%3ESpring%20MVC%20Dispatcher%20 Servlet%3C%2Fservlet-name%3E%0A%09%09%3Curl-pattern %3E%2Fspring%2F*%3C%2Furl-pattern%3E%0A%09%3C%2Fser vlet-mapping%3E%0A%3C%2Fweb-app%3E%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
test-server
Spring MVC Dispatcher Servlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/application-config.xml
1
Spring MVC Dispatcher Servlet
/spring/*
test-server Spring MVC Dispatcher Servlet org.springframework.web.servlet.Dispa tcherServlet contextConfigLocation /WEB-INF/application-config.xml 1 Spring MVC Dispatcher Servlet /spring/*
创建的Servlet可以处理这个请求,http://localhost:8080/test-server/spring
这是访问BlazeDS的基本的URL。当然这也是Spring标准的DispatcherServlet.
现在已经把Spring整合到Java web工程中了。要整合BlazeDS,就要修改下Spring的配置文件。
application-config.xml文件如下
Xml代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cbeans%20xmln s%3D%22http%3A%2F%2Fwww.springframework.org%2Fschem a%2Fbeans%22%0A%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww .w3.org%2F2001%2FXMLSchema-instance%22%0A%20xmlns%3 Aflex%3D%22http%3A%2F%2Fwww.springframework.org%2Fs chema%2Fflex%22%0A%20xsi%3AschemaLocation%3D%22%0A% 20http%3A%2F%2Fwww.springframework.org%2Fschema%2Fb eans%0A%20http%3A%2F%2Fwww.springframework.org%2Fsc hema%2Fbeans%2Fspring-beans-2.5.xsd%0A%20http%3A%2F %2Fwww.springframework.org%2Fschema%2Fflex%0A%20htt p%3A%2F%2Fwww.springframework.org%2Fschema%2Fflex%2 Fspring-flex-1.0.xsd%22%3E%20%20%0A%20%3C!--%20Spri ng%20Beans%E2%80%99s%20--%3E%0A%20%3Cbean%20id%3D%2 2myService%22%20class%3D%22MyServiceImpl%22%20%2F%3 E%0A%20%3C!--%20Simplest%20possible%20message%20bro ker%20--%3E%20%0A%20%3Cflex%3Amessage-broker%2F%3E% 0A%20%20%3C!--%20exposes%20myService%20as%20BlazeDS %20destination%20--%3E%0A%20%3Cflex%3Aremoting-dest ination%20ref%3D%22myService%22%20%2F%3E%0A%3C%2Fbe ans%3E%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
flex="http://www.springframework.org/schema/flex"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring -beans-2.5.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring- flex-1.0.xsd">
flex:message-broker/>
flex:remoting-destination ref="myService" />
flex="http://www.springframework.org/schema/ flex" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring -beans-2.5.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring- flex-1.0.xsd"> flex:message-broker/> flex:remoting-destination ref="myService" />
通过配置,使BlazeDS接口开放。首先要添加Flex的namespace。添加之后,使用message-broker标签创建 MessageBrokerFactoryBean。看下配置只是个简单标签。配置是默认的。要确保WEB-INF/flex 下有service-config.xml这个配置文件。remoting-destination标签使Spring bean变为远程目标。
接下来修改默认的BlazeDS service-config.xml文件。代码如下
Xml代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cservices-con fig%3E%0A%09%3Cservices%3E%0A%09%09%3Cdefault-chann els%3E%0A%09%09%09%3Cchannel%20ref%3D%22my-amf%22%2 0%2F%3E%0A%09%09%3C%2Fdefault-channels%3E%0A%09%3C% 2Fservices%3E%0A%09%3Cchannels%3E%0A%09%09%3Cchanne l-definition%20id%3D%22my-amf%22%0A%09%09%09class%3 D%22mx.messaging.channels.AMFChannel%22%3E%0A%09%09 %09%3Cendpoint%0A%09%09%09%09url%3D%22http%3A%2F%2F %7Bserver.name%7D%3A%7Bserver.port%7D%2F%7Bcontext. root%7D%2Fspring%2Fmessagebroker%2Famf%22%0A%09%09% 09%09class%3D%22flex.messaging.endpoints.AMFEndpoin t%22%20%2F%3E%0A%09%09%3C%2Fchannel-definition%3E%0 A%09%09%3Cchannel-definition%20id%3D%22my-polling-a mf%22%0A%09%09%09class%3D%22mx.messaging.channels.A MFChannel%22%3E%0A%09%09%09%3Cendpoint%0A%09%09%09% 09url%3D%22http%3A%2F%2F%7Bserver.name%7D%3A%7Bserv er.port%7D%2F%7Bcontext.root%7D%2Fspring%2Fmessageb roker%2Famfpolling%22%0A%09%09%09%09class%3D%22flex .messaging.endpoints.AMFEndpoint%22%20%2F%3E%0A%09% 09%09%3Cproperties%3E%0A%09%09%09%09%3Cpolling-enab led%3Etrue%3C%2Fpolling-enabled%3E%0A%09%09%09%09%3 Cpolling-interval-seconds%3E4%3C%2Fpolling-interval -seconds%3E%0A%09%09%09%3C%2Fproperties%3E%0A%09%09 %3C%2Fchannel-definition%3E%0A%09%3C%2Fchannels%3E% 0A%3C%2Fservices-config%3E%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
true
4
true 4
看一下 endpoint标签的的url.唯一可以修改的就是content.root之后的spring.所有远程目标配置都应该配置在application-config.xml文件中。
File>New>Other
选择Flex Project
填写工程名称test-flex
用默认的地址
选择Web application(运行在Flash player)
Application Type 选择None
点击Next
指定Output的文件夹。如 C:\workspace\test-server\WebContent\
点击Finish
Xml代码 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22utf-8%22%3F%3E%0A%3Cmx%3AApplica tion%20xmlns%3Amx%3D%22http%3A%2F%2Fwww.adobe.com%2 F2006%2Fmxml%22%20%0A%09layout%3D%22absolute%22%20% 0A%09creationComplete%3D%22srv.getMyEntities()%22%3 E%0A%09%3Cmx%3AAMFChannel%20id%3D%22myamf%22%20uri% 3D%22%2Ftest-server%2Fspring%2Fmessagebroker%2Famf% 22%2F%3E%20%20%0A%20%3Cmx%3AChannelSet%20id%3D%22ch annelSet%22%20channels%3D%22%7B%5Bmyamf%5D%7D%22%2F %3E%20%20%0A%20%3Cmx%3ARemoteObject%20id%3D%22srv%2 2%20%0A%20%20%20destination%3D%22myService%22%20cha nnelSet%3D%22%7BchannelSet%7D%22%2F%3E%20%20%0A%20% 3Cmx%3ADataGrid%20dataProvider%3D%22%7Bsrv.getMyEnt ities.lastResult%7D%22%2F%3E%0A%3C%2Fmx%3AApplicati on%3E%0A%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">
RemoteObject id="srv"
destination="myService" channelSet="{channelSet}"/>
RemoteObject id="srv" destination="myService" channelSet="{channelSet}"/>
以上代码的AMFChannel访问了Spring的服务。
要注意的是RemoteObject标签中的destination的destination要和spring的application-config.xml中remote-service标签的ref所设置的值保持一致。通过代码可以看出Flex并没有包含关于Spring的东西。编写Flex的
开发人员不必知道关于Spring的知识。
要更新test-server的程序。可以Refresh这个工程。
下面测试一下。启动Tomcat.http://localhost:8080/test-server/main.html
为了在Flex builder中调试运行。可以做如下设置 之后直接运行就可以了。整合完成。