flex3.0+spring2.5+hibernate3.2

                MyEclipse6.5+MySQL5.5+flex3.0+spring2.5+hibernate3.2

 

新建Flex项目:

flex3.0+spring2.5+hibernate3.2

Project name:项目名(自己随便起名)

Application server type:后台应用程序选择J2EE

Java source folderjava文件要放的位置

点击Next——>


flex3.0+spring2.5+hibernate3.2

Target runtime:选择服务器

Flex WAR file:选择Flexjava通讯的BlazeDS.war文件(此处必须是3.2版本及以上,否则无法与spring结合)

Output folder:存放输出文件

点击Next——>
flex3.0+spring2.5+hibernate3.2
 

Output folder URL:访问路径,8585端口号要与你的Tomcat端口号一致

点击FinishFlex工程创建完成。

 

Flex工程基础上添加web应用:

选中firstFSH项目,右键——>MyEclipse——>Add Web Project Capabilities…——>
flex3.0+spring2.5+hibernate3.2

Create web.xml问你是否创建web.xml,由于创建flex工程时已经有web.xml所以现不再创建,J2EE版本选择5.0

点击Finish,这时firstFSH工程会出现两个小地球,Flexjava结合完毕。

选中firstFSH项目,右键——>Properties——>Flex Server——>
flex3.0+spring2.5+hibernate3.2
 Root URL:改成http://localhost:8585

Context root:改成/firstFSH

(注意:如果出错,点击Validate Location即可。)

点击OK

 

测试FlexJava能否正常的进行通讯。

firstFSH工程下的src包下创建com.fsh.test.HelloWorld
flex3.0+spring2.5+hibernate3.2

package com.fsh.test;

public class HelloWorld {

         public String sayHelloTo(String name) {

                   System.out.println("****** Flex+java通讯成功 *****");

                   return "Hello "+name+" !" ;

         }

}

修改remoting-config.xml文件,加入如下代码

remoting-config.xml

    <destination id="hello">

             <properties>

                          <source>com.fsh.test.HelloWorld</source>

                          <scope>application</scope>

             </properties>

</destination>
flex3.0+spring2.5+hibernate3.2


flex3.0+spring2.5+hibernate3.2
 

 

编写firstFSH.mxml页面文件,代码如下

firstFSH.mxml

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

         <mx:Script>

                   <![CDATA[

                            import mx.collections.ArrayCollection;

                            import mx.rpc.events.FaultEvent;

                            import mx.rpc.events.ResultEvent;

                            import mx.controls.Alert ;

                            private function sayHelloTo():void {

                                     say.addEventListener(ResultEvent.RESULT, getResultHandler) ;

                                     say.sayHelloTo("张三") ;

                            }

                            private function getResultHandler(evt:ResultEvent):void {

                                     say.removeEventListener(ResultEvent.RESULT, getResultHandler) ;

                                     if(evt.result != null) {

                                               Alert.show(evt.result.toString()) ;

                                     } else {

                                               Alert.show("Exception") ;

                                     }

                            }

                   ]]>

         </mx:Script>

         <mx:RemoteObject id="say" destination="hello" />

         <mx:Panel x="338" y="83" width="578" height="391" layout="absolute">

                   <mx:HBox x="10" y="10" width="100%">

                            <mx:Button label="测试FlexJava通讯" width="170" fontSize="13" click="sayHelloTo() ;"/>

                   </mx:HBox>

         </mx:Panel>

</mx:Application>

 

右键选中firstFSH工程——>Run As(运行)——>
flex3.0+spring2.5+hibernate3.2

Flex3.0Java5.5结合成功,可以正常通讯

 

加入Spring2.5

右键选中firstFSH工程——>MyEclipse——>Add Spring Capabilities…——>
flex3.0+spring2.5+hibernate3.2

选中Spring2.5

选中红框内的5个选项

选中Copy checked Library contents to project folder (TLDs always copied)

点击Next——>
flex3.0+spring2.5+hibernate3.2

选中FinishSpring2.5加载完毕。

 

现测试Spring2.5加载的是否正确

src下建立com.fsh.common.factory包,将附件中的SpringFactory.java工厂类放入该包下
flex3.0+spring2.5+hibernate3.2
 

 

web.xml中加载Spring管理

web.xml

<context-param>

<param-name>contextConfigLocation</param-name>

           <param-value>/WEB-INF/classes/applicationContext.xml</param-value>

      </context-param>

      <listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

      </listener>

在与remoting-config.xml并列的service-config.xml文件中加入如下代码

services-config.xml

<factories>

 <factory id="spring" class="com.wingo.myfirstfsh.common.SpringFactory"/>

</factories>

remoting-config.xml文件中加入如下代码

remoting-config.xml

    <destination id="hello">

             <properties>

                       <factory>spring</factory>

                          <source>helloWorld</source>

             </properties>

</destination>

 

运行项目——>
flex3.0+spring2.5+hibernate3.2

此时Flex3.0+Spring2.5配置完成。

 

Mysql5.5下创建t_user表字段如下
flex3.0+spring2.5+hibernate3.2

添加两条数据
flex3.0+spring2.5+hibernate3.2

MyEclipse右上角切换视图——>MyEclipse Database Explorer——>新建数据库连接

 

DB Browser空白处右键——>New——>

flex3.0+spring2.5+hibernate3.2

Driver template:选择你选应用的数据库

Driver name:随便起名称

Connection URL:数据库连接,user_db是你的数据库名称

Add JARs:添加数据库连接所需的驱动包(附件中有)

此时可以点击Test Driver进行测试连接是否成功

点击Finish,会生成一个aaa连接对象,如下图aaa


flex3.0+spring2.5+hibernate3.2
 

 

将视图切换回Flex Development

右键选中firstFSH工程——>Add Hibernate Capabilities…——>
flex3.0+spring2.5+hibernate3.2
 

选中Hibernate3.2

选中红框中的两项MyEclipse Libraries

选中Copy checked Library Jars to project folder and add to build-path

点击Next——>
flex3.0+spring2.5+hibernate3.2
 

选中Spring configuration file (applicationContext.xml)管理(因为前面已经加入Spring

点击Next——>
flex3.0+spring2.5+hibernate3.2
 

选中Existing Spring configuration file(因为项目中已经存在applicationContext.xml文件,所以此处不需要新建该文件)

SessionFactory Id:输入Session工厂ID属性

点击Next——>
flex3.0+spring2.5+hibernate3.2

Bean Id:数据库连接ID

DB Driver:选择你刚刚建立的数据库连接对象

点击Next——>
flex3.0+spring2.5+hibernate3.2
 

勾去Create SessionFactory class(交给Spring管理)

点击Finish

 

firstFSH工程下创建daopo包,具体如下
flex3.0+spring2.5+hibernate3.2
 

切换数据库连接视图

右键选中t_user表——> Add Hibernate Capabilities——>
flex3.0+spring2.5+hibernate3.2
 点击Next——>
flex3.0+spring2.5+hibernate3.2
 

Id Generator:主键发生器,选中native本地

点击FinishHibernate加载完毕。

 

现进行测试Hibernate加载的是否正确以及Flex+Spring+Hibernate是否成功

 

调整java端代码结构

po包下的TUserDAO放到dao.impl包下,同时修改applicationContext.xml中的TUserDAO做出修改
flex3.0+spring2.5+hibernate3.2

dao包下创建IUserDAO接口
flex3.0+spring2.5+hibernate3.2
 

点击Finish——>如下
flex3.0+spring2.5+hibernate3.2
 

在上述接口中加入public List<TUser> findAll() 抽象方法

同时修改dao.impl.TUserDAO实现IUserDAO接口
flex3.0+spring2.5+hibernate3.2


com.fsh.service包下加入IUserService接口,在com.fsh.service.impl包下加入UserServiceImpl类实现IuserService接口
flex3.0+spring2.5+hibernate3.2
 

IUserService接口:

package com.fsh.service;

import java.util.List;

import com.fsh.po.TUser;

public interface IUserService {

         public List<TUser> findAll() ;

}

UserServiceImpl实现类:

package com.fsh.service.impl;

 

import java.util.List;

import com.fsh.dao.IUserDAO;

import com.fsh.po.TUser;

import com.fsh.service.IUserService;

 

public class UserServiceImpl implements IUserService{

         private IUserDAO iuserDao ;

         public IUserDAO getIuserDao() {

                   return iuserDao;

         }

         public void setIuserDao(IUserDAO iuserDao) {

                   this.iuserDao = iuserDao;

         }

         public List<TUser> findAll() {

                   // TODO Auto-generated method stub

                   return this.iuserDao.findAll();

         }

}

 

applicationContext.xml加入以下代码

         <bean id="userService" class="com.fsh.service.impl.UserServiceImpl">

                   <property name="iuserDao">

                            <ref local="TUserDAO"/>

                   </property>

         </bean>

 

Remoting-config.xml加入以下代码

    <destination id="userService">

             <properties>

                       <factory>spring</factory>

                          <source>userService</source>

             </properties>

</destination>

 

为以后的维护和代码结构整洁,现将flex端做出如下的MVC调整:

按照下图在flex_src包下建立control.UserCtrlevents.UserEventmodel.UserModelvo.UserVO四个AS
flex3.0+spring2.5+hibernate3.2
 

UserVO.as视图VO类,与Javapo.TUser类对应:

package vo

{

         [Bindable]

         public class UserVO

         {

                   public var num:Number;           //序号

                  

                   public var uid:int;

                   public var uname:String;

                   public var upassword:String;

                   public var usex:String;

                   public var uage:int;

                  

                   public function fill(obj:Object):void{

                            for(var i:* in obj){

                                     this[i]=obj[i];

                            }

                   }

         }

}

 

UserEvent.as自定义事件类:

package events

{

         public class UserEvent

         {

                   public static const showSucs:String = "showSucs" ;

                   public static const showFail:String = "showFail" ;

         }

}

 

UserModel.as主要用来绑定视图VO对象:

package model

{

         import flash.events.EventDispatcher;

         import mx.collections.ArrayCollection;

         [Bindable]

         public class UserModel extends EventDispatcher  {

                   private static var _userModel:UserModel;

                   public static function getInstance():UserModel {

                            if(_userModel == null) {

                                     _userModel = new UserModel() ;

                            }

                            return _userModel ;

                   }

                   public var userList:ArrayCollection ;

         }

}

 

UserCtrl.as控制层:

package control

{

import vo.UserVO;

         import events.UserEvent;

         import flash.events.Event;

         import model.UserModel;

        

         import mx.collections.ArrayCollection;

         import mx.controls.Alert;

         import mx.rpc.events.FaultEvent;

         import mx.rpc.events.ResultEvent;

         import mx.rpc.remoting.RemoteObject;

         import mx.managers.CursorManager;

        

         public class UserCtrl

         {

                   private var _userService:RemoteObject = null ;

                   private var _userModel:UserModel = UserModel.getInstance() ;

                   public function UserCtrl()

                   {

                            _userService = new RemoteObject() ;

                            _userService.destination = "userService" ;

                            _userService.addEventListener(FaultEvent.FAULT, faultHandler) ;

                   }

                  

                   private function faultHandler(evt:FaultEvent):void {

                            Alert.show(evt.fault.toString()) ;

                   }

                  

                   public function findAll():void {

                            _userService.findAll.addEventListener(ResultEvent.RESULT, resultHandler) ;

                            _userService.findAll.send() ;

                            CursorManager.setBusyCursor();

                   }

                   private function resultHandler(evt:ResultEvent):void {

                            if(evt.result != null) {

                                     this.getUserListResult(evt.result) ;

                            } else {

                                     _userModel.dispatchEvent(new Event(UserEvent.showFail));

                            }

                            CursorManager.removeBusyCursor();

                            _userService.removeEventListener(ResultEvent.RESULT, resultHandler) ;

                   }

                   private function getUserListResult(datas:Object):void {

                            var info:ArrayCollection = new ArrayCollection() ;

                            for(var i:Object in datas) {

                                     var uvo:UserVO = new UserVO() ;

                                     uvo.num = Number(i)+1 ;

                                     uvo.fill(datas[i]) ;

                                     info.addItem(uvo) ;

                            }

                            _userModel.userList = info ;

                            _userModel.dispatchEvent(new Event(UserEvent.showSucs));

                   }

         }

}

 

修改firstFSH.mxml类,代码如下:

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

         <mx:Script>

                   <![CDATA[

                            import events.UserEvent;

                            import model.UserModel;

                            import control.UserCtrl;

                            import mx.collections.ArrayCollection;

                            import mx.rpc.events.FaultEvent;

                            import mx.rpc.events.ResultEvent;

                            import mx.controls.Alert ;

                           

                            private var _theUserCtrl:UserCtrl = new UserCtrl();

                            [Bindable]

                            private var _theUserModel:UserModel = UserModel.getInstance();

                           

                            private function sayHelloTo():void {

                                     say.addEventListener(ResultEvent.RESULT, getResultHandler) ;

                                     say.sayHelloTo("张三") ;

                            }

                            private function getResultHandler(evt:ResultEvent):void {

                                     say.removeEventListener(ResultEvent.RESULT, getResultHandler) ;

                                     if(evt.result != null) {

                                               Alert.show(evt.result.toString()) ;

                                     } else {

                                               Alert.show("Exception") ;

                                     }

                            }

                           

                            private function findAll():void {

                                     _theUserModel.addEventListener(UserEvent.showSucs, resultHandler) ;

                                     _theUserModel.addEventListener(UserEvent.showFail, resultHandler) ;

                                     _theUserCtrl.findAll();

                            }

                            private function resultHandler(evt:Event):void {

                              _theUserModel.removeEventListener(UserEvent.showSucs,resultHandler) ;

                              _theUserModel.removeEventListener(UserEvent.showFail,resultHandler) ;

                                     if(evt.type == UserEvent.showSucs) {

                                               userList_dg.dataProvider = _theUserModel.userList ;

                                     } else if(evt.type == UserEvent.showFail) {

                                               Alert.show("ERROR") ;

                                     }

                            }

                           

                            private function doClear():void {

                                     userList_dg.dataProvider = _theUserModel.userList = null ;

                            }

 

                   ]]>

         </mx:Script>

        

         <mx:RemoteObject id="say" destination="hello" />

        

         <mx:Panel x="338" y="83" width="578" height="391" layout="absolute">

                   <mx:HBox x="10" y="10" width="100%">

<mx:Button label="测试FlexJava通讯" width="170" fontSize="13" click="sayHelloTo() ;"/>

                   </mx:HBox>

                   <mx:HBox x="10" y="44" width="100%">

                            <mx:Button label="查看" id="showUser" click="findAll() ;" fontSize="12" width="102"/>

                            <mx:Button label="清空" click="doClear() ;" fontSize="12" width="108"/>

                   </mx:HBox>

                   <mx:HBox width="100%" x="10" y="72">

                            <mx:DataGrid textAlign="center" fontWeight="bold" fontSize="12" id="userList_dg">

                                     <mx:columns>

                                               <mx:DataGridColumn headerText=" "  dataField="num"/>

                                               <mx:DataGridColumn headerText=" "  dataField="uname"/>

                                               <mx:DataGridColumn headerText=" "  dataField="usex"/>

                                               <mx:DataGridColumn headerText=" "  dataField="uage"/>

                                     </mx:columns>

                            </mx:DataGrid>

                   </mx:HBox>

         </mx:Panel>

</mx:Application>

 

工程运行——>
flex3.0+spring2.5+hibernate3.2
  

出现上述错误的解决办法:

applicationContext.xml中的<beans>头里加入如下代码即可

default-autowire="byName" default-lazy-init="true"
flex3.0+spring2.5+hibernate3.2
 

 

再运行——>
flex3.0+spring2.5+hibernate3.2
 

出现上述异常是由于springHibernateasm.jar包冲突造成的

解决办法:

将删除工程WEB-INF/lib下的asm-2.2.3.jar包,刷新项目,重启tomcat

 

再运行——>
flex3.0+spring2.5+hibernate3.2
 

 

至此flex3.0+spring2.5+hibernate3.2配置完成!