原文转自:https://blog.csdn.net/hongxingxiaonan/article/details/50282001
看项目看到关于这个的使用,才在网上搜了一下。
SpringMVC中WebDataBinder的应用及原理
Controller方法的参数类型可以是基本类型,也可以是封装后的普通Java类型。若这个普通Java类型没有声明任何注解,则意味着它的每一个属性都需要到Request中去查找对应的请求参数。众所周知,无论客户端传入的是什么类型的请求参数,最终都要以字节的形式传给服务端。而服务端通过Request的getParameter方法取到的参数也都是字符串形式的结果。所以,需要有一个把字符串形式的参数转换成服务端真正需要的类型的转换工具,在spring中这个转换工具为WebDataBinder。
WebDataBinder不需要我们自己去创建,我们只需要向它注册参数类型对应的属性编辑器PropertyEditor。PropertyEditor可以将字符串转换成其真正的数据类型,它的void setAsText(String text)方法实现数据转换的过程。
具体的做法是,在Controller中声明一个InitBinder方法,方法中利用WebDataBinder将自己实现的或者spring自带的PropertyEditor进行注册。像下面这样:
-
-
public void initBinder(WebDataBinder binder) throws Exception {
-
binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));
-
binder.registerCustomEditor(Date.class, new CustomDateEditor( new SimpleDateFormat( "yyyy-MM-dd"), true));
-
}
处理没有任何注解的普通Java类型的参数解析器是ModelAttributeMethodProcessor,下面是参加解析方法的代码:
-
public final Object resolveArgument(
-
MethodParameter parameter, ModelAndViewContainer mavContainer,
-
NativeWebRequest request, WebDataBinderFactory binderFactory)
-
throws Exception {
-
-
String name = ModelFactory.getNameForParameter(parameter);
-
Object target = (mavContainer.containsAttribute(name)) ?
-
mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, request);
-
-
WebDataBinder binder = binderFactory.createBinder(request, target, name);
-
if (binder.getTarget() != null) {
-
bindRequestParameters(binder, request);
-
validateIfApplicable(binder, parameter);
-
if (binder.getBindingResult().hasErrors()) {
-
if (isBindExceptionRequired(binder, parameter)) {
-
throw new BindException(binder.getBindingResult());
-
}
-
}
-
}
-
-
mavContainer.addAllAttributes(binder.getBindingResult().getModel());
-
return binder.getTarget();
-
}
每次请求到来后的参数解析都会利用WebDataBinderFactory创建一个binder对象,然后从这个binder中取得最终解析好的参数对象。WebDataBinderFactory是在InvocableHandlerMethod中定义的,即不同的Controller方法有着不同的WebDataBinderFactory。其实创建binder的同时还对binder进行了初始化,这个初始化过程就会执行Controller中的InitBinder方法。InitBinderDataBinderFactory实现了初始化binder的方法:
-
public void initBinder(WebDataBinder binder, NativeWebRequest request) throws Exception {
-
for (InvocableHandlerMethod binderMebinderMethod thod : this.binderMethods) {
-
if (isBinderMethodApplicable(binderMethod, binder)) {
-
Object returnValue = binderMethod.invokeForRequest(request, null, binder);
-
if (returnValue != null) {
-
throw new IllegalStateException( "@InitBinder methods should return void: " + binderMethod);
-
}
-
}
-
}
-
}
上面方法中的binderMethods就是在Controller中定义的InitBinder方法,并且binderMethod 同Controller中的其他方法一样也是InvocableHandlerMethod。从上面的代码可以看出,InitBinder方法可以声明多个,WebDataBinderFactory初始化binder的时候会分别调用每个InitBinder方法。而我们在初始化的过程中使用了binder.registerCustomEditor,间接地向BeanWrapperImpl中注册了传入的PropertyEditor,以便在参数类型转换的时候使用。
还记得刚才的ModelAttributeMethodProcessor解析参数时,创建binder之后调用了bindRequestParameters实现了请求参数的绑定,它的子类ServletModelAttributeMethodProcessor重写了这个方法:
-
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
-
ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
-
ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
-
servletBinder.bind(servletRequest);
-
}
不论是父类还是子类,其实都是调用了binder的bind方法。下面是ServletRequestDataBinder的bind方法
-
public void bind(ServletRequest request) {
-
MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
-
MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
-
if (multipartRequest != null) {
-
bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
-
}
-
addBindValues(mpvs, request);
-
doBind(mpvs);
-
}
经过了bindRequestParameters方法的处理,现在binder中target(即HandlerMethod的参数)已经包含了Request中的请求参数。
那么,现在还有一个问题,
InvocableHandlerMethod中的WebDataBinderFactory是如何来的呢?它的创建过程在RequestMappingHandlerAdapter(本文所有逻辑过程均假定使用RequestMappingHandlerAdapter):
-
private WebDataBinderFactory getDataBinderFactory(HandlerMethod handlerMethod) throws Exception {
-
Class<?> handlerType = handlerMethod.getBeanType();
-
Set<Method> methods = this.dataBinderFactoryCache.get(handlerType);
-
if (methods == null) {
-
methods = HandlerMethodSelector.selectMethods(handlerType, INIT_BINDER_METHODS);
-
this.dataBinderFactoryCache.put(handlerType, methods);
-
}
-
List<InvocableHandlerMethod> binderMethods = new ArrayList<InvocableHandlerMethod>();
-
for (Method method : methods) {
-
InvocableHandlerMethod binderMethod = new InvocableHandlerMethod(handlerMethod.getBean(), method);
-
binderMethod.setHandlerMethodArgumentResolvers( this.initBinderArgumentResolvers);
-
binderMethod.setDataBinderFactory( new DefaultDataBinderFactory( this.webBindingInitializer));
-
binderMethod.setParameterNameDiscoverer( this.parameterNameDiscoverer);
-
binderMethods.add(binderMethod);
-
}
-
return createDataBinderFactory(binderMethods);
-
}