论坛首页 编程语言技术论坛

FlexPetStore--集成Flex和Spring (2)

浏览 3961 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-05-22  
Flex部署在Flex Builder帮助中有详细说明(如部署目录结构)。遗憾的是没有例子介绍Flex如何集成Spring。本文尝试解释这部分内容。由于机器重装,导致FlexPetStore的项目丢失,幸好是覆盖安装留下了可运行的WAR。

FlexPetStore通过RemoteObject技术访问JPetStoreFacade业务对象(引入一个FacadeAccessor对象),因此使用了Flex Data Service 2.0。

大致流程:

Flex (ActionScript )<--> AMF协议(AMF协议编码)<-->(解码)<-->FacadeAccessor (Java) <--> PetStoreFacade (Java)<--> ...

看了一下 FDS 2.0 提供的几个应用WAR,发现只要按照Flex的部署思路,在现有WAR基础上适当修改就可以集成Flex和Spring。首先需要把flex.war该名为flexpetstore.war,现在按照一下介绍逐步修改。

Spring集成

1. 加入Lib库。把spring.jar和jpetsotre.jar放到 flexpetstore\WEB-INF\lib下,这是运行Spring和JPetstore的必备包。
2. 修改flexpetstore.war的web.xml,经过一番对比和试验找到两个段落需要修改。
   a. 添加web应用根目录和Spring应用上下文以及数据访问资源配置。
   b. 添加Remoting、web service。struts部分极有可能多余,因为没有action来Request。有待测试。
   c. 修改welcome-file-list,FlexPetStore.html是FlexPetStore项目名称
  

到此为止,集成Spring配置基本完成。还有一部分服务端的Java代码作为RemoteObject供Flex访问,下一部分再讲解。

附件是两份web.xml。

FacadeAccessor首先获取
应用context,然后从context得到PetStoreFacade业务对象。
java 代码
 
  1. // Decompiled by DJ v2.9.9.60 Copyright 2000 Atanas Neshkov  Date: 2007-05-22 22:12:45  
  2. // Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!  
  3. // Decompiler options: packimports(3)   
  4. // Source File Name:   FacadeAccessor.java  
  5.   
  6. package org.springframework.samples.jpetstore.domain.logic.flex;  
  7.   
  8. import java.io.*;  
  9. import java.util.List;  
  10. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  11. import org.springframework.samples.jpetstore.domain.*;  
  12. import org.springframework.samples.jpetstore.domain.logic.PetStoreFacade;  
  13. import org.springframework.samples.jpetstore.domain.logic.flex.exception.NotFoundBeanDifinitionException;  
  14. import org.springframework.samples.jpetstore.domain.logic.flex.exception.OutputBeanDifinitionException;  
  15.   
  16. public class FacadeAccessor {  
  17.   
  18.     public FacadeAccessor() {  
  19.         petStoreFacade = null;  
  20.         init();  
  21.     }  
  22.   
  23.     private void init() {  
  24.         try {  
  25.             getPetStoreFacade();  
  26.         } catch (Exception exception) {  
  27.         }  
  28.     }  
  29.   
  30.     private PetStoreFacade getPetStoreFacade()  
  31.             throws NotFoundBeanDifinitionException,  
  32.             OutputBeanDifinitionException {  
  33.         if (petStoreFacade == null) {  
  34.             ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(  
  35.                     "/applicationContext*.xml");  
  36.             String beanDefinitionNames[] = ctx.getBeanDefinitionNames();  
  37.             if (beanDefinitionNames == null || beanDefinitionNames.length < 1)  
  38.                 throw new NotFoundBeanDifinitionException(  
  39.                         "Not found bean difinitions.");  
  40.             petStoreFacade = (PetStoreFacade) ctx.getBean("petStore");  
  41.         }  
  42.         return petStoreFacade;  
  43.     }  
  44.   
  45.     private void outputBeanDefinitionNames(String beanDefinitionNames[])  
  46.             throws OutputBeanDifinitionException {  
  47.         if (beanDefinitionNames != null && beanDefinitionNames.length > 0) {  
  48.             DataOutputStream out = null;  
  49.             try {  
  50.                 out = new DataOutputStream(new BufferedOutputStream(  
  51.                         new FileOutputStream("c:\\beandifinitions.txt")));  
  52.                 for (int i = 0; i < beanDefinitionNames.length; i++)  
  53.                     out.writeBytes((new StringBuilder(String  
  54.                             .valueOf(beanDefinitionNames[i]))).append("\r\n")  
  55.                             .toString());  
  56.   
  57.                 out.close();  
  58.             } catch (IOException e) {  
  59.                 throw new OutputBeanDifinitionException((new StringBuilder(  
  60.                         "error occurs when output bean difinitions to file,"))  
  61.                         .append(e.getMessage()).toString());  
  62.             }  
  63.         }  
  64.     }  
  65.   
  66.     public Account getAccount(String username) {  
  67.         return petStoreFacade.getAccount(username);  
  68.     }  
  69.   
  70.     public Account getAccount(String username, String password) {  
  71.         return petStoreFacade.getAccount(username, password);  
  72.     }  
  73.   
  74.     public void insertAccount(Account account) {  
  75.         petStoreFacade.insertAccount(account);  
  76.     }  
  77.   
  78.     public void updateAccount(Account account) {  
  79.         petStoreFacade.updateAccount(account);  
  80.     }  
  81.   
  82.     public List getUsernameList() {  
  83.         return petStoreFacade.getUsernameList();  
  84.     }  
  85.   
  86.     public List getCategoryList() {  
  87.         return petStoreFacade.getCategoryList();  
  88.     }  
  89.   
  90.     public Category getCategory(String categoryId) {  
  91.         return petStoreFacade.getCategory(categoryId);  
  92.     }  
  93.   
  94.     public List getProductListByCategory(String categoryId) {  
  95.         return petStoreFacade.getProductListByCategory(categoryId);  
  96.     }  
  97.   
  98.     public List searchProductList(String keywords) {  
  99.         return petStoreFacade.searchProductList(keywords);  
  100.     }  
  101.   
  102.     public Product getProduct(String productId) {  
  103.         return petStoreFacade.getProduct(productId);  
  104.     }  
  105.   
  106.     public List getItemListByProduct(String productId) {  
  107.         return petStoreFacade.getItemListByProduct(productId);  
  108.     }  
  109.   
  110.     public Item getItem(String itemId) {  
  111.         return petStoreFacade.getItem(itemId);  
  112.     }  
  113.   
  114.     public boolean isItemInStock(String itemId) {  
  115.         return petStoreFacade.isItemInStock(itemId);  
  116.     }  
  117.   
  118.     public void insertOrder(Order order) {  
  119.         petStoreFacade.insertOrder(order);  
  120.     }  
  121.   
  122.     public Order getOrder(int orderId) {  
  123.         return petStoreFacade.getOrder(orderId);  
  124.     }  
  125.   
  126.     public List getOrdersByUsername(String username) {  
  127.         return petStoreFacade.getOrdersByUsername(username);  
  128.     }  
  129.   
  130.     private static final boolean output = false;  
  131.   
  132.     private static final String OUTPUT_BEANDIFINITIONS_PATHFILE = "c:\\beandifinitions.txt";  
  133.   
  134.     private static final String APPLICATION_CONTEXT_XML = "/applicationContext*.xml";  
  135.   
  136.     private PetStoreFacade petStoreFacade;  
  137. }  
  • web.zip (1.2 KB)
  • 下载次数: 195
  • web.zip (2.7 KB)
  • 下载次数: 229
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics