自定义对象的 BlazeDS 和 ArrayList

发布于 2024-07-17 09:12:12 字数 564 浏览 2 评论 0原文

我正在使用 BlazeDS 将 Flex 与 Java 连接起来。 我在将自定义对象的 ArrayLists 从 Flex 传递到 java 时遇到问题。

我有两个对象,一个称为类别,另一个称为部分。 类别有一个包含Section 对象的ArrayList。 我可以在 Flex 和 Java 之间来回发送类别对象的 ArrayList,问题是当我尝试访问已从 Flex 返回到 Java 的类别对象的部分 ArrayList 时,出现以下错误:

flex.messaging.MessageException: java.lang.ClassCastException : flex.messaging.io.amf.ASObject

由于某种原因,我我得到的是 ASObjects 的 ArrayList 而不是我的部分对象。 我尝试查找如何在 ActionScript 中显式键入数组,但我唯一能找到的是使用 Vector 对象,而 BlazeDS 不支持该对象。 是否可以在 Category 对象的 ArrayList 中传递 Section 对象的 ArrayList,还是我必须找到其他方法?

I'm using BlazeDS to connect Flex with Java. I'm having trouble passing ArrayLists of custom objects from Flex to java.

I have two objects, one is called Category, the other Section. A Category has an ArrayList of Section objects. I can send an ArrayList of Category objects back and forth between Flex and Java, the problem is when I try to access the sections ArrayList of a Category object that has been returned to Java from Flex, I get the following error:

flex.messaging.MessageException: java.lang.ClassCastException : flex.messaging.io.amf.ASObject

For some reason I'm getting an ArrayList of ASObjects rather than my Section objects. I tried looking up how to explicitly type arrays in actionscript, but the only thing I could find was using a Vector object, which BlazeDS does not support. Is it possible to pass an ArrayList of Section objects within an ArrayList of Category objects, or do I have to find another way around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜还是长夜 2024-07-24 09:12:12

对 AS3 最常见的抱怨之一是缺少类型化数组。 ArrayList 只包含对象,您必须自己转换结果。

下面是我将传递的 Java 和 AS3 类的示例。

在 Java 中:

顶级类:

package mystuff;

public class StuffToSend
{
    public List<Section> sections;
    ...
}

Sections 类:

package mystuff;

public class Section
{
    public List<Catagory> categories;
    ...
}

Category 类:

package mystuff;

public class Category
{
    ...
}

在 AS3 中:

package mystuff
{
    [RemoteClass(alias="mystuff.StuffToSend")] // So AS3 knows which Java class to map
    public class StuffToSend
    {
        public var sections:ArrayCollection;
        ...
    }
}

package mystuff 
{
    [RemoteClass(alias="mystuff.Section")] // So AS3 knows which Java class to map
    public class Section 
    {
        public var categories:ArrayCollection;
        ...
    }
}

package mystuff 
{
    [RemoteClass(alias="mystuff.Category")] // So AS3 knows which Java class to map
    public class Category
    {
        ...
    }
}  

您可以在此处了解有关 RemoteObjects 的更多信息: 数据访问

One of the most common complaints with AS3 is the lack of typed arrays. ArrayLists will only contain objects, you will have to cast the results yourself.

Here is an example of a Java and AS3 class that I would pass around.

In Java:

The top level class:

package mystuff;

public class StuffToSend
{
    public List<Section> sections;
    ...
}

Sections class:

package mystuff;

public class Section
{
    public List<Catagory> categories;
    ...
}

Category class:

package mystuff;

public class Category
{
    ...
}

In AS3:

package mystuff
{
    [RemoteClass(alias="mystuff.StuffToSend")] // So AS3 knows which Java class to map
    public class StuffToSend
    {
        public var sections:ArrayCollection;
        ...
    }
}

package mystuff 
{
    [RemoteClass(alias="mystuff.Section")] // So AS3 knows which Java class to map
    public class Section 
    {
        public var categories:ArrayCollection;
        ...
    }
}

package mystuff 
{
    [RemoteClass(alias="mystuff.Category")] // So AS3 knows which Java class to map
    public class Category
    {
        ...
    }
}  

You can learn more about remoteObjects here: Data Access

╰沐子 2024-07-24 09:12:12

Flex 实际上发送回了一个 flex.messaging.io.ArrayCollection 对象。 下面是将其转换为我的 java 类的 ArrayList 的代码:

public ArrayList<MyObject> convertArrayCollection(ArrayCollection array){
        ArrayList<MyObject> myObjectArray = new ArrayList();
        ASTranslator ast = new ASTranslator();
        MyObject myObject;
        ASObject aso;

        for (int i=0;i< array.size(); i++){
            myObject = new MyObject();
            aso = new ASObject();

            aso = (ASObject) array.get(i);
            aso.setType("com.myPackage.MyObject");
            myObject = (MyObject) ast.convert(aso, MyObject.class);
            myObjectArray.add(myObject);
        }
        return myObjectArray;
    }

Flex was actually sending back a flex.messaging.io.ArrayCollection object. Below is the code to convert this to an ArrayList of my java class:

public ArrayList<MyObject> convertArrayCollection(ArrayCollection array){
        ArrayList<MyObject> myObjectArray = new ArrayList();
        ASTranslator ast = new ASTranslator();
        MyObject myObject;
        ASObject aso;

        for (int i=0;i< array.size(); i++){
            myObject = new MyObject();
            aso = new ASObject();

            aso = (ASObject) array.get(i);
            aso.setType("com.myPackage.MyObject");
            myObject = (MyObject) ast.convert(aso, MyObject.class);
            myObjectArray.add(myObject);
        }
        return myObjectArray;
    }
白首有我共你 2024-07-24 09:12:12

真正的答案是,BlazeDS 很愚蠢,并且需要类引用来将活动脚本对象映射回 Java(即使它刚刚成功地将完全相同的对象从 Java 映射到 AS)。 今天我在完全相同的问题上浪费了相当多的时间。 我有很多类似的映射,它们都工作得很好,但今天我创建了一个新的映射,它开始给我类转换异常。

在这里找到答案:

您的案例解决方案中的链接将是:

package mystuff
{
    [RemoteClass(alias="mystuff.Section")] 
    public class Section
    {
        private var stupidBlazeDs : Category;
        public var categories:ArrayCollection;
    ...
    }
}

可能有更好的选择,但我今天已经够了。

the real answer is, that BlazeDS is stupid, and requires class reference to map your active script object back into Java (even if it just successfully mapped exactly the same object from Java to AS). I wasted quite some time on exactly the same problem today. I had quite a few similar mappings and they all worked fine, but today I created a new one, and it started to give me class cast exception.

found an answer here: Link

in your case solution would be:

package mystuff
{
    [RemoteClass(alias="mystuff.Section")] 
    public class Section
    {
        private var stupidBlazeDs : Category;
        public var categories:ArrayCollection;
    ...
    }
}

there might be better options but I had enough for today.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文