我有一个Spring form:select
form:options
与List<Custom_Object>
绑定。 List<Custom_Object>
在下面的代码中命名为LOCALIZATION_LIST
。
path
的{{1}}属性用于设置所选选项。
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
中根本不存在的值的情况。因此,目前Spring MVC显示了LOCALIZATION_LIST
的第一个元素。
有没有办法可以弄清楚在LOCALIZATION_LIST
的{{1}}中设置所选对象的绑定操作是否成功?
因此,当绑定不成功时,我可以在表单中添加一个额外的表单:选项和新值。
答案 0 :(得分:0)
你想要实现的是验证表格提交的过程,这是我理解的(也许你错了)。你可以使用Spring-Validator这里有一个关于使用它的好教程。
Spring Form validation。希望这会有所帮助。
答案 1 :(得分: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>