zoukankan      html  css  js  c++  java
  • 如何用BlazeDS前后台数据交互

    续上一篇文章如何用BlazeDS更改图片 之后,本篇文章介绍了如何在后台构建数据,并传送到前台。一般来讲有两种方式:构造XML数据,传送到前台后,通过XMLSerializer反序列化到DataBox中;后台构建Java对象,通过BlazeDS传送到前台后,映射成TWaver Flex的数据对象,然后直接添加到DataBox中。这里只介绍后面这种RemoteObject的方式,XML的方式将在后续文章中介绍。

    先来看看技术点:BlazeDS的序列化机制。Flex的类只要指定[RemoteClass(alias="common.CNode")],或者registerClassAlias(“common.CNode”, CNode),就可以将Java类映射成Flex类。public的变量,或者有get和set方法的变量都将自动映射。但有些类型的变量映射就会出问题,比如Point:java和flex的Point类型无法自动映射。因此,自己控制哪些变量序列化,如何序列化相当关键。更多资料请参考官方文档。

    Serializing between ActionScript and Java
    Explicitly mapping ActionScript and Java objects

    下面介绍如何自定义序列化
    1. 创建Java数据类,这里将TWaver Flex的Data、Element和Node分开创建了,以便复用。其他数据类型,比如Link,Group等,大家可以类似创建,可以自己实现试试,练练手。

     

     

    1 public class CData implements Externalizable {
    2 ...
    3 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    4 this.setIcon((String)in.readObject());
    5 this.setName((String)in.readObject());
    6 this.setToolTip((String)in.readObject());
    7 }
    8
    9 public void writeExternal(ObjectOutput out) throws IOException {
    10 out.writeObject(this.getIcon());
    11 out.writeObject(this.getName());
    12 out.writeObject(this.getToolTip());
    13 }
    14 }
    1 public class CElememt extends CData implements Externalizable {
    2 ...
    3 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    4 super.readExternal(in);
    5 this.setLayerID(in.readObject());
    6 }
    7
    8 public void writeExternal(ObjectOutput out) throws IOException {
    9 super.writeExternal(out);
    10 out.writeObject(this.getLayerID());
    11 }
    12 }
    1 public class CNode extends CElememt implements Externalizable {
    2 ...
    3 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    4 super.readExternal(in);
    5
    6 this.setImage((String)in.readObject());
    7 this.setX(in.readDouble());
    8 this.setY(in.readDouble());
    9 this.setWidth(in.readDouble());
    10 this.setHeight(in.readDouble());
    11 }
    12
    13 public void writeExternal(ObjectOutput out) throws IOException {
    14 super.writeExternal(out);
    15
    16 out.writeObject(this.getImage());
    17 out.writeDouble(this.getX());
    18 out.writeDouble(this.getY());
    19 out.writeDouble(this.getWidth());
    20 out.writeDouble(this.getHeight());
    21 }
    22 }

    2. 生成数据,很简单,一个循环模拟了一些数据

    1 public static CNode[] getNodes(){
    2 CNode[] result = new CNode[4];
    3 for(int i=0; i<result.length; i++){
    4 CNode node = new CNode();
    5 node.setName("Node"+i);
    6 node.setX(50*i);
    7 node.setY(50*i);
    8 node.setToolTip("toolTip"+i);
    9 node.setIcon("group_icon");
    10 node.setImage("group_image");
    11 node.setLayerID("Layer"+i);
    12 result[i] = node;
    13 }
    14 return result;
    15 }

    3. 创建Flex数据类,这里注意保持和Java序列化的顺序一致

     

    1 public class CNode extends twaver.Node implements IExternalizable {
    2 ...
    3 public function readExternal(input:IDataInput):void {
    4 //Data
    5   super.icon = input.readObject() as String;
    6 super.name = input.readObject() as String;
    7 super.toolTip = input.readObject() as String;
    8 //parent
    9
    10 //Element
    11 super.layerID = input.readObject();
    12
    13 //Node
    14 super.image = input.readObject() as String;
    15 super.location = new Point(input.readDouble(), input.readDouble());
    16 var Number = input.readDouble();
    17 if(width >= 0){
    18 super.width = width;
    19 }
    20 var height:Number = input.readDouble();
    21 if(height >= 0){
    22 super.height = height;
    23 }
    24 }
    25
    26 public function writeExternal(output:IDataOutput):void {
    27 //Data
    28 output.writeObject(super.icon);
    29 output.writeObject(super.name);
    30 output.writeObject(super.toolTip);
    31 //parent
    32
    33 //Element
    34 output.writeObject(super.layerID);
    35
    36 //Node
    37 output.writeObject(super.image);
    38 output.writeDouble(super.x);
    39 output.writeDouble(super.y);
    40 output.writeDouble(super.width);
    41 output.writeDouble(super.height);
    42 }
    43 }

    4. 加载数据,用上一篇文章封装好的方法一个调用就OK了,另外别忘了用registerClassAlias给Flex类指定别名

     

    1 registerClassAlias("common.CNode", CNode);
    2
    3 private function loadDatas(event:MouseEvent):void {
    4 RemoteObjectUtil.invoke("DataUtil", "getNodes", function(result:Object):void{
    5 var nodes:Array = result as Array;
    6 for each(var node:CNode in nodes){
    7 box.add(node);
    8 }
    9 });
    10 }

    如需源码请留言

  • 相关阅读:
    bzoj 4446: [Scoi2015]小凸玩密室【树形dp】
    bzoj 4403: 序列统计【lucas+组合数学】
    bzoj 3745: [Coci2015]Norma【分治】
    bzoj 3232: 圈地游戏【分数规划+最小割】
    Codeforces 1000 (A~E)
    bzoj 4753: [Jsoi2016]最佳团体【01分数规划+二分+树上背包】
    bzoj 3872: [Poi2014]Ant colony【树形dp+二分】
    bzoj 2067: [Poi2004]SZN【贪心+二分+树形dp】
    洛谷 P1314 聪明的质监员【二分+前缀和】
    bzoj 4622: [NOI 2003] 智破连环阵【dfs+匈牙利算法】
  • 原文地址:https://www.cnblogs.com/twaver/p/2100705.html
Copyright © 2011-2022 走看看