您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > flex整合spring,hibernate例子完整版
1.建立数据表Oracle数据库建表语句createtableflexuser(useridnumber(10),usernamevarchar(20),passvarchar(20),emailvarchar(20),primarykey(userid));2.插入测试数据insertintoflexuservalues('1','testuser','testuser','flex@surekam.com');3.新建flex项目按NEXT进入下一步1.必须设置J2EE运行服务器目标。第一次设置时需要新建。点击New,选择ApacheTomcatv6.0Server,如图:2.点击下一步,设置apache-tomcat-6.0.16的目录,如图:3.点击完成,J2EE运行服务器目标设为ApacheTomcatv6.0,如图:点击FINISH4测试flex+bleazeds的web环境是否正确。在myEclipse视图下在src下构建一个class,模拟服务器端的一个远程对象.packagecom.surekam.flexandjava;publicclassHelloWorld{publicStringsayHelloTo(Stringname){return你好!+name;}}在flex视图下在WEB-INF/flex/remotiong-config.xml中注册这个远程对象destinationid=hellopropertiessourcecom.surekam.flexandjava.HelloWorld/source/properties/destination5在flexsrc/flexTest.mxml中添加以下代码:?xmlversion=1.0encoding=utf-8?s:Applicationxmlns:fx=:s=library://ns.adobe.com/flex/sparkxmlns:mx=library://ns.adobe.com/flex/mxminWidth=955minHeight=600fx:Script![CDATA[importmx.controls.Alert;importmx.rpc.events.FaultEvent;importmx.rpc.events.ResultEvent;protectedfunctionro_resultHandler(event:ResultEvent):void{//TODOAuto-generatedmethodstubmessage.text=event.resultasString;}protectedfunctionro_faultHandler(event:FaultEvent):void{//TODOAuto-generatedmethodstubAlert.show(失败原因:+event.toString());}protectedfunctionbutton1_clickHandler(event:MouseEvent):void{//TODOAuto-generatedmethodstubro.sayHelloTo(username.text);}]]/fx:Scriptfx:Declarations!--将非可视元素(例如服务、值对象)放在此处--s:RemoteObjectid=rodestination=helloresult=ro_resultHandler(event)fault=ro_faultHandler(event)endpoint=/flexTest/messagebroker/amf/s:RemoteObject/fx:Declarationss:Buttonx=355y=65label=按钮click=button1_clickHandler(event)/s:Labelid=messagex=219y=125text=标签height=32width=206/s:TextInputid=usernamex=219y=65//s:Application工程发布运行,测试一下。这里/flexTest/messagebroker/amf的flexTest与项目同名在Flex视图下,server里new一个Tomcatserver服务,在开起服务运行,出现页面输入beyond,出现你好!beyond就成功了6在myeclipse视图下:在项目上右键选择myeclipse-添加spring支持右键Myeclipse-Addspring点击next在点击完成为flex通信提供spring管理在myeclipse视图下:添加包:com.surekam.project.common添加SpringFactory.javaPackagecom.surekam.project.common;importorg.springframework.context.ApplicationContext;importorg.springframework.web.context.support.WebApplicationContextUtils;importorg.springframework.beans.BeansException;importorg.springframework.beans.factory.NoSuchBeanDefinitionException;importflex.messaging.FactoryInstance;importflex.messaging.FlexFactory;importflex.messaging.config.ConfigMap;importflex.messaging.services.ServiceException;publicclassSpringFactoryimplementsFlexFactory{privatestaticfinalStringSOURCE=source;publicvoidinitialize(Stringid,ConfigMapconfigMap){}publicFactoryInstancecreateFactoryInstance(Stringid,ConfigMapproperties){SpringFactoryInstanceinstance=newSpringFactoryInstance(this,id,properties);instance.setSource(properties.getPropertyAsString(SOURCE,instance.getId()));returninstance;}//endmethodcreateFactoryInstance()publicObjectlookup(FactoryInstanceinst){SpringFactoryInstancefactoryInstance=(SpringFactoryInstance)inst;returnfactoryInstance.lookup();}staticclassSpringFactoryInstanceextendsFactoryInstance{SpringFactoryInstance(SpringFactoryfactory,Stringid,ConfigMapproperties){super(factory,id,properties);}publicStringtoString(){returnSpringFactoryinstanceforid=+getId()+source=+getSource()+scope=+getScope();}publicObjectlookup(){ApplicationContextappContext=WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());StringbeanName=getSource();try{returnappContext.getBean(beanName);}catch(NoSuchBeanDefinitionExceptionnexc){ServiceExceptione=newServiceException();Stringmsg=Springservicenamed'+beanName+'doesnotexist.;e.setMessage(msg);e.setRootCause(nexc);e.setDetails(msg);e.setCode(Server.Processing);throwe;}catch(BeansExceptionbexc){ServiceExceptione=newServiceException();Stringmsg=UnabletocreateSpringservicenamed'+beanName+';e.setMessage(msg);e.setRootCause(bexc);e.setDetails(msg);e.setCode(Server.Processing);throwe;}}}}在flex视图下:在WEB-INF/flex中的services-config.xml中注册刚编写的SpringFactory工厂factoriesfactoryid=springclass=com.surekam.project.common.SpringFactory//factories在myeclipse视图下:.在WEB-INF/web.xml中加入对spring的支持!--Backendconfiguration--context-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/classes/applicationContext.xml/param-value/context-paramservletservlet-nameSpringContextServlet/servlet-nameservlet-classorg.springframework.web.context.ContextLoaderServlet/servlet-classload-on-startup1/load-on-startup/servletservletservlet-nameSpringLog4jConfigServlet/servlet-nameservlet-classorg.springframework.web.util.Log4jConfigServlet/servlet-class/servletservletservlet-nameweb/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class/servlet7添加hibernate添加数据源加进hibernate支持在myEclipse视图下:右键MyEclipes-AddHibernate-Next--Next-Next----Next把那个不选择点击完成;出现datasource类找不到的问题需要为hibernate提供spring的支持在myeclipse
本文标题:flex整合spring,hibernate例子完整版
链接地址:https://www.777doc.com/doc-6152523 .html