spring-mvcannotationsjavabeanspropertyeditor

Is there an annotation-based way to register PropertyEditors globally in Spring MVC 3.0?


I was wondering if there was a way of registering a PropertyEditor globally within Spring MVC 3.0 onwards. In the documentation their documentation they show how to use annotations to customise bean PropertyEditor on a per-controller basis, and -it seems to me- like an XML way of doing it globally. So I was wondering, is there a way, using just annotations to register PropertyEditors for all controllers without having to do a @InitBinder method for each one. Creating a common super-class with a @InitBinder method is not desirable either.

The other question on this subject was asked before Spring 3.0 was released.


Solution

  • package com.projectr.web;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.springframework.beans.propertyeditors.CustomBooleanEditor;
    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.beans.propertyeditors.CustomNumberEditor;
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.support.WebBindingInitializer;
    import org.springframework.web.context.request.WebRequest;
    import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
    
    /**
     * Shared WebBindingInitializer for custom property editors.
     * 
     * @author aramirez
     * 
     */
    public class CommonBindingInitializer implements WebBindingInitializer {
        public void initBinder(WebDataBinder binder, WebRequest request) {
            binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
            binder.registerCustomEditor(Long.class, null, new CustomNumberEditor( Long.class, null, true));
            binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
            binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", request.getLocale());
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
        }
    }
    

    In your application-context or dispatcher-servlet

      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="webBindingInitializer">
          <bean class="com.projectr.web.CommonBindingInitializer"/>
        </property>
      </bean>
    

    Annotation equivalent of the code above. Note that @ControllerAdvice is introduced in Spring 3.2.x

    @ControllerAdvice
    public class CommonBindingInitializer {
      @InitBinder
      public void registerCustomEditors(WebDataBinder binder, WebRequest request) {
        binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
        binder.registerCustomEditor(Long.class, null, new CustomNumberEditor( Long.class, null, true));
        binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
        binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", request.getLocale());
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
      }
    }