【问题标题】:Error when implementing Enums using Actionscript 3使用 Actionscript 3 实现枚举时出错
【发布时间】:2010-09-22 13:56:56
【问题描述】:

我们的项目是 Flex/Parsley/Blazeds/Spring 项目,我正在尝试在 Actionscript3 中实现 java Enums,我所要做的就是将 Enum 值发送到 Spring 服务方法。

Java 枚举代码(从 XSD 生成)

public enum ReferenceLookupType {
    PATIENT_VISIT_TYPE("PATIENT_VISIT_TYPE"), PATIENT_STATUS(
            "PATIENT_STATUS"), PATIENT_VISIT_INVALID_REASON(
            "PATIENT_VISIT_INVALID_REASON"), LIPID_PREFILLED_CODE(
            "LIPID_PREFILLED_CODE");

 private final String value;

    private ReferenceLookupType(String value) {
        this.value = value;
    }

    public String toString() {
        return value;
    }

    public static ReferenceLookupType convert(String value) {
        for (ReferenceLookupType inst : values()) {
            if (inst.toString().equals(value)) {
                return inst;
            }
        }
        return null;
    }
}

Actionscript 枚举是:

package {

[Bindable]
    [RemoteClass(alias="gov.hhs.cms.ehrds.datacollection.model.ReferenceLookupType")]
    public final class ReferenceLookupType {

        public static const PATIENT_VISIT_TYPE:ReferenceLookupType = new ReferenceLookupType("PATIENT_VISIT_TYPE");
        public static const PATIENT_STATUS:ReferenceLookupType = new ReferenceLookupType("PATIENT_STATUS");
        public static const PATIENT_VISIT_INVALID_REASON:ReferenceLookupType = new ReferenceLookupType("PATIENT_VISIT_INVALID_REASON");
        public static const LIPID_PREFILLED_CODE:ReferenceLookupType = new ReferenceLookupType("LIPID_PREFILLED_CODE");

private var _value:String;

        public function ReferenceLookupType(value:String) : void
        {
            _value = value;
        }

        public function toString():String
        {
            return _value;
        }
    }
}

在mxml代码中:

[Bindable]
            private var refLookupType:ReferenceLookupType = ReferenceLookupType.LIPID_PREFILLED_CODE;


dispatcher(new ReferenceDataMessage(refLookupType, "RefData"));

我得到的错误是:

"Unable to create a new instance of type 'gov.hhs.cms.ehrds.datacollection.model.ReferenceLookupType'." faultCode="Client.Message.Encoding" faultDetail="Types cannot be instantiated without a public, no arguments constructor."


[RPC Fault faultString="Unable to create a new instance of type 'gov.hhs.cms.ehrds.datacollection.model.ReferenceLookupType'." faultCode="Client.Message.Encoding" faultDetail="Types cannot be instantiated without a public, no arguments constructor."]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()[E:\dev\4.0.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:345]
    at mx.rpc::Responder/fault()[E:\dev\4.0.0\frameworks\projects\rpc\src\mx\rpc\Responder.as:68]
    at mx.rpc::AsyncRequest/fault()[E:\dev\4.0.0\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:113]
    at NetConnectionMessageResponder/statusHandler()[E:\dev\4.0.0\frameworks\projects\rpc\src\mx\messaging\channels\NetConnectionChannel.as:609]
    at mx.messaging::MessageResponder/status()[E:\dev\4.0.0\frameworks\projects\rpc\src\mx\messaging\MessageResponder.as:264]

您能否帮助我在这里缺少什么以及如何以正确的方式在 Actionscript 中实现枚举。

谢谢

哈里什

【问题讨论】:

    标签: java apache-flex actionscript-3 enums


    【解决方案1】:

    枚举在 Flex/BlazeDS 中不能开箱即用。你必须做一些自定义魔法。

    到目前为止,该主题的权威来源是 Farrata Systems 的 this blog entry

    主要问题是任何通过网络发送的对象都必须有一个无参数的构造函数。枚举打破了这个规则。

    因此,您需要为 Enums 使用自定义序列化器/反序列化器。

    FWIW,我还建议您查看来自同一组的DTO2FX。它们将正确且自动地生成 Java 枚举的 actionscript 版本,以确保它们可以通过网络发送而不会出现问题。

    【讨论】:

    • 感谢您对此的意见...我之前遇到过这篇文章,但我认为应该有一种更简单、更容易的方法......我会尝试并告诉我..再次感谢
    • 很遗憾,这是我所知道的最简单的方法。
    猜你喜欢
    • 2013-02-16
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多