SpringMVC中WebDataBinder的应用及原理

 Controller方法的参数类型可以是基本类型,也可以是封装后的普通Java类型。若这个普通Java类型没有声明任何注解,则意味着它的每一个属性都需要到Request中去查找对应的请求参数。众所周知,无论客户端传入的是什么类型的请求参数,最终都要以字节的形式传给服务端。而服务端通过Request的getParameter方法取到的参数也都是字符串形式的结果。所以,需要有一个把字符串形式的参数转换成服务端真正需要的类型的转换工具,在spring中这个转换工具为WebDataBinder。
 
   WebDataBinder不需要我们自己去创建,我们只需要向它注册参数类型对应的属性编辑器PropertyEditor。PropertyEditor可以将字符串转换成其真正的数据类型,它的void setAsText(String text)方法实现数据转换的过程。
 
  具体的做法是,在Controller中声明一个InitBinder方法,方法中利用WebDataBinder将自己实现的或者spring自带的PropertyEditor进行注册。像下面这样:
  
[java] view plain copy
 
  1. @InitBinder  
  2. public void initBinder(WebDataBinder binder) throws Exception {  
  3.     binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));  
  4.     binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));  
  5. }  
  
  处理没有任何注解的普通Java类型的参数解析器是ModelAttributeMethodProcessor,下面是参加解析方法的代码:
[java] view plain copy
 
  1. public final Object resolveArgument(  
  2.         MethodParameter parameter, ModelAndViewContainer mavContainer,  
  3.         NativeWebRequest request, WebDataBinderFactory binderFactory)  
  4.         throws Exception {  
  5.   
  6.     String name = ModelFactory.getNameForParameter(parameter);  
  7.     Object target = (mavContainer.containsAttribute(name)) ?  
  8.             mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, request);  
  9.   
  10.     WebDataBinder binder = binderFactory.createBinder(request, target, name);  
  11.     if (binder.getTarget() != null) {  
  12.         bindRequestParameters(binder, request);  
  13.         validateIfApplicable(binder, parameter);  
  14.         if (binder.getBindingResult().hasErrors()) {  
  15.             if (isBindExceptionRequired(binder, parameter)) {  
  16.                 throw new BindException(binder.getBindingResult());  
  17.             }  
  18.         }  
  19.     }  
  20.   
  21.     mavContainer.addAllAttributes(binder.getBindingResult().getModel());  
  22.     return binder.getTarget();  
  23. }  

  每次请求到来后的参数解析都会利用WebDataBinderFactory创建一个binder对象,然后从这个binder中取得最终解析好的参数对象。WebDataBinderFactory是在InvocableHandlerMethod中定义的,即不同的Controller方法有着不同的WebDataBinderFactory。其实创建binder的同时还对binder进行了初始化,这个初始化过程就会执行Controller中的InitBinder方法。InitBinderDataBinderFactory实现了初始化binder的方法:
[java] view plain copy
 
  1. public void initBinder(WebDataBinder binder, NativeWebRequest request) throws Exception {  
  2.     for (InvocableHandlerMethod binderMebinderMethod thod : this.binderMethods) {  
  3.         if (isBinderMethodApplicable(binderMethod, binder)) {  
  4.             Object returnValue = binderMethod.invokeForRequest(request, null, binder);  
  5.             if (returnValue != null) {  
  6.                 throw new IllegalStateException("@InitBinder methods should return void: " + binderMethod);  
  7.             }  
  8.         }  
  9.     }  
  10. }  
   
      上面方法中的binderMethods就是在Controller中定义的InitBinder方法,并且binderMethod 同Controller中的其他方法一样也是InvocableHandlerMethod。从上面的代码可以看出,InitBinder方法可以声明多个,WebDataBinderFactory初始化binder的时候会分别调用每个InitBinder方法。而我们在初始化的过程中使用了binder.registerCustomEditor,间接地向BeanWrapperImpl中注册了传入的PropertyEditor,以便在参数类型转换的时候使用。
 
      还记得刚才的ModelAttributeMethodProcessor解析参数时,创建binder之后调用了bindRequestParameters实现了请求参数的绑定,它的子类ServletModelAttributeMethodProcessor重写了这个方法:
[java] view plain copy
 
  1. protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {  
  2.     ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);  
  3.     ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;  
  4.     servletBinder.bind(servletRequest);  
  5. }  
    
     不论是父类还是子类,其实都是调用了binder的bind方法。下面是ServletRequestDataBinder的bind方法
[java] view plain copy
 
  1. public void bind(ServletRequest request) {  
  2.     MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);  
  3.     MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);  
  4.     if (multipartRequest != null) {  
  5.         bindMultipart(multipartRequest.getMultiFileMap(), mpvs);  
  6.     }  
  7.     addBindValues(mpvs, request);  
  8.     doBind(mpvs);  
  9. }  
 
     这个方法跟依赖注入的过程非常相似,依赖注入是根据属性在容器中找到满足条件的对象,然后设置到当前的bean中。而上面的方法不是在容器中查找,而是从Request中获取,即把Request中的请求参数注入到binder的target中去。此时进行类型转换的就是刚刚注册的PropertyEditor,因为InitBinder方法每次都会执行,所以使用者可以在每个Controller中对相同类型的参数定义不同的参数转换方式。

    经过了bindRequestParameters方法的处理,现在binder中target(即HandlerMethod的参数)已经包含了Request中的请求参数。
 
      那么,现在还有一个问题,InvocableHandlerMethod中的WebDataBinderFactory是如何来的呢?它的创建过程在RequestMappingHandlerAdapter(本文所有逻辑过程均假定使用RequestMappingHandlerAdapter):
 
[java] view plain copy
 
  1. private WebDataBinderFactory getDataBinderFactory(HandlerMethod handlerMethod) throws Exception {  
  2.     Class<?> handlerType = handlerMethod.getBeanType();  
  3.     Set<Method> methods = this.dataBinderFactoryCache.get(handlerType);  
  4.     if (methods == null) {  
  5.         methods = HandlerMethodSelector.selectMethods(handlerType, INIT_BINDER_METHODS);  
  6.         this.dataBinderFactoryCache.put(handlerType, methods);  
  7.     }  
  8.     List<InvocableHandlerMethod> binderMethods = new ArrayList<InvocableHandlerMethod>();  
  9.     for (Method method : methods) {  
  10.         InvocableHandlerMethod binderMethod = new InvocableHandlerMethod(handlerMethod.getBean(), method);  
  11.         binderMethod.setHandlerMethodArgumentResolvers(this.initBinderArgumentResolvers);  
  12.         binderMethod.setDataBinderFactory(new DefaultDataBinderFactory(this.webBindingInitializer));  
  13.         binderMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);  
  14.         binderMethods.add(binderMethod);  
  15.     }  
  16.     return createDataBinderFactory(binderMethods);  
  17. }  
      
原文地址:https://www.cnblogs.com/kms1989/p/6738638.html