Spring JS Ajax 调用 - 枚举数据绑定无法转换但字符串有效

发布于 2024-12-03 08:20:01 字数 2188 浏览 5 评论 0原文

我使用带有 dijit 组件的 Spring 从模型中的枚举呈现选项列表:

<form:select path="selection">
                <form:option value="" label="Please select"></form:option>
                <form:options></form:options>
                </form:select>
                <noscript>
                    <button id="btnSelect" type="submit" name="_eventId_dropDownSelectionChange">Select</button>
                </noscript>
                <script type="text/javascript">
                    Spring.addDecoration(new Spring.ElementDecoration({
                        elementId : "selection",
                        widgetType : "dijit.form.Select",
                        widgetAttrs : {
                            forceWidth : true,
                            missingMessage : "<s:message code="NotEmpty" />"
                        }
                    }));
                    Spring.addDecoration(new Spring.AjaxEventDecoration({
                        elementId : "selection",
                        formId : "templateModel",
                        event : "onChange",
                        params : {  _eventId: "dropDownSelectionChange" }
                    }));
                </script>

当 Javascript 关闭时,来自 btnSelect 的帖子成功将所选项目转换为服务器端的枚举。也就是说,我的转换服务显示以下消息:

DEBUG: org.springframework.binding.mapping.impl.DefaultMapping - Adding mapping result [Success@15fddb33 mapping = parameter:'selection' -> selection, code = 'success', error = false, originalValue = 'IPSUM', mappedValue = 'IPSUM']

然而,当 Javascript 打开且帖子经过 XHR 时,枚举转换失败。转换服务显示此消息:

DEBUG: org.springframework.binding.mapping.impl.DefaultMapping - Adding mapping result [TargetAccessError@7b7d8769 mapping = parameter:'selection' -> selection, code = 'typeMismatch', error = true, errorCause = org.springframework.binding.expression.ValueCoercionException: Value could not be converted to target class; is a suitable type converter registered?, originalValue = 'selection', mappedValue = [null]]

我不明白如果他们都使用相同的转换服务,为什么会发生这种情况。我是否必须设置特殊配置以便 AJAX 调用可以绑定到枚举?

I'm using Spring with dijit components to render an option list from an enum in my model:

<form:select path="selection">
                <form:option value="" label="Please select"></form:option>
                <form:options></form:options>
                </form:select>
                <noscript>
                    <button id="btnSelect" type="submit" name="_eventId_dropDownSelectionChange">Select</button>
                </noscript>
                <script type="text/javascript">
                    Spring.addDecoration(new Spring.ElementDecoration({
                        elementId : "selection",
                        widgetType : "dijit.form.Select",
                        widgetAttrs : {
                            forceWidth : true,
                            missingMessage : "<s:message code="NotEmpty" />"
                        }
                    }));
                    Spring.addDecoration(new Spring.AjaxEventDecoration({
                        elementId : "selection",
                        formId : "templateModel",
                        event : "onChange",
                        params : {  _eventId: "dropDownSelectionChange" }
                    }));
                </script>

When Javascript is off, the post from btnSelect successfully converts the selected item to an enum on the server side. That is, my conversion service shows this message:

DEBUG: org.springframework.binding.mapping.impl.DefaultMapping - Adding mapping result [Success@15fddb33 mapping = parameter:'selection' -> selection, code = 'success', error = false, originalValue = 'IPSUM', mappedValue = 'IPSUM']

However, when Javascript is on and the post goes over XHR, the enum conversion fails. Conversion service showing this message:

DEBUG: org.springframework.binding.mapping.impl.DefaultMapping - Adding mapping result [TargetAccessError@7b7d8769 mapping = parameter:'selection' -> selection, code = 'typeMismatch', error = true, errorCause = org.springframework.binding.expression.ValueCoercionException: Value could not be converted to target class; is a suitable type converter registered?, originalValue = 'selection', mappedValue = [null]]

I don't understand why this is happening if they're both using the same conversion service. Do I have to set up a special configuration so that AJAX calls can bind to enums?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文