flex3 和 java (web)的组合开发

 1、开发环境:在myeclipse7.0中整合flex plup3.0, 安装flex plup3.0时,选myeclipse 中eclipse的目录,然

      找到flex 的安装好的目录,将plugins和features对应到拷到myeclipse里面 eclipse相应的目录下。

2、创建 flex和 java (Web)的工程(通信框架用blazeds.war):

               1、在myeclipse7下转到flex开发环境,创建flex工程 flexssh:

               2、右键创建好的flex 工程,为flex添加 web能力,使之有web能力,才可以使用ssh框架.

 二、flex和java的通信

  1. 写一个java类:

package com.qili.flexssh.test;
import com.qili.flexssh.util.UploadFile;
public class Hello {
 public String say(String yourworld){
  System.out.println("at:"+UploadFile.getNowDatetime()+" you say:"+yourworld);
  return "at:"+UploadFile.getNowDatetime()+" you say:"+yourworld;
 }
}

  1. 在WebRoot/WEB-INF/flex/remoting-config.xml 下添加  

<destination id="hello_java_class"> <!-- flex mxml页面要调用的ID -->
        <properties>
         <source>com.qili.flexssh.test.Hello</source> <!-- java中的类 -->
        </properties>
   </destination>

       

 

    3) 页面写 flex程序

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script>
  <![CDATA[
   import mx.events.ResourceEvent;
   import mx.controls.Alert;
   import mx.rpc.events.ResultEvent;
   [Bindable]private var returnmess:String;  //获取java类里面的方法回调的值
   function sendMess():void{
    var myworld:String=txtinput.text;   //获取页面的值
    now_thispage_hello.say(myworld);    //调用 <mx:RemoteObject 的ID 里面的say方法(也是java类中的方法)
   }
   function getReturnMess(event:ResultEvent):void{   //<mx:RemoteObject 里面的result方法
    returnmess=event.result as String;
    lbshow.text=returnmess;   //在页面上显示
   }
  ]]>
 </mx:Script>
 <!-- 这个是与java通信的关键  id是在当前页在调用的ID;destination里面属性是 在 remoting-config.xml里面定义的bean
 endpoint里面的/flexssh/messagebroker/amf 第一个是当前项目的名称(flexssh)
 
  -->
 <mx:RemoteObject id="now_thispage_hello" destination="hello_java_class" endpoint="/flexssh/messagebroker/amf">
     <!-- 如果java类中有多个方法,这里可以写多个,每一个为java类中的方法名,第二个是当前页面的返回方法 -->
     <mx:method name="say" result="getReturnMess(event)" />
    </mx:RemoteObject>
 <mx:Button x="165" y="158" label="say hello" id="btnhello" click="sendMess()"/>
 <mx:TextInput x="61" y="203" id="txtinput" fontSize="12" keyUp="sendMess()" width="269" height="47"/>
 <mx:Text x="144" y="300" id="lbshow"  fontSize="14" color="#FFFCFB" fontWeight="bold"/>
 
</mx:Application>

 

三、flex与spring之间的组合应用

      flex和spring的通讯要用到自己的自定义 flexspring

      1、创建flex工厂,要用到两个自定义类 SpringFactoryInstance 和 FlexSpringFactory

第一个类

package com.qili.flexssh.util.flex;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import flex.messaging.FactoryInstance;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;

public class SpringFactoryInstance extends FactoryInstance
{
    SpringFactoryInstance(FlexSpringFactory factory, String id, ConfigMap properties)
    {
        super(factory, id, properties);
    }
    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();
            throw e;
        }
        catch (BeansException bexc)
        {
            ServiceException e = new ServiceException();
            throw e;
        }
    }
   
}

 

第二个类

package com.qili.flexssh.util.flex;

import flex.messaging.FactoryInstance; 
import flex.messaging.FlexFactory; 
import flex.messaging.config.ConfigMap; 
 
/**
 * 要对某个java类实现自己定义的切入,可以使用工厂配置,需要在ro访问前,从spring里去getbean<br>
 * 
 * @author Libin
 *
 */ 
public class FlexSpringFactory implements FlexFactory{

 public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
  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(); 
 }

 public void initialize(String arg0, ConfigMap arg1) {
  // TODO Auto-generated method stub
  
 }  
   
}

2、在 WebRoot/WEB-INF/flex/services-config.xml 定义自己的 flex  springFactory

    <!-- 自定义的Spring工厂 -->
    <factories> 
      <factory id="springFactory" class="com.qili.flexssh.util.flex.FlexSpringFactory" />  
    </factories>

3、定义自己的flex server

package com.qili.flexssh.flexserver;
import java.util.List;
import com.qili.flexssh.obj.Goods;
public interface IFlexGoodsServer {
   public List getAllGoods();
   public boolean addGoods(Goods g);

     }
4、写实现的类

 package com.qili.flexssh.flexserver.impl;
import java.util.List;
import com.qili.flexssh.obj.Goods;
import com.qili.flexssh.obj.GoodsDAO;
import com.qili.flexssh.server.IGoodsServer;
import com.qili.flexssh.util.myUtil;
public class FlexGoodsServer implements IGoodsServer {
 private GoodsDAO goodsDAO;
 public GoodsDAO getGoodsDAO() {
  return goodsDAO;
 }
 public void setGoodsDAO(GoodsDAO goodsDAO) {
  this.goodsDAO = goodsDAO;
 }
 public void addGoods(Goods g) {
  goodsDAO.save(g);
 }
 public List getAllGoods() {
     myUtil.println(myUtil.getNowDatetime()+" is execu", 2);
  return goodsDAO.findAll();
 }
}

5、 在WebRoot/WEB-INF/flex/remoting-config.xml 下添加

   <destination id="flexgetallgoods_java">     <!-- 供flex页面调用的ID-->
     <properties>    
         <factory>springFactory</factory>   <!--   自己定义在 servers-config.xml的  bean ID-->
        <source>FlexGoodsServer</source>    <!-- 调  applicationContext-flexserver中定义的ID-->
     </properties>   
  </destination> 

6、编写页面程序

 

   <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.controls.AdvancedDataGrid;
   import mx.collections.ArrayCollection;
   import mx.rpc.events.ResultEvent;
   function getreurnAllgood(event:ResultEvent):void{ //返回的类型
    var arrgoods:ArrayCollection=event.result as ArrayCollection; 
    datagoods.dataProvider=arrgoods;
   }
   function get_allgoods():void{   //发出请求,调用java类中的方法
    goodsServer.getAllGoods();  //写到gridview控件中
   }
   
  ]]>
 </mx:Script>
  <mx:RemoteObject id="goodsServer" destination="flexgetallgoods_java" endpoint="/flexssh/messagebroker/amf">
     <mx:method name="getAllGoods" result="getreurnAllgood(event)" />
     
    </mx:RemoteObject>
 
 <mx:Button x="10" y="240" label="获取" fontSize="12" click="get_allgoods()"/>
 <mx:DataGrid x="10" y="272" id="datagoods">
  <mx:columns>
   <mx:Array>
    <mx:DataGridColumn dataField="id" headerText="ID"/>
    <mx:DataGridColumn dataField="goodsname" headerText="产品名" />
    <mx:DataGridColumn dataField="price" headerText="价格" />
    <mx:DataGridColumn dataField="proaddress" headerText="产地" />
   </mx:Array>
  </mx:columns>
 </mx:DataGrid>
</mx:Application>

 

7、完成。

你可能感兴趣的:(flex3 和 java (web)的组合开发)