flex3+blazeds+spring+hibernate整合小结
??? 近来flex盛行,因此这两天也借了本书看了两天,发觉作为非页面设计人员,flex 还是很好的,flex builder很好用,拖拉就有很COOL的界面了,而且flex总的来说基本东西不难学,有编程基础的人很快掌握,当然要精通就要时间了,因为库,API等很多.
??? 下面就flex3+blazeds+spring+hibernate整合作个小结,是之前读外国好文的心得,而见国内这方面的文比较少,因此笔记之.
?
首先要知道,flex3是做前端的,其实就是view层的东西了,可以替换掉struts 2,如果项目中你喜欢的话.而blazeds是adobe免费的转换网关(可以理解成转换网关),负责把后端的数据与actionscript进行转换,当然也可以用
收费的那个livecycle data services了.而spring+hiberate的组合很传统了.
?
1 准备好东西
A 下载blazeds,这里下载blazeds_turnkey_3-0-0-544的版本,因为自带了tomcat还有些好的例子
B FLEXBUILDER 3
???????? C MYSQL 5
?????? D SPRING+HIBERNATE,myeclipse,这些就不说了.
?
2 本文是根据http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=2&postId=7923
去小结的.其中在http://www.adobe.com/devnet/livecycle/articles/blazeds_spring.html中,讨论了如何跟spring结合,如何跟
hsqldb结合.那么文中是举了blazeds自带的例子来做说明的,在它的基础上进行修改,变成hibernate+spring.
?
3 先从http://download.macromedia.com/pub/developer/flex_spring.zip下载例子文件,其中包括了例子和作者写的
用spring调用的组件.
解压flex-spring.zip
将/flex-spring/factory/bin/flex/samples/factories 目录下的class文件拷贝到/WEB-INF/classes/flex/samples/factories目录中
?
4 在/WEB-INF/flex/services-config.xml文件中注册spring factory
<factories>
<factory id="spring" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"[]>
<hibernate-mapping package="flex.samples.spring.store">
?<class name="Product" table="PRODUCT">
??<id name="productId" type="long" column="PRODUCTID"
???unsaved-value="0">
???<generator />
??</id>
??<property name="name" column="NAME" length="40" />
??<property name="category" column="CATEGORY" length="40" />
??<property name="image" column="IMAGE" length="40" />
??<property name="price" column="PRICE" type="double" />
??<property name="description" column="DESCRIPTION" length="255" />
??<property name="qtyInStock" column="QTYINSTOCK"
???type="integer" />
?</class>
</hibernate-mapping>
?
9 编写applicationContext.xml,注意要放在blazeds_turnkey_3-0-0-544\tomcat\webapps\blazeds\WEB-INF下
内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
??<bean id="dataSource"
??value="com.mysql.jdbc.Driver" />
??<property name="url"
???value="jdbc:mysql://localhost:3306/flexhibernate" />
??<property name="username" value="root" />
??<property name="password" value="abc" />
?</bean>
?<bean id="sessionFactory"
??/>
??</property>
?</bean>
?<bean id="txManager"
??/>
??</property>
?</bean>
?<bean id="productDAOBeanTarget"
??ref="sessionFactory" />
?</bean>
?<bean id="productDAOBean"
??ref="txManager" />
??<property name="target" ref="productDAOBeanTarget" />
??<property name="transactionAttributes">
???<props>
????<prop key="create*">PROPAGATION_REQUIRED</prop>
????<prop key="update*">PROPAGATION_REQUIRED</prop>
????<prop key="delete*">PROPAGATION_REQUIRED</prop>
????<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
???</props>
??</property>
?</bean>
</beans>
很典型的传统spring+hibernate的配置了.
?
10 修改blazeds_turnkey_3-0-0-544\tomcat\webapps\blazeds\WEB-INF\flex目录下的
remoting-config.xml文件,增加如下部分:
<destination id="productService">
<properties>
<factory>spring</factory>
<source>productDAOBean</source>
</properties>
</destination>
??
11 准备spring,hibernate,mysqljdbc等驱动包,可以先放在blazeds_turnkey_3-0-0-544\tomcat\webapps\blazeds\WEB-INF\lib下,
当然也可以通过下面写ANT文件去指定,道理都是一样的.注意我用的是spring 1.2.8的包,2.0X的还没试过,各位可以试下.
?
12 可以看到,原文作者在sample\store目录下,有个build.xml,但其中因为我是windows下的,所以修改了路径符号为\,
如下
<?xml version="1.0" encoding="utf-8"?>
<project name="Store Application" basedir="." default="main">
??? <property name="FLEX_HOME" value="C:\Program Files\Adobe\Flex Builder 3\sdks\3.1.0"/>
??? <property name="DEPLOY_DIR" value="L:\blazeds_turnkey_3-0-0-544\tomcat\webapps\blazeds"/>
??? <property name="CONTEXT_ROOT" value="blazeds"/>
<property?? name="lib.dir"?? value="L:\blazeds_turnkey_3-0-0-544\tomcat\webapps\blazeds\WEB-INF\lib"/>
<path?? id="classpath">??
????????????????? <fileset?? dir="${lib.dir}">??
????????????????????????? <include?? name="*.jar"/>??
????????????????? </fileset>??
????????? </path>??
??? <taskdef resource="flexTasks.tasks" classpath="${FLEX_HOME}\ant\flexTasks.jar"? />
??? <target name="main" depends="compile-flex,html-wrapper,compile-java"/>
???
??? <target name="compile-flex">
??????? <mxmlc file="flex/storeadmin.mxml"
??????? ?services="${DEPLOY_DIR}\WEB-INF\flex\services-config.xml"
??????? ?context-root="${CONTEXT_ROOT}"
???output="${DEPLOY_DIR}\storeadmin\storeadmin.swf"/>
??????? <mxmlc file="flex/store.mxml"
??????? ?services="${DEPLOY_DIR}/WEB-INF\flex\services-config.xml"
??????? ?context-root="${CONTEXT_ROOT}"
???output="${DEPLOY_DIR}\store\store.swf"/>
??? ?<copy todir="${DEPLOY_DIR}\store\pic">
??? ??<fileset dir="flex\pic"/>
? ??</copy>
???
??? </target>
??? <target name="compile-java">
??????? <javac srcdir="java" destdir="${DEPLOY_DIR}\WEB-INF\classes">
?? <classpath?? refid="classpath"/>?
??????? ?</javac>
?? <copy todir="${DEPLOY_DIR}/WEB-INF/classes/flex/samples/spring/store">?
??? <fileset dir="java" includes="**/*hbm.xml"/>?
? </copy>
??? </target>
?<target name="html-wrapper">
??????? <html-wrapper
????????????? application="app"
????????????? width="100%"
????????????? height="100%"
????????????? swf="storeadmin"
????????????? version-major="9"
????????????? version-minor="0"
????????????? version-revision="0"
????????????? history="true"?????????????
????????????? template="express-installation"
????????????? output="${DEPLOY_DIR}\storeadmin"/>
??????? <html-wrapper
????????????? application="app"
????????????? width="100%"
????????????? height="100%"
????????????? swf="store"
????????????? version-major="9"
????????????? version-minor="0"
????????????? version-revision="0"
????????????? history="true"?????????????
????????????? template="express-installation"
????????????? output="${DEPLOY_DIR}\store"/>
??? </target>
???
</project>
?
要注意的是,因为flex 3的ant扩展了ant,因此,要保证Adobe\Flex Builder 3\sdks\3.1.0\ant下有文件flexTasks.jar,
也要把flexTasks.jar COPY到ant的目录下去.
之后就可以在sample\store根目录下,运行ant打包运行了.
?
13 之后启动tomcat,运行
http://localhost:8400/blazeds/store/index.html
和?http://localhost:8400/blazeds/storeadmin/index.html,
? 就可以看到效果了.
?
14 简单分析其调用过程
把其中一个flex文件打开,比如
<Product id="product"
??name="{productName.text}"
??category="{category.text}"
??price="{Number(price.text)}"
??qtyInStock="{int(qtyInStock.text)}"
??image="{image.text}"
??description="{description.text}"/>
?<mx:RemoteObject id="srv" destination="productService"/>
?<mx:Form width="100%">
?
??<mx:FormItem label="Name">
???<mx:TextInput id="productName" text="{product.name}"/>
??</mx:FormItem>
?
??<mx:FormItem label="Category">
???<mx:TextInput id="category" text="{product.category}"/>
??</mx:FormItem>
??
??<mx:FormItem label="Image">
???<mx:TextInput id="image" text="{product.image}"/>
??</mx:FormItem>
??
??<mx:FormItem label="Price">
???<mx:TextInput id="price" text="{product.price}"/>
??</mx:FormItem>
??<mx:FormItem label="In Stock">
???<mx:TextInput id="qtyInStock" text="{product.qtyInStock}"/>
??</mx:FormItem>
?
??<mx:FormItem label="Description" width="100%">
???<mx:TextArea id="description" text="{product.description}" width="100%" height="100"/>
??</mx:FormItem>
??
?</mx:Form>
?<mx:ControlBar>
??<mx:Button label="Update" click="srv.updateProduct(product)"/>
?</mx:ControlBar>
?
其中,关注<mx:Button label="Update" click="srv.updateProduct(product)"/>,这里,就是调用srv的updateProduct(product)了,
其中,srv是一个?<mx:RemoteObject id="srv" destination="productService"/>,destination="productService"是什么?
就是第10步中指定的那个productService了,呵呵,应该大致明白了吧?