I am new to Spring MVC. I am writing an app that uses Spring, Spring MVC and JPA/Hibernate I don\'t know how to make Spring MVC set a value coming from a dropdown into a mod
The trick as you have already noted is to register a custom converter which will convert the id from the drop down into a Custom instance.
You can write a custom converter this way:
public class IdToCustomerConverter implements Converter<String, Customer>{
@Autowired CustomerRepository customerRepository;
public Customer convert(String id) {
return this.customerRepository.findOne(Long.valueOf(id));
}
}
Now register this converter with Spring MVC:
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="IdToCustomerConverter"/>
</list>
</property>
</bean>