SpringMVC中WebDataBinder的应用及原理
- @InitBinder
- 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));
- }
- 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);
- }
- }
- }
- }
- protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
- ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
- ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
- servletBinder.bind(servletRequest);
- }
- 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);
- }
这个方法跟依赖注入的过程非常相似,依赖注入是根据属性在容器中找到满足条件的对象,然后设置到当前的bean中。而上面的方法不是在容器中查找,而是从Request中获取,即把Request中的请求参数注入到binder的target中去。此时进行类型转换的就是刚刚注册的PropertyEditor,因为InitBinder方法每次都会执行,所以使用者可以在每个Controller中对相同类型的参数定义不同的参数转换方式。
经过了bindRequestParameters方法的处理,现在binder中target(即HandlerMethod的参数)已经包含了Request中的请求参数。
- 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);
- }
SpringMVC中WebDataBinder的应用及原理的更多相关文章
- 详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析]
		目录 前言 现象 源码分析 HandlerMethodArgumentResolver与HandlerMethodReturnValueHandler接口介绍 HandlerMethodArgumen ... 
- 详解SpringMVC中Controller的方法中参数的工作原理
		Spring MVC中Controller的处理方法的参数可以是Integer,String,自定义对象,ServletRequest,ServletResponse,ModelAndView等等,非 ... 
- 详解SpringMVC中Controller的方法中参数的工作原理——基于maven
		转自:http://www.tuicool.com/articles/F7byQn 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:ht ... 
- 【MVC - 参数原理】详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析]
		前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/spring ... 
- SpringMVC中Controller
		详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析] 目录 前言 现象 源码分析 HandlerMethodArgumentResolver与HandlerMethodR ... 
- SpringMVC中注解@RequestBody和@ResponseBody的使用区别
		首先上源码 在面试时经常会问到我们如何使用SpringMVC将Http请求转换为java对象,或者又是问如何将结果转换为java的呢? SpringMVC在接收到请求之后HandlerMapping像 ... 
- 8.springMVC中的RESTful架构风格
		RESTful架构:是一种设计的风格,并不是标准,只是提供了一组设计原则和约束条件,也是目前比较流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. 关于 ... 
- JavaEE开发之SpringMVC中的静态资源映射及服务器推送技术
		在上篇博客中,我们聊了<JavaEE开发之SpringMVC中的自定义拦截器及异常处理>.本篇博客我们继续的来聊SpringMVC的东西,下方我们将会聊到js.css这些静态文件的加载配置 ... 
- 将SpringMVC中的HttpMessageConverter替换为Gson
		读者们看到这个标题也许会感到奇怪,SpringMVC中默认的HttpMessageConverter不是Jackson吗,但是我在使用的过程中发现Jackson并不好用,如果有一些复杂的嵌套类型,当然 ... 
随机推荐
- PyMySQL防止SQL注入
			一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ... 
- DP专题·四(树形dp)
			1.poj 115 TELE 题意:一个树型网络上有n个结点,1~n-m为信号传送器,n-m+1~n为观众,当信号传送给观众后,观众会付费观看,每铺设一条道路需要一定费用.现在求以1为根,使得收到观众 ... 
- 第二课客户端链接Linux系统
			使用Putty客户端软件连接Linux主机 使用rpm –qa | grep ssh命令查看是否已经安装ssh服务,如下图是已经安装了ssh服务,如果未列出相关的openssh那么代表未安装这时候就需 ... 
- cdoj1344卿学姐种美丽的花
			地址:http://acm.uestc.edu.cn/#/problem/show/1344 题目: 卿学姐种美丽的花 Time Limit: 8000/4000MS (Java/Others) ... 
- docker 在windows上的使用
			Docker ToolBox 安装 1.首先,安装Docker ToolBox,其中包含了Docker三剑客: docker , docker-machine 和 docker-compose . 安 ... 
- shell脚本实现进度条
			使用shell脚本编写进度条 可已加入到shell脚本当中 主要作用:好看 美观 没毛用 (一) 普通进度条: #!/bin/bashb='' for ((i=0;$i<=20;i++)) do ... 
- Spring中的@Transactional以及事务的详细介绍
			首先来说下事务,说到事务就不得不说它的四个特性(acid): 一.特性 1.原子性(atomicity):一个事务当作为一个不可分割的最小工作单元,一组操作要么全部成功,要么全部失败. 2.一致性(c ... 
- spring boot项目获取application配置文件参数的两种方式
			前言:了解过spring boot这个技术的,应该知道spring boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件**.properties的信息 ... 
- Android开发中的logcat工具使用
			http://os.51cto.com/art/200905/126051.htm 用adb直接查看log: adb logcat 清除之前的log: adb logcat -c 加过滤查看lo ... 
- JavaWeb -- Struts1 使用示例: 表单校验 防表单重复提交 表单数据封装到实体
			1. struts 工作流程图 超链接 2. 入门案例 struts入门案例: 1.写一个注册页面,把请求交给 struts处理 <form action="${pageContext ... 
