`
caijingbin0207
  • 浏览: 106941 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

flex3.0+spring2.5+hibernate3.2

    博客分类:
  • FSH
阅读更多

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

 

新建Flex项目:

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

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

Java source folderjava文件要放的位置

点击Next——>


Target runtime:选择服务器

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

Output folder:存放输出文件

点击Next——>

 

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

点击FinishFlex工程创建完成。

 

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

选中firstFSH项目,右键——>MyEclipse——>Add Web Project Capabilities…——>

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

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

选中firstFSH项目,右键——>Properties——>Flex Server——>

 Root URL:改成http://localhost:8585

Context root:改成/firstFSH

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

点击OK

 

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

firstFSH工程下的src包下创建com.fsh.test.HelloWorld

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>



 

 

编写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.0Java5.5结合成功,可以正常通讯

 

加入Spring2.5

右键选中firstFSH工程——>MyEclipse——>Add Spring Capabilities…——>

选中Spring2.5

选中红框内的5个选项

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

点击Next——>

选中FinishSpring2.5加载完毕。

 

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

src下建立com.fsh.common.factory包,将附件中的SpringFactory.java工厂类放入该包下

 

 

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配置完成。

 

Mysql5.5下创建t_user表字段如下

添加两条数据

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

 

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

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

Driver name:随便起名称

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

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

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

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



 

 

将视图切换回Flex Development

右键选中firstFSH工程——>Add Hibernate Capabilities…——>

 

选中Hibernate3.2

选中红框中的两项MyEclipse Libraries

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

点击Next——>

 

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

点击Next——>

 

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

SessionFactory Id:输入Session工厂ID属性

点击Next——>

Bean Id:数据库连接ID

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

点击Next——>

 

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

点击Finish

 

firstFSH工程下创建daopo包,具体如下

 

切换数据库连接视图

右键选中t_user表——> Add Hibernate Capabilities——>

 点击Next——>

 

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

点击FinishHibernate加载完毕。

 

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

 

调整java端代码结构

po包下的TUserDAO放到dao.impl包下,同时修改applicationContext.xml中的TUserDAO做出修改

dao包下创建IUserDAO接口

 

点击Finish——>如下

 

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

同时修改dao.impl.TUserDAO实现IUserDAO接口


com.fsh.service包下加入IUserService接口,在com.fsh.service.impl包下加入UserServiceImpl类实现IuserService接口

 

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

 

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>

 

工程运行——>

  

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

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

default-autowire="byName" default-lazy-init="true"

 

 

再运行——>

 

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

解决办法:

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

 

再运行——>

 

 

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

 

  • 大小: 49.5 KB
  • 大小: 47.7 KB
  • 大小: 41.8 KB
  • 大小: 38.5 KB
  • 大小: 47.3 KB
  • 大小: 17.9 KB
  • 大小: 42.8 KB
  • 大小: 34 KB
  • 大小: 14.6 KB
  • 大小: 95.7 KB
  • 大小: 45.8 KB
  • 大小: 7.1 KB
  • 大小: 12.4 KB
  • 大小: 29.2 KB
  • 大小: 7.9 KB
  • 大小: 48.3 KB
  • 大小: 20.5 KB
  • 大小: 78.9 KB
  • 大小: 49.8 KB
  • 大小: 48.7 KB
  • 大小: 53.7 KB
  • 大小: 36.2 KB
  • 大小: 8.2 KB
  • 大小: 51.8 KB
  • 大小: 64.8 KB
  • 大小: 15.7 KB
  • 大小: 51.8 KB
  • 大小: 21.3 KB
  • 大小: 8.7 KB
  • 大小: 5.7 KB
  • 大小: 19.2 KB
  • 大小: 22.8 KB
  • 大小: 27.7 KB
  • 大小: 20.6 KB
  • 大小: 23.9 KB
分享到:
评论

相关推荐

    hibernate3.2+spring2.5+flex3.0整合框架

    hibernate3.2+spring2.5+flex3.0整合框架,自己搭建的,可供学习之用,该框架的搭建环境是在myeclipse6.5下,还有就是因为上传文件的大不限制,只好分两部分下,请见谅!

    hibernate3.2+spring2.5+flex3.0整合框架2

    hibernate3.2+spring2.5+flex3.0整合框架,自己搭建的,可供学习之用,该框架的搭建环境是在myeclipse6.5下,还有就是因为上传文件的大不限制,只好分两部分下,请见谅!

    网站技术简介

    总之,VideoShare通过集成Flex3、BlazeDS、Spring2.5、Hibernate3.2、MySQL5.0、FFmpeg、MPlayer和Red5等一系列先进技术,构建了一个集视频分享、互动交流、社交娱乐于一体的综合性平台,不仅满足了用户对高质量视频...

    Spring攻略(第二版 中文高清版).part1

    2.5 使用Spring表达式语言 64 2.5.1 问题 64 2.5.2 解决方案 64 2.5.3 工作原理 65 2.6 设置Bean作用域 69 2.6.1 问题 69 2.6.2 解决方案 69 2.6.3 工作原理 70 2.7 自定义Bean初始化和析构 72 ...

    Spring攻略(第二版 中文高清版).part2

    2.5 使用Spring表达式语言 64 2.5.1 问题 64 2.5.2 解决方案 64 2.5.3 工作原理 65 2.6 设置Bean作用域 69 2.6.1 问题 69 2.6.2 解决方案 69 2.6.3 工作原理 70 2.7 自定义Bean初始化和析构 72 ...

Global site tag (gtag.js) - Google Analytics