How can i register a global custom editor in Spring-MVC?

后端 未结 3 1278
眼角桃花
眼角桃花 2020-12-02 15:05

I use the following custom editor in MANY Spring-MVC controllers according to:

A controller

binder.registerCustomEditor(BigDecimal.class, new CustomN         


        
3条回答
  •  生来不讨喜
    2020-12-02 15:17

    Starting Spring 3.2, you can use @ControllerAdvice instead of using @ExceptionHandler, @InitBinder, and @ModelAttribute in each Controller. They will be applied to all @Controller beans.

    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.InitBinder;
    import org.springframework.web.context.request.WebRequest;
    
    @ControllerAdvice
    public class GlobalBindingInitializer {
      @InitBinder
      public void registerCustomEditors(WebDataBinder binder, WebRequest request) {
        binder.registerCustomEditor(BigDecimal.class, new  CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true));
      }
    }
    

    If you had started out with Spring Roo generated code, or limit the annotations scanned by component-scan using include-filter, then add the required filter in webmvc-config.xml

    
    
      
      
      
    
    

提交回复
热议问题