springMVC4(9)属性编辑器剖析入参类型转换原理

简介: <div class="markdown_views"><p>我们通过Http请求提交的参数都以字符串的形式呈现,但最终在springMVC的方法入参中,我们却能得到各种类型的数据,包括Number、Boolean、复杂对象类型、集合类型、Map类型等,这些都是springMVC内置的数据类型转换器帮我们完成的。springMVC的将请求数据绑定到方法入参的流程如下所示:</

我们通过Http请求提交的参数都以字符串的形式呈现,但最终在springMVC的方法入参中,我们却能得到各种类型的数据,包括Number、Boolean、复杂对象类型、集合类型、Map类型等,这些都是springMVC内置的数据类型转换器帮我们完成的。springMVC的将请求数据绑定到方法入参的流程如下所示:

Created with Raphaël 2.1.0数据绑定流程图解ServletRequestServletRequestDataBinderDataBinderConversionServiceConversionServiceValidatorValidatorBindingResultBindingResult请求数据提交数据类型转换格式化数据合法性验证生成数据绑定结果

在本文里,我们通过属性编辑器来理解springMVC的数据转换、绑定过程。

PropertyEditorRegistrySupport

而对于常见的数据类型,Spring在PropertyEditorRegistrySupport中提供了默认的属性编辑器,这些常见的数据类型如下图所示:
这里写图片描述
在PropertyEditorRegistrySupport中,有两个重要的Map类型成员变量:
1. private Map<Class<?>, PropertyEditor> defaultEditors:用于保存默认属性类型的编辑器,元素的key为属性类型,值为对应属性编辑器的实例
2. private Map<Class<?>, PropertyEditor> customEditors:用于保存用户自定义的属性编辑器,元素的键值和defaultEditors一致。

在PropertyEditorRegistrySupport中,有一个重要的成员方法:createDefaultEditors()来创建默认的属性编辑器,它的定义如下所示:

/**
 * Actually register the default editors for this registry instance.
 */
private void createDefaultEditors() {
    //创建一个HashMap存储默认的属性编辑器
    this.defaultEditors = new HashMap<Class<?>, PropertyEditor>(64);

    // 简单的属性编辑器,没有参数化功能,在JDK中没有包含下列任意目标类型的编辑器
    //这里和我们上表的资源类相对应
    this.defaultEditors.put(Charset.class, new CharsetEditor());
    this.defaultEditors.put(Class.class, new ClassEditor());
    this.defaultEditors.put(Class[].class, new ClassArrayEditor());
    this.defaultEditors.put(Currency.class, new CurrencyEditor());
    this.defaultEditors.put(File.class, new FileEditor());
    this.defaultEditors.put(InputStream.class, new InputStreamEditor());
    this.defaultEditors.put(InputSource.class, new InputSourceEditor());
    this.defaultEditors.put(Locale.class, new LocaleEditor());
    this.defaultEditors.put(Pattern.class, new PatternEditor());
    this.defaultEditors.put(Properties.class, new PropertiesEditor());
    this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());
    this.defaultEditors.put(TimeZone.class, new TimeZoneEditor());
    this.defaultEditors.put(URI.class, new URIEditor());
    this.defaultEditors.put(URL.class, new URLEditor());
    this.defaultEditors.put(UUID.class, new UUIDEditor());
    if (zoneIdClass != null) {
        this.defaultEditors.put(zoneIdClass, new ZoneIdEditor());
    }

    // 默认的集合类编辑器实例,这里和我们上表的集合类相对应
    // 我们能够通过注册自定义的相同类型属性编辑器来重写下面的默认属性编辑器
    this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));
    this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));
    this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));
    this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));
    this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));

    // 基本数据的数组类型的默认编辑器
    this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());
    this.defaultEditors.put(char[].class, new CharArrayPropertyEditor());

    this.defaultEditors.put(char.class, new CharacterEditor(false));
    this.defaultEditors.put(Character.class, new CharacterEditor(true));

    this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false));
    this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));

    // JDK中没有Number包装类的相关属性编辑器
    // 通过自定义我们的CustomNumberEditor来重写JDK默认的属性编辑器
    this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false));
    this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true));
    this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false));
    this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true));
    this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false));
    this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true));
    this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false));
    this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true));
    this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false));
    this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true));
    this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false));
    this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true));
    this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));
    this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));

    // 只有我们显式将configValueEditorsActive设为true,才会注册下面类型的编辑器
    if (this.configValueEditorsActive) {
        StringArrayPropertyEditor sae = new StringArrayPropertyEditor();
        this.defaultEditors.put(String[].class, sae);
        this.defaultEditors.put(short[].class, sae);
        this.defaultEditors.put(int[].class, sae);
        this.defaultEditors.put(long[].class, sae);
    }
}

PropertyEditor

PropertyEditor是Java原生的属性编辑器接口,它的核心功能是将一个字符串转换为一个java对象。
它的定义和常用方法如下所示:

public interface PropertyEditor {

    //设置属性的值,基本属性类型要以包装类传入
    void setValue(Object value);

    //返回属性的值,基本数据类型会被封装成相应的包装类
    Object getValue();

    //为属性提供一个表示初始值的字符串,属性编辑器以此值作为属性的默认值
    String getJavaInitializationString();

    //将属性对象用一个字符串表示,一遍外部的属性编辑器能以可视化的方式显示。
    //默认返回null,表示改属性不能以字符串形式表示
    String getAsText();

    //利用所给字符串text更新属性内部的值
    void setAsText(String text) throws java.lang.IllegalArgumentException;

}

实例解析自定义属性编辑器

1. 自定义编辑器类

它的一个核心实现类是PropertyEditorSupport,如果我们要编写自定义的属性编辑器,只需要继承这个类,然后重写setAsText方法即可。下面我们来看一个自定义属性编辑器的实例:尝试将字符串“myName,1995-01-01,15k”转换为Person POJO对象,Person对象的定义如下:

package com.mvc.model;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;

public class Person {
    private String name;
    private Date birthday;
    private Long salary;

    //ignore getter and setter 

    @Override
    public String toString() {
        return "Person [name=" + name + ", birthday=" + birthday + ", salary="
                + salary + "]";
    }

}

下面是我们自定义的属性编辑器:

public class MyEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        String[] values = text.split(",");
        Person person = new Person();
        person.setName(values[0]);
        try {
            person.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(values[1]));//格式化字符串并解析成日期类型
        } catch (ParseException e) {
            e.printStackTrace();
        }
        person.setSalary(Long.valueOf(values[2].replace("k", "000")));//转换为工资格式
        setValue(person);//调用setValue来将我们的Person对象设置为编辑器的属性值
        super.setAsText(text);
    }
}

2. 注册编辑器

自定义完属性编辑器后,我们需要将其注册才能生效,SpringMVC中使用自定义的属性编辑器有3种方法:

1. Controller方法中添加@InitBinder注解的方法

实例:

@InitBinder
public void initBinder(WebDataBinder binder) { 
  binder.registerCustomEditor(Person.class, new MyEditor());  
}

2. 实现 WebBindingInitializer接口

方法1是针对特定的控制器的,如果我们需要对全局控制器生效,可以编写自己的WebBindingInitializer,然后在spring容器中注册,如下所示:

public class MyWebBindingInitializer implements WebBindingInitializer {

  @Override
  public void initBinder(WebDataBinder binder, WebRequest request) { 
    binder.registerCustomEditor(Dept.class, new CustomDeptEditor());  
  }
}

在容器中注册:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="com.mvc.editor.MyWebBindingInitializer" />
    </property>
</bean>

3. @ControllerAdvice注解

我们可以通过此注解配置一个控制器增强,

@ControllerAdvice
public class InitBinderControllerAdvice {

  @InitBinder
  public void initBinder(WebDataBinder binder) { 
    binder.registerCustomEditor(Dept.class, new CustomDeptEditor());  
  }

}

我们需要将其纳入<context:component-scan>的扫描路径中才能生效。

从上面的分析我们能看到,springMVC注册了大量的数据类型编辑器,恰是通过这些属性编辑器,springMVC帮助我们完成了请求参数字符串到入参数据的绑定。在一篇文章里,我们会谈到SpringMVC对新的转换器框架的支持。

相关实践学习
基于小程序Serverless开发个人相册小程序
本场景基于小程序云Serverless+小程序开发者工具(IDE),快速搭建个人相册小程序
SAE的功能与使用入门
欢迎来到《SAE的功能与使用入门》,本课程是“云原生Serverless Clouder认证“系列中的第三阶段。课程将向您介绍阿里云Serverless应用引擎(SAE)服务相关的概念、特性与使用方式。通过课程将带您逐步深入探索Serverless世界,借助SAE服务,即使没有丰富的云计算和IT经验,也能够让开发人员在实际业务场景中便捷的掌握如何构建和部署应用程序,快速拥抱Serverless架构,将精力聚焦在应用代码和业务逻辑的实现上。 学习完本课程后,您将能够: 掌握Serverless应用引擎(SAE)的基本概念与核心优势 了解Serverless应用引擎(SAE)的核心功能 掌握使用Serverless应用引擎(SAE)的开发和部署流程 了解Serverless应用引擎(SAE)的适用场景和最佳实践 &nbsp;
目录
相关文章
|
1月前
|
前端开发 Java Maven
属性编辑器未在PropertyEditorManager中注册?
属性编辑器未在PropertyEditorManager中注册?
12 0
|
2月前
|
前端开发 JavaScript 数据库
前端 富文本编辑器原理
前端 富文本编辑器原理
31 0
|
5月前
|
前端开发 C# 开发工具
Unity快手上手【熟悉unity编辑器,C#脚本控制组件一些属性之类的】
Unity快手上手【熟悉unity编辑器,C#脚本控制组件一些属性之类的】
105 0
|
JavaScript 前端开发 API
如何使用 layui 的富文本编辑器组件?底层原理是什么?
如何使用 layui 的富文本编辑器组件?底层原理是什么?
412 0
|
Java Spring
Spring自定义属性编辑器及原理解释.md
Spring自定义属性编辑器及原理解释.md
|
JavaScript
SAP Cloud for Customer Rule Editor的使用方法和底层工作原理
SAP Cloud for Customer Rule Editor的使用方法和底层工作原理
SAP Cloud for Customer Rule Editor的使用方法和底层工作原理
|
Java Spring 数据格式
Spring的属性编辑器
bean类 import java.util.Date; public class Bean { private Date date; public Date getDate() { return date; ...
772 0