【问题标题】:Spring ElementDecoration and AjaxEventDecoration incompatible with Dijit?Spring ElementDecoration 和 AjaxEventDecoration 与 Dijit 不兼容?
【发布时间】:2026-01-06 20:55:02
【问题描述】:

我正在使用 Spring 3.0.5、Webflow 2.3.0 和 Spring JS 进行装饰。

我的一个表格有以下代码:

<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({
    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>

目的是根据下拉菜单中选择的内容呈现表单的第二部分。这可以通过对下拉列表的 onChange 进行 AJAX 调用来实现,或者在 noscript 的情况下 - 按下“选择”按钮。

事物的 noscript 方面有效。我在下拉列表中选择了一些内容,按“选择”,然后刷新时呈现表单的其余部分。

但是,AJAX 调用在其请求中没有刚刚选择的值。就好像 AJAX 调用发生在 Dijit 组件更新其表示之前。这些组件是否与我使用它们的方式兼容?

如果满足以下条件,我可以让它工作:

  1. 我没有将组件装饰为 Dijit 组件,而只是使用 onclick AJAX 装饰的普通下拉菜单。
  2. 我将 AJAX 调用放在一个按钮上,而不是放在 Dijit onChange 上

【问题讨论】:

    标签: spring dojo spring-webflow spring-js


    【解决方案1】:

    遇到了同样的问题,解决方法是在隐藏字段中设置选定的值,而 Spring.AjaxEventDecoration 会负责其余的工作。

                    <input id="vehicleType" name="vehicleType" type="hidden" />
                        <select id="selectVehicle">
                            <c:forEach var="vehicle" items="${vehicleTypes}">
                                <option value="${vehicle.type}"><fmt:message key="${vehicle.description}" /></option>
                            </c:forEach>
                        </select>
                    </p>
                    <script type="text/javascript">
                        Spring.addDecoration(new Spring.ElementDecoration({
                            elementId : 'selectVehicle',
                            widgetType : "dijit.form.Select",
                            widgetAttrs : {
                                value : "${vehicleType}",
                                onChange : function(newValue){
                                document.getElementById('vehicleType').value=dijit.byId('selectVehicle').get('value');}
                            }                           
                        }));
                        Spring.addDecoration(new Spring.AjaxEventDecoration({
                            elementId : "selectVehicle",
                            formId : "quote",
                            event : "onChange",
                            params : {
                                _eventId : "dropDownVehicleSelectionChange",
                                fragments : "body"
    
                            }
                        }));
                    </script>
    

    【讨论】: