关闭

如何将ASObject转换为JAVA对象

2100人阅读 评论(0) 收藏 举报
我试过不建立As对象也可以实现java对象的转换
flex相关jar包中java对象转换到AS对象中间有个ASObject(一个java类继承MAP)他把java类的属性读出放到其Map映射返回到flex前台就是ASObject了
就是说你new 一个ASOBject 按照MAP设置好属性,值 到前台就是一个ASObject对象可以直接用该对象取值

总结就是:
flex对象传到java(如果没经过绑定强转)都看成ASObject 其为一个Map型集合可以通过类属性充当Key从中取值
flex中集合传来都看成flex.messaging.io.ArrayCollection该集合实现List接口传递给flex flex可以直接识别
flexObjec <--> ASObject(flex.messaging.io.amf.ASObject) <---> javaObject
flexArray <--> ArrayCollection(flex.messaging.io.ArrayCollection) <---> javaArray(List)

例:我查询数据库返回ASObject到flex
import flex.messaging.io.ArrayCollection;

public ArrayCollection getSqlData(String sql)
    {
       ArrayCollection asList = new ArrayCollection(jdbc.getSqlData(sql));  
        return asList;
    }
这是我自己写的一个类尝试转换的 时间久远那些方法测试过没我也忘了(基于Spring相关类)


Java代码  收藏代码
  1. /** 
  2.               * flex类未指定对应java对象 
  3.               *  
  4.               * 将flex提交于java后台的数据对应到java对象中 
  5.               * @param fromObj 
  6.               * @param toObj 
  7.               */  
  8.              public static void flexObjectToVo(ASObject fromObj,Object toObj)  
  9.              {  
  10.                  Assert.notNull(fromObj, "Flex对象为空");  
  11.                  Assert.notNull(toObj, "java对象为空");  
  12.                  PropertyDescriptor targetPds[] = BeanUtils.getPropertyDescriptors(toObj.getClass());  //遍历获取java对象全部属性  
  13.                  for (Object obj : targetPds)  //循环遍历属性  
  14.                  {  
  15.                      PropertyDescriptor targetPd = (PropertyDescriptor)obj;  
  16.                      if(targetPd.getWriteMethod() == null//获取该属性的set方法如果无则继续下次循环  
  17.                          continue;  
  18.                        
  19.                      //有set方法的属性 vo的正常属性  
  20.                      try  
  21.                      {  
  22.                      Object value = fromObj.get(targetPd.getName()); //flex提交的值  
  23.                      Method writeMethod = targetPd.getWriteMethod();  
  24.                      if(!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()))  
  25.                          writeMethod.setAccessible(true); //设置到Vo  
  26.                      writeMethod.invoke(toObj, new Object[] {value});  
  27.                      }  
  28.                      catch(Throwable e)  
  29.                      {  
  30.                          System.out.println("从flex拷值到java对象错误"+e.getMessage()+"\n 当前造成错误的属性"+targetPd.getName());  
  31.                          continue//当前属性为空 继续赋值  
  32.                      }  
  33.                       
  34.                  }  
  35.              }  
  36.                
  37.                
  38.              /** 
  39.               * flex类未指定对应java对象 
  40.               *  
  41.               * 将flex提交于java后台的数据对应到java对象中 
  42.               * @param fromObj 
  43.               * @param toObj 
  44.               */  
  45.              public static Object getflexObjectToVo(ASObject fromObj,Object toObj)  
  46.              {  
  47.                  Assert.notNull(fromObj, "Flex对象为空");  
  48.                  Assert.notNull(toObj, "java对象为空");  
  49.                  PropertyDescriptor targetPds[] = BeanUtils.getPropertyDescriptors(toObj.getClass());  //遍历获取java对象全部属性  
  50.                  for (Object obj : targetPds)  //循环遍历属性  
  51.                  {  
  52.                      PropertyDescriptor targetPd = (PropertyDescriptor)obj;  
  53.                      if(targetPd.getWriteMethod() == null//获取该属性的set方法如果无则继续下次循环  
  54.                          continue;  
  55.                        
  56.                      //有set方法的属性 vo的正常属性  
  57.                      try  
  58.                      {  
  59.                      Object value = fromObj.get(targetPd.getName()); //flex提交的值  
  60.                      Method writeMethod = targetPd.getWriteMethod();  
  61.                      if(!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()))  
  62.                          writeMethod.setAccessible(true); //设置到Vo  
  63.                      writeMethod.invoke(toObj, new Object[] {value});  
  64.                      }  
  65.                      catch(Throwable e)  
  66.                      {  
  67.                          System.out.println("从flex拷值到java对象错误"+e.getMessage()+"\n 当前造成错误的属性"+targetPd.getName());  
  68.                          continue//当前属性为空 继续赋值  
  69.                      }  
  70.                       
  71.                  }  
  72.                  return toObj;  
  73.              }  
  74.                
  75.              /** 
  76.               * flex类指定对应java对象 
  77.               *  
  78.               * 将flex提交于java后台的数据对应到java对象中 
  79.               * @param fromObj 
  80.               * @param toObj 
  81.               */  
  82.              public static Object flexObjectToVo(ASObject fromObj)  
  83.              {  
  84.                  Object toObj = null;  
  85.                  try  
  86.                  {  
  87.                   toObj = Class.forName(fromObj.getType()).newInstance();  
  88.                  }catch (Exception e)  
  89.                  {  
  90.                      System.out.println("从flex指定类型中获取java类失败"+e.getMessage());  
  91.                      return null;  
  92.                  }  
  93.                    
  94.                  Assert.notNull(fromObj, "Flex对象为空");  
  95.                  Assert.notNull(toObj, "java对象为空");  
  96.                  PropertyDescriptor targetPds[] = BeanUtils.getPropertyDescriptors(toObj.getClass());  //遍历获取java对象全部属性  
  97.                  for (Object obj : targetPds)  //循环遍历属性  
  98.                  {  
  99.                      PropertyDescriptor targetPd = (PropertyDescriptor)obj;  
  100.                      if(targetPd.getWriteMethod() == null//获取该属性的set方法如果无则继续下次循环  
  101.                          continue;  
  102.                        
  103.                      //有set方法的属性 vo的正常属性  
  104.                      try  
  105.                      {  
  106.                      Object value = fromObj.get(targetPd.getName()); //flex提交的值  
  107.                      Method writeMethod = targetPd.getWriteMethod();  
  108.                      if(!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()))  
  109.                          writeMethod.setAccessible(true); //设置到Vo  
  110.                      writeMethod.invoke(toObj, new Object[] {value});  
  111.                      }  
  112.                      catch(Throwable e)  
  113.                      {  
  114.                          System.out.println("从flex拷值到java对象错误"+e.getMessage()+"\n 当前造成错误的属性"+targetPd.getName());  
  115.                          continue//当前属性为空 继续赋值  
  116.                      }  
  117.                       
  118.                  }  
  119.                    
  120.                  return toObj;  
  121.              }  
  122.                
  123.              /** 
  124.               * 将flex传递与java的集合转换成toObj形式并以集合返回 
  125.               * @param ASOList 
  126.               * @param toObj 
  127.               * @return 
  128.               */  
  129.              public static List flexObjectToVo(List ASOList,Object toObj)  
  130.              {  
  131.                  Assert.notNull(toObj, "java对象为空");  
  132.                  List<Object> returnList = new LinkedList<Object>();  
  133.                  for ( int i = 0 ; i < ASOList.size(); i++)  
  134.                  {  
  135.                      ASObject asObj = (ASObject)ASOList.get(i);  
  136.                      returnList.add(getflexObjectToVo(asObj,toObj));  
  137.                  }  
  138.                  return returnList;  
  139.              }  
  140.                
  141.              /** 
  142.               * 将结果集放到as对象 
  143.               * @param asObject 
  144.               * @param rs 
  145.               */  
  146.              public static void getChartFlexList(ASObject asObject,ResultSet rs)  
  147.              {  
  148.                  try {  
  149.                     int ci = rs.getMetaData().getColumnCount();  
  150.                     for (int i = 1 ; i <=ci ; i++)  
  151.                     {             
  152.                         asObject.put(rs.getMetaData().getColumnName(i), rs.getObject(i));         
  153.                     }  
  154.                 } catch (SQLException e) {  
  155.                     System.out.println("sql 异常"+e.getMessage());  
  156.                 } //总列数  
  157.              }  
  158.                
  159.                
  160.              /** 
  161.               * 将结果集放到as集合中 这样实现结果集有序及重复 
  162.               * @param asObject 
  163.               * @param rs 
  164.               */  
  165.              public static void getChartFlexList(ArrayCollection asList,ResultSet rs)  
  166.              {  
  167.                  try {  
  168.                     int ci = rs.getMetaData().getColumnCount();  
  169.                       
  170.                     for (int i = 1 ; i <=ci ; i++)                         
  171.                     {     
  172.                         ASObject asObject = new ASObject();   
  173.                         asObject.put(rs.getMetaData().getColumnName(i), rs.getObject(i));     
  174.                         asList.add(asObject);  
  175.                     }  
  176.                 } catch (SQLException e) {  
  177.                     System.out.println("sql 异常"+e.getMessage());  
  178.                 } //总列数  
  179.              } 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:59495次
    • 积分:530
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:13篇
    • 译文:0篇
    • 评论:10条
    文章分类
    最新评论
    Flex