0

In the following controller

@Controller
public class MyFormController {
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
    // ...
}

The class does not inherit any other class or it doesn't have any WebDataBinder instance variable. How come then the custom editors are stored and where?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
user1539343
  • 1,569
  • 6
  • 28
  • 45

1 Answers1

-1

The Spring MVC infrastructure is complex. There are a lot of pieces that come together to invoke your @Controller handler method. I explain some of it in my answer here.

In summary, Spring MVC scans your @Controller classes for @RequestMapping annotated methods. It creates mappings for these in a RequestMappingHandlerMapping and uses a RequestMappingHandlerAdapter to dispatch to them, ie. invoke your handler methods.

Before it invokes the method, it goes through a couple of housekeeping steps. You can see these in the source code, here. The short version is:

  1. It wraps the HttpServletRequest and HttpServletResponse in an adapter.
  2. It creates factories producing Model and WebDataBinder instances (the latter come from your @InitBinder methods).
  3. It creates a ServletInvocableHandlerMethod which encapsulates the invocation of your handler method with HandlerMethodArgumentResolver to generate arguments for your handler method and HandlerMethodReturnValueHandler to process the return value of your handler method.
  4. It creates a ModelAndViewContainer which will potentially be used to later render a view as the HTTP response.
  5. It prepares an async environment in case the request uses async components.

Then it invokes the method.

That WebDataBinder object you're curious about is stored and used in various places in the execution context described above.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724