flex+spring+blazeds在MyEclipse下非常详细的结合配置

    技术2022-05-13  3

    1.创建JavaWeb工程FlexSpring。

    2.加入Spring2.5,选择core和web两项,确定。

    3.加入BlazeDS,将blazeds.war用解压缩软件解压,得到WEB-INF和META-INF两文件夹,将这两个文件夹复制到WebRoot下替换工程下原来的文件。完成的工程结构如下:

    4.将工程发布到服务器,并启动服务器(添加Flex工程时验证服务需要用到);

    5.加入Flex工程,右击工程->Flex Project Natrue -> Add Flex Project Natrue ,弹出如下图的窗口,在弹出窗口中选择Application server type为J2EE,然后下一步。

     

    6.填写flex工程属性配置:去掉复选框的选择在相应的输入框中添加相应信:1)第一项为服务器下工程的目录2)访问工程的URl路径3)工程的名字,填写完毕之后点击Validate Configuration 按钮进行验证,正确后点击finish。

    7.完成后工程会报个错误,这是因为Flex工程的html模板文件没有,解决办法是打开Myeclipse的problems窗口,在Error中找到”Cannot create HTML wrapper. Right-click here to recreate folder html “ 然后右键-》recreate Html template,Ok搞定。

    8.接下来让我们规划一下工程结构,使Flex源文件与Java源文件分开管理,src.java文件夹用来存放Java源文件,src.flex文件夹用来存放Flex原文件。完成的工程结构如下:

     

    9.将下面的类加入你的src目录下:

    package com.company; import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import flex.messaging.FactoryInstance; import flex.messaging.FlexContext; import flex.messaging.FlexFactory; import flex.messaging.config.ConfigMap; import flex.messaging.services.ServiceException; public class SessionFactory4Flex implements FlexFactory { private static final String SOURCE = "source"; private FactoryInstance instance; public void initialize(String id, ConfigMap configMap) { instance = new FactoryInstance(this,id,configMap); } public FactoryInstance createFactoryInstance(String id, ConfigMap properties) { instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId())); return instance; } public Object lookup(FactoryInstance inst) { ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(FlexContext.getServletContext()); String beanName = instance.getSource(); try { return context.getBean(beanName); } catch (NoSuchBeanDefinitionException nexc) { ServiceException e = new ServiceException(); String msg = "Spring service named '" + beanName + "' does not exist."; e.setMessage(msg); e.setRootCause(nexc); e.setDetails(msg); e.setCode("Server.Processing"); throw e; } catch (BeansException bexc) { ServiceException e = new ServiceException(); String msg = "Unable to create Spring service named '" + beanName + "' "; e.setMessage(msg); e.setRootCause(bexc); e.setDetails(msg); e.setCode("Server.Processing"); throw e; } } }

    10.将下面的代码加入到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>

    11.将下面的代码添加到web-inf下的services-config.xml中。

    <factories> <factory id="spring" class="com.SpringFactory4Flex" /> </factories>  

    12.接下来我问写一个简单的Flex前端进行测试。

    <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12"> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.rpc.events.ResultEvent; private function doClick():void{ remoteServer.sayHello(username.text,password.text); } private function getSayHelloResult(event:ResultEvent):void{ mes.text = event.result as String } private function reset():void{ username.text = ""; password.text = ""; } ]]> </mx:Script> <mx:RemoteObject destination="server" id="remoteServer" showBusyCursor="true"> <mx:method name="sayHello" result="getSayHelloResult(event)"/> </mx:RemoteObject> <mx:Panel x="208" y="92" width="272" height="178" layout="absolute" title="登录窗口"> <mx:Button x="80" y="70" label="登陆" click="doClick()"/> <mx:Button x="188" y="70" label="取消" click="reset()"/> <mx:TextInput x="80" y="10" id="username"/> <mx:Label x="10" y="12" text="用户名:"/> <mx:Label x="10" y="40" text="密 码:"/> <mx:TextInput x="80" y="38" id="password" displayAsPassword="true"/> <mx:Label id="mes" x="10" y="102" text="请你登陆。。。" width="230" color="#FB020E" fontSize="14" textAlign="center"/> </mx:Panel> </mx:Application>

    13.编写后台Java代码。

     package com.company; public class Server { public String sayHello(String userName,String password){ if(userName.equals("litter") && password.equals("123456")) return "你好"+userName+"先生(女士)!"; return "对不起,你的用户名或密码错误!"; } }

    14.配置remoting-config.xml文件。将下面的代码remoting-config.xml加入文件中。

    <destination id="server"> <properties> <factory>spring</factory> <source>server</source><!--此处为你的bean名字--> </properties> </destination>

     15.加入<bean id="server" class="com.company.Server" />到applicationContext.xml中。

     16.运行。运行截图。如下:

     源码下载


    最新回复(0)