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
java

               xalan.jar    web

 (2)而後要加入Flex BlazeDS須要的配置文件。在WEB-INF下新建一個名爲flex的文件夾,而後將四個xml文件(messagin-config.xml/proxy-config.xml/remoting-config.xml/services-config.xml)拷到該文件夾下。spring

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

<context-param> 服務器

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

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

</context-param> webapp

<!-- 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, id, 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

相關文章
相關標籤/搜索