4/08/2016

How to set selected value in spring form select?

In general i have a list based on the list i need to populate the select box .There should be one default value which is not in the list, ie Select Country
<form:select path="country">
   <form:option value="NONE" label="Select Country"/>
   <form:options items="${countryList}" />
</form:select>
Now we have the value of country country code or id and based on the value selected values needs to be displayed.When we return the control to the particular page make sure that object bound to that page has proper values set for the variable defined in path attribute.
Lets take the example below
<form:select path="countryCode">
     <form:options items="${countryList}"  itemLabel=countryName itemValue=countryCode/>
</form:select>
We just need to populate the country code which needs to selected along with country list in object bound to that page,Rest everything is taken care by spring

CommentsComments