Announcement Announcement Module
No announcement yet.
Spring 3.x way to trim all text fields Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.x way to trim all text fields

    My goal is to trim leading and trailing whitespaces from all the input and textarea fields during form binding. I also need to be able to tweak this behavior (i.e. disable trimming for some particular field).

    I've googled a lot and searched this forum and the only solution I ended up with is registering StringTrimmerEditor property editor in FormController.

    But I'm worried about these concerns:
    1. FormController class hierarchy is deprecated since Spring 3.0 in favor of annotated controllers
    2. Spring 3.0 introduced type conversion which aims to substitute property editors

    I guess this could be dealt with ApplicationConversionService (that ROO installs as of version 1.1.1) by I'm not sure.

    So the questions are as follows:
    • Can I achieve my goal w/o using PropertyEditors?
    • Has FormattingConversionServiceFactory something to do with my problem?
    • Is there any trimming formatter provided with Spring-MVC out of the box to replace StringTrimmerEditor?

  • #2
    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:
    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.


    • #3
      Thanks for your reply, Osvaldas.

      Dropping <mvc:annotation-driven/> seems to be quite a load of work Moreover eventually I will end up with a property editor anyway, though as you know I'm trying to not to use property editors and move towards conversion service infrastructure. Thus this approach won't save me... unfortunately.

      I guess I would need to debug through the whole process of binding and conversion to understand how it is done and how to wedge into this process properly. I will post here as soon as I have any progress.


      • #4
        I just discovered an arguably cleaner solution to this. You can make all your controllers inherit from abstract superclass that initializes the DataBinder:

        public abstract class BaseController {
            public void initBinder(WebDataBinder binder) {
                binder.registerCustomEditor(String.class, new StringTrimmerEditor(" \t\r\n\f", true));


        • #5
          This solution still uses PropertyEditor while I want to use Conversion Service instead.