普通Java Web工程流行使用ssh框架,而當前臺使用Flex製做的時候,後臺就不須要用Struts了,經過使用BlazeDS遠程方法調用便可。 首先,新建Java Web工程,而後添加Flex項目,詳情見經過J2EE Web工程添加Flex項目,進行BlazeDS開發 。 隨後,導入Jar包,flex相關的jar包在添加Flex項目的時候已經導入,這裏主要是導入Spring和Hibernate相關的jar包以及spring和flex集成的jar包,我用的是spring 3.0.3和hibernate 3.2.1。 下面是lib列表: antlr-2.7.6.jar asm-2.2.3.jar asm-commons-2.2.3.jar asm-util-2.2.3.jar aspectjlib.jar aspectjrt.jar aspectjweaver.jar backport-util-concurrent.jar c3p0-0.9.1.jar cfgatewayadapter.jar cglib-nodep-2.1_3.jar com.springsource.edu.emory.mathcs.backport-3.0.0.jar com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar commons-codec-1.3.jar commons-collections-3.1.jar commons-httpclient-3.0.1.jar commons-io-1.4.jar commons-lang-2.3.jar commons-logging.jar dom4j-1.6.1.jar ehcache-1.2.3.jar flex-messaging-common.jar flex-messaging-core.jar flex-messaging-opt.jar flex-messaging-proxy.jar flex-messaging-remoting.jar flex-rds-server.jar freemarker-2.3.15.jar hibernate-commons-annotations-3.2.0.Final.jar hibernate-core-3.5.4-Final.jar hibernate-jpa-2.0-api-1.0.0.Final.jar jackson-lgpl-0.9.5.jar javassist.jar jta-1.1.jar log4j-1.2.15.jar mysql-connector-java-5.1.13-bin.jar org.springframework.aop-3.0.3.RELEASE.jar org.springframework.asm-3.0.3.RELEASE.jar org.springframework.aspects-3.0.3.RELEASE.jar org.springframework.beans-3.0.3.RELEASE.jar org.springframework.context-3.0.3.RELEASE.jar org.springframework.context.support-3.0.3.RELEASE.jar org.springframework.core-3.0.3.RELEASE.jar org.springframework.expression-3.0.3.RELEASE.jar org.springframework.flex-1.0.3.RELEASE.jar org.springframework.instrument-3.0.3.RELEASE.jar org.springframework.instrument.tomcat-3.0.3.RELEASE.jar org.springframework.jdbc-3.0.3.RELEASE.jar org.springframework.jms-3.0.3.RELEASE.jar org.springframework.orm-3.0.3.RELEASE.jar org.springframework.oxm-3.0.3.RELEASE.jar org.springframework.test-3.0.3.RELEASE.jar org.springframework.transaction-3.0.3.RELEASE.jar org.springframework.web-3.0.3.RELEASE.jar org.springframework.web.portlet-3.0.3.RELEASE.jar org.springframework.web.servlet-3.0.3.RELEASE.jar org.springframework.web.struts-3.0.3.RELEASE.jar slf4j-api-1.5.8.jar slf4j-log4j12-1.5.8.jar xalan.jar 修改配置文件,web.xml,其中最重要的是修改原先MessegeBroker Servlet,改成由Spring web應用前端控制器處理全部請求。 複製代碼 代碼 <!-- MessageBroker Servlet 單獨爲Flex配置xml--> <servlet> <servlet-name>flex</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/config/flex-application-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all /messagbroker requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>flex</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> 複製代碼 配置 flex-application-config.xml,注意增長的幾個命名空間,須要包org.springframework.flex-1.0.3.RELEASE.jar,同時在這個文件裏定義了bean,在mxml裏面會引用到: 複製代碼 flex-application-config.xml <?xml version="1.0" encoding="UTF-8"?> <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-2.5.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd"> <!-- Bootstraps and exposes the BlazeDS MessageBroker simplest form --> <flex:message-broker/> <bean id="test" class="sample.Test"> <property name="userDAO"> <ref bean="userDAO" /> </property> <flex:remoting-destination /> </bean> </beans> 複製代碼 hibernate的配置,用到c0p0.jar, daoContext.xml 複製代碼 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 使用c3p0定義數據源Bean --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db"/> <property name="user" value="user"/> <property name="password" value="pass"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql"> true </prop> <prop key="hibernate.format_sql"> true </prop> <prop key="hibernate.show_statistics"> false </prop> </props> </property> <property name="mappingResources"> <list> <value>sample/dao/User.hbm.xml</value> </list> </property> </bean> <bean id="userDAO" class="sample.dao.UserDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> </beans> 複製代碼 test.mxml,這裏主要是定義和調用遠程對象,遠程對象的bean在上面的flex-application-config.xml裏面有定義,調用類sample.Test.java中的createUser方法。 <?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"> <fx:Script> <![CDATA[ import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; protected function button_clickHandler(event:MouseEvent):void { // TODO Auto-generated method stub test.createUser("name","pass"); } protected function resulth(event:ResultEvent):void { label.text="Succeed!"; } protected function faulth(event:FaultEvent):void { label.text="failed!"; } ]]> </fx:Script> <fx:Declarations> <!-- 將非可視元素(例如服務、值對象)放在此處 --> <mx:RemoteObject id="ro" destination="test" result="resulth(event);" fault="faulth(event);" /> </fx:Declarations> <s:Button x="165" y="96" label="點一下" width="414" height="110" fontSize="26" id="button" click="button_clickHandler(event)"/> <s:Label x="287" y="290" text="遠程調用測試" width="202" height="50" verticalAlign="middle" textAlign="center" fontSize="22" id="label"/> </s:Application> 複製代碼 總結:這篇筆記不大全,只是大概展示了整合這些框架的要點,其中,主要是Flex4,spring3和BlazeDS4之間的整合。尤爲須要注意的是jar包的導入。 本文沒有涉及到blazeDS和spring整合實現遠程方法調用服務和消息服務的配置,會在接下來寫。 這裏有一個我的以爲還蠻不錯的Flex教程:Flex系列教程BY蒲公英