实现flex与java通信的方式通常有三种:http请求、web服务(web service)和RemoteObject(使用中间件
交互)。其中通过RemoteObject组件与java交互是最快速方便的,但是不同语言交互需要使用插件实现
AMF(Action Message Format)协议,关于java的flex插件我们通常使用开源免费的BlazeDS。
开发环境:eclipse和flash builder,如果eclipse安装了fb插件则更好了。
依赖框架:spring2.6、Hibernate3.3、blasds(下载地址:http://opensource.adobe.com/wiki/display/blazeds/Downloads)
关键jar包:spring-webmvc.jar、spring.jar、org.springframework.flex-1.0.3.RELEASE.jar、hibernate3.jar
eclipse新建一个dynamic web project
加入Hibernate、spring必备jar包至WEB-INF/lib目录下
解压blazeds.war文件,将blazeds\WEB-INF\lib目录下所有jar文件加入到项目WEB-INF/lib目录下
找到org.springframework.flex-1.0.3.RELEASE.jar(我在spring3中找到的)并加入到项目WEB-INF/lib目录下
将blazeds\WEB-INF下的flex文件夹拷贝到项目WEB-INF目录下
编写诸多dao、service代码,在此不再赘述。
我编写了一个名叫LoginServiceImpl的类,供flex调用:
public class LoginServiceImpl implements LoginService{ private LoginInfoDao loginDao; public void setLoginDao(LoginInfoDao loginDao) { this.loginDao = loginDao; } @Override public long getLoginInfoTotalCounts() { return loginDao.getLoginInfoTotalCounts(); } }
编写spring配置文件,数据库连接池、事务管理、诸多IOC配置在此不再赘述。关键看如何让LoginServiceImpl允许flex调用,这里需要定义<flex>标签:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 为了把请求路由给 MessageBroker,添加以下 tag--> <flex:message-broker /> <bean id="loginServiceBean" class="com.bless.logininfo.service.impl.LoginServiceImpl"> <!-- 指定当前bean被远程调用 --> <flex:remoting-destination /> <property name="loginDao" ref="loginInfoDaoBean"></property> </bean> </beans>
Flex调用java方法的大概原理是:flex向服务器发送调用请求,该请求路径是*/messagebroker/*的形式,所以spring需要截获请求路径,再根据请求信息调用指定方法,为了截获路径,所以需要使用spring mvc。
随后编写web.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>ssh2model</display-name> <description>ssh2model Application</description> <!-- spring mvc --> <servlet> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> <!-- 著名 Character Encoding filter --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <!-- spring IOC --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:com/bless/base/config/spring/springApplicationContext.xml, classpath:com/bless/*/config/spring/spring-*-*.xml </param-value> </context-param> <!-- blaseds监听器:用于获取服务器内置对象(request,application等) --> <listener> <listener-class>flex.messaging.HttpFlexSession</listener-class> </listener> <!--Spring ApplicationContext 载入 ,必须--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring 刷新Introspector防止内存泄露 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- session超时定义,单位为分钟 --> <session-config> <session-timeout>20</session-timeout> </session-config> <!--添加远程支持,方便Flex映射Java类--> <servlet> <display-name>RDSDispatchServlet</display-name> <servlet-name>RDSDispatchServlet</servlet-name> <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class> <init-param> <param-name>useAppserverSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>messageBrokerId</param-name> <param-value>_messageBroker</param-value> </init-param> <load-on-startup>10</load-on-startup> </servlet> <servlet-mapping id="RDS_DISPATCH_MAPPING"> <servlet-name>RDSDispatchServlet</servlet-name> <url-pattern>/CFIDE/main/ide.cfm</url-pattern> </servlet-mapping> </web-app>
最后找到WEB-INF/flex/services-config.xml文件,更改services标签内的配置,如下图所示:
以上spring+Hibernate+blaseds基本整合完成,将项目放在服务器中启动,第一次整合肯定会有问题,要么缺jar包要么配置有问题,耐心点一个个解决掉就行了,没事多问百度谷歌。
<?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" creationComplete="init(event)"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; import mx.rpc.events.ResultEvent; import mx.rpc.remoting.RemoteObject; protected function init(event:FlexEvent):void { var remote:RemoteObject = new RemoteObject(); remote.destination = "loginServiceBean"; remote.getLoginInfoTotalCounts(); remote.addEventListener(ResultEvent.RESULT,function callback(event:ResultEvent):void{ Alert.show(event.result.toString()); }); } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Button x="172" y="54" label="按钮"/> </s:Application>