0

我有一个 Spring MVC form:select,它form:optionsList<Custom_Object>. 在下面的代码中List<Custom_Object>命名LOCALIZATION_LIST

path属性form:select用于设置选择的选项。

<form:form action="editNode.do" method="post" name="editNodeForm" commandName="editElementDetails">
  <table>
    <tr>
      <td>Data Type</td>
      <td>
       <form:select path="datatype" onchange="" cssClass="large" id="datatypes">
        <c:if test="${! empty LOCALIZATION_LIST}">
          <form:options items="${LOCALIZATION_LIST}" itemLabel="local_Name" itemValue="local_Name"/>
        </c:if>
       </form:select>
      </td>
    </tr>   
  </table>
</form:form>

现在我的问题是,可能存在一个场景,editElementDetails.datatype其中可能包含一个根本不存在的值LOCALIZATION_LIST。所以目前 Spring MVC 显示LOCALIZATION_LIST为选中的第一个元素。

有没有办法可以确定用于设置所选对象的绑定操作是否form:options成功form:select

因此,当绑定不成功时,我可以添加一个额外的表单:表单中带有新值的选项。

4

2 回答 2

0

您想要实现的是验证表单提交的过程,这是我所理解的(也许你们都错了)。您可以在这里使用Spring-Validator,这是一个关于使用它的不错的教程。

春季表单验证。希望这可以帮助。

于 2013-01-18T10:38:38.017 回答
0

注意:回答我自己的问题

我现在通过手动搜索传入的命令对象字段值来解决这个问题,如果没有找到,则添加一个与之对应LOCALIZATION_LIST的额外内容。form:option

我希望为此找到一个 JSTL/Spring-MVC 开箱即用的解决方案。

这是代码:

<form:form action="editNode.do" method="post" name="editNodeForm" commandName="editElementDetails">
  <table>
    <tr>
      <td>Data Type</td>
      <td>
       <form:select path="datatype" onchange="" cssClass="large" id="datatypes">
        <c:if test="${! empty LOCALIZATION_LIST}">
          <c:set var="contains" value="false" />
          <c:forEach var="item" items="${LOCALIZATION_LIST}">
            <c:if test="${item eq editElementDetails.datatype}">
              <c:set var="contains" value="true" />
            </c:if>
          </c:forEach>
          <c:if test="${ !contains }">
            <form:option label="${editElementDetails.datatype}" value="${editElementDetails.datatype}"/>
          </c:if>
          <form:options items="${LOCALIZATION_LIST}" itemLabel="local_Name" itemValue="local_Name"/>
        </c:if>
       </form:select>
      </td>
    </tr>   
  </table>
</form:form>
于 2013-01-19T07:51:45.517 回答