I have a Spring MVC form:select whose form:options are bind with a List<Custom_Object>. The List<Custom_Object> is named as LOCALIZATION_LIST in the code below.
The path attribute of form:select is used to set the selected option.
<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>
Now my problem is that there might be a scenario when editElementDetails.datatype may contain a value which is not there in LOCALIZATION_LIST at all. So currently Spring MVC shows the first element of LOCALIZATION_LIST as selected.
Is there a way I can figure out whether the bind action for setting the selected object in form:options of form:select was successful or not?
So that when the binding was not successful, I can then add one extra form:option with the new value in the form.
Note: Answering my own question
I figured it out for now by manually searching for the incoming command object field value in the
LOCALIZATION_LISTand if not found, adding an extraform:optioncorresponding to it.I was hoping to find a JSTL/Spring-MVC out-of-box solution for this though.
Here is the code: