here is my form :
<form:form modelAttribute="fooDTO">
fooCountry:
<form:select path="country">
<form:options items="${countries}" itemLabel="shortName" itemValue="id"/>
</form:select>
here is the backing pojo :
public class FooDTO
{
private Country country;
//getters and setters present
}
The selected option defaults to the country value in fooDTO, which is good. But the binding then fails when submitting the form – I get the aformentioned error, do I have to register a custom editor in a binder, or is there a simpler method ? Country is pretty much as you’d expect, and countries is indeed a list of countries populated in the controller …
Spring 3 introduced the Converter SPI which makes this quite easy. Have a look at 6.5 in the documentation
Taking source from the docs and putting in your country, you would do something like
Then in the xml config you would configure the converter
As GriffeyDog pointed out, you may want to put the country id in for the select path so you can get the Country by ID or something instead of whatever is returned by toString() of your Country object.