Flex与Web应用程序整合技术分享

1.   采用BlazeDS进行java应用程序与flex应用程序间的通信。

(1)          在官网上下载blazeds的开发包,最好下载完整版,里面有一个例子和所有我们所需的所有文件(我下载的是blazeds-turnkey-4.0.0.14931版)。blazeds.wards-console.warsamples.war三个文件放在tomcatwebapps目录下

                               flex-messaging-common.jar
 flex-messaging-core.jar
 flex-messaging-opt.jar
 flex-messaging-proxy.jar
 flex-messaging-remoting.jar
 backport-util-concurrent.jar
 cfgatewayadapter.jar
 commons-httpclient-3.0.1.jar
 commons-codec-1.3.jar
 commons-logging.jar
 concurrent.jar

               xalan.jar    

 (2)然后要加入Flex BlazeDS需要的配置文件。在WEB-INF下新建一个名为flex文件夹,然后将四个xml文件messagin-config.xml/proxy-config.xml/remoting-config.xml/services-config.xml)拷到该文件夹下。

(3)    修改web.xml文件,加入Flex的配置(直接拷贝即可)

<context-param>

<param-name>flex.class.path</param-name>

<param-value>/WEB-INF/flex/hotfixes,/WEB-INF/flex/jars</param-value>

</context-param>

<!-- Http Flex Session attribute and binding listener support -->

<listener>

<listener-class>flex.messaging.HttpFlexSession</listener-class>

</listener>

<!-- Messagebroker Servlet -->

<servlet>

<servlet-name>MessagebrokerServlet</servlet-name>

<display-name>MessagebrokerServlet</display-name>

<servlet-class>flex.messaging.MessagebrokerServlet</servlet-class>

<init-param>

<param-name>services.configuration.file</param-name>

<param-value>/WEB-INF/flex/services-config.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>MessagebrokerServlet</servlet-name>

<url-pattern>/messagebroker/*</url-pattern>

</servlet-mapping>


以上步骤需在java项目中完成

2.Flex采用Cairngorm作为前端架构

(1)         Flex Builder中新建一J2EE项目,并配置服务器路径(在tomcat/webapp/目录下,需关联一web项目)

(2)         Flex Build Path中引入Cairngorm.swc,查看是否引用了正确的sdk包,如不正确,需重新引用。

(3)         按照Cairngorm架构,在src目录下分别创建bussinesscommandeventmodelview文件夹。

其中,view为视图层,model为模型层,event为事件,command为业务逻辑层,bussiness中需创建delegate(中间人角色,command调用web service获得数据时,需创建一个delegate完成,一对一关系),Front Controller注册eventcommand的对应关系)和services.mxml(服务定义)。

具体调用过程如下:

n       首先我们要了解我们的数据

n       我们将使用数据来定义View

n       我们将使用View来定义可能的用户动作

n       用户动作将会转化(转变)为Events

n       Event将会被映射到Command

n       Command可能被映射到Delegate

n       Delegate映射到Service

n       Command将使用Service传回的结果更新Model Locator

3.   FlexSpring整合

            (1)         首先,需要有一个加载bean的工厂类,我的项目中为SpringFactory.java,类中的代码固定

public class SpringFactory implements FlexFactory{

        

     //定义一个常量资源

     private static final String SOURCE = "source";  

     

     

     public void initialize(String id,ConfigMap configMap) {

      

     } 

     /**

          * This method is called when we initialize the deFinition of an instance

          * which will be looked up by this factory.  It should validate that

          * the properties supplied are valid to define an instance.

          * Any valid properties used for this configuration must be accessed to

          * avoid warnings about unused configuration elements.  If your factory

          * is only used for application scoped components, this method can simply

          * return a factory instance which delegates the creation of the component

          * to the FactoryInstance's lookup method.

          */

     //创建一个factory实例

     public FactoryInstance createFactoryInstance(String id,ConfigMap properties) {

     //生成一个Spring的实例

     SpringFactoryInstance instance = new SpringFactoryInstance(this,id,properties);

     

         

           instance.setSource(properties.getPropertyAsstring(SOURCE,instance.getId()));  

            return instance;  

     }

 

     

     public Object lookup(FactoryInstance inst)  

        {  

            SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;  

            return factoryInstance.lookup();  

        }   

     

     //内部静态类

     static class SpringFactoryInstance extends FactoryInstance  

        {  

     //内部类构造函数

            SpringFactoryInstance(SpringFactory factory,String id,ConfigMap properties)  

            {  

                super(factory,properties);  

            }   

           //用于测试

            public String toString()  

            {  

                return "SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope();  

            }  

            //查询

            public Object lookup()   

           {  

             //这就是从spring容器中getbean

                ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());  

                String beanName = getSource();   

     

                try 

                {  

                    return appContext.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;  

                }   

            }

     

        }

(2)         Spring配置文件注册相应flex实现接口(跟正常配置spring一样)

               (3)         Services-config.xml中加入如下代码

 

<factories>

<factory id="spring"class="flexHandle.SpringFactory"/>

</factories>

指定flex加载bean的工厂类的路径

        (4)         最后,在remoting-config.xml中加入如下代码

<destination id="Client2Server">

            <properties>

                <factory>spring</factory>//Flex工厂类

            <source>clienToServerManager</source>//Flex接口bean

            </properties>

 </destination>

 

 

参考文档:《整合FlexJava—配置篇》

               《基于CairngormFlex应用程序设计》

参考网站:http://blog.dreamhui.net/archives/64

         http://www.iteye.com/topic/180613

         http://www.oschina.net/question/12_7654

相关文章

一:display:flex布局display:flex是一种布局方式。它即可以...
1. flex设置元素垂直居中对齐在之前的一篇文章中记载过如何...
移动端开发知识点pc端软件和移动端apppc端软件是什么,有哪些...
最近挺忙的,准备考试,还有其他的事,没时间研究东西,快周...
display:flex;把容器设置为弹性盒模型(设置为弹性盒模型之后...
我在网页上运行了一个Flex应用程序,我想使用Command←组合键...