These are all valid questions, and I spent quite some time recently looking for easy solution to this "string trimming" issue, with no promising results.
I believe there's no elegant way of doing it, at least not in Spring 3.0. The best I could find involves the following steps:
1. Drop <mvc:annotation-driven/> namespace configuration.
2. Manually setup all the required Spring MVC infrastructure beans: FormattingConversionServiceFactoryBean, LocalValidatorFactoryBean, DefaultAnnotationHandlerMapping, AnnotationMethodHandlerAdapter, various types of HttpMessageConverter beans, etc.
3. Among all these infrastructure beans there will be a ConfigurableWebBindingInitializer. It has a property called "propertyEditorRegistrar" - pass it an instance of custom implementation of PropertyEditorRegistrar interface that registers StringTrimmerEditor:
Code:
public class StringTrimmerEditorRegistrar implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(String.class, new StringTrimmerEditor(" \t\r\n\f", true));
}
}
All this setup seems like a lot of work for something as trivial as string trimming, and it is ugly as hell. So if you're reading this post and you know a better solution, you're more than welcome to share.