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



5 配置web.xml

contextConfigLocation
/WEB-INF/applicationContext.xml


org.springframework.web.context.ContextLoaderListener

6 在下载的程序中,我们重点关注samples/store这个目录下的程序.
其中flex目录是写好了的flex界面了,大家可以用flexbuilder去看.

而java目录其实就是后端的目录.无非就是用spring来实现对product对象的增,删改.

7 我们先用mysql 5在建立数据库 flexhibernate,表结构如下:
CREATE TABLE `product` (
  `productId` int(11) NOT NULL auto_increment,
  `name` varchar(40) NOT NULL,
  `category` varchar(40) NOT NULL,
  `image` varchar(255) NOT NULL,
  `price` double NOT NULL,
  `description` varchar(255) NOT NULL,
  `qtyInStock` int(20) NOT NULL,
  KEY `productId` (`productId`)
)

 

8 ,我们编写一个product.hbm.xml如下,放在samples/store/java目录下
   


 
      unsaved-value="0">
   
  

  
  
  
  
  
      type="integer" />
 

 

9 编写applicationContext.xml,注意要放在blazeds_turnkey_3-0-0-544/tomcat/webapps/blazeds/WEB-INF

内容如下
    




 
    class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  
      value="jdbc:mysql://localhost:3306/flexhibernate" />
  
  
 

    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  
   
    flex/samples/spring/store/Product.hbm.xml
   

  

  
   
    
     org.hibernate.dialect.HSQLDialect
    

   

  

  
   
  

 

    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  
   
  

 

    class="flex.samples.spring.store.HibernateProductDAO">
  
 

    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  
  
  
   
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED,readOnly

   
  
 

  很典型的传统spring+hibernate的配置了.

 

10 修改blazeds_turnkey_3-0-0-544/tomcat/webapps/blazeds/WEB-INF/flex目录下的
remoting-config.xml文件,增加如下部分:
  

spring
productDAOBean

  

  

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下的,所以修改了路径符号为\,

如下
  


   
   
   

  
                    
                            
                    
            

   

   
   
   
                 services="${DEPLOY_DIR}/WEB-INF/flex/services-config.xml"
         context-root="${CONTEXT_ROOT}"
   output="${DEPLOY_DIR}/storeadmin/storeadmin.swf"/>
                 services="${DEPLOY_DIR}/WEB-INF/flex/services-config.xml"
         context-root="${CONTEXT_ROOT}"
   output="${DEPLOY_DIR}/store/store.swf"/>
     
      
    

   
   

   
       
    
         

    
     
 

   

 
                      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"/>
                      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"/>
   

   

 

 要注意的是,因为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文件打开,比如
   

  name="{productName.text}"
  category="{category.text}"
  price="{Number(price.text)}"
  qtyInStock="{int(qtyInStock.text)}"
  image="{image.text}"
  description="{description.text}"/>

 

 
 
  
   
  

 
  
   
  

  
  
   
  

  
  
   
  

  
   
  

 
  
   
  

  
 

 
  
 

 

  其中,关注,这里,就是调用srv的updateProduct(product)了,

其中,srv是一个 ,destination="productService"是什么?

就是第10步中指定的那个productService了,呵呵,应该大致明白了吧?

 

你可能感兴趣的:(JAVA,ajax/web相关)