每当我们学习到一个新的东西时,都要有这样的诱惑,比如数据转换,什么是数据转换,什么时候使用数据转换?
数据转换顾名思义就是将一种数据类型转换成另一种数据类型,在我们界面输入时,都是在<Input>标签中输入内容,那输入的类型绝逼是String类型,那我们的底层是需要存入的是Date型,或者Integer,或者是将一系列的String类型封装成一个对象。那这就用到了数据类型转换。
Spring中数据类型转换方式有三种方式,分别是自定义转换器ConversionSevice,自定义编辑器@InitBinder,通过WebBindingInitializer接口装配全局自定义转换器。
假如将String类型转换成Date类型。
1.使用自定义转换器ConversionSevice
首先自定义的转换器的类,将String类型转换成Date类型
public class StringTODateConverter implements Converter<String,Date>{
//日期类型模板:yyyy-MM-dd
private String datePattern;
private void setDatePattern(String datePattern){
this.datePattern=datePattern;
}
@Override
public Date convert(String Date){
try{
SimpleDateFormat dateFormat=new SimpliDateFormat(this.datePattern);
return dateFormat.parse(date);
}
catch(Exception e){
e.printStackTrace();
System.out.println("日期格式转化失败");
return null;
}
}
}
再在SpringMVC配置文件中加入自定义字符转换器。
<!-- 装配自定义的类型转换器 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 自定义类型的转换器 -->
<bean id="conversionService"
class="org.springframework.core.convert.support.ConversionServiceFactory">
<property name="converters">
<list>
<bean class="com.sgf.springmvc.controller.StringToDateConserver"
p:datePattern="yyyy-MM-dd"></bean>
</list>
</property>
</bean>
2.自定义编辑器@InitBinder
新建DateEditor.java
package com.sgf.springmvc.controller;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//自定义编辑器
public class DateEditor extends PropertyEditorSupport{
//将传入的String类型转换成Date类型
@Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
try {
Date date=dateFormat.parse(text);
setValue(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在UserController()方法中添加initBinder()方法,并且使用@InitBinde注解,该注解会在控制器初始化时注册属性编辑器。WebDataBinder对象对象用于处理请求消息和处理方法的绑定工作。binder.registerCustomEditor()方法对传入的Date类型使用DateEditor类型进行转换。
//在控制器初始化时注册属性编辑器
@InitBinder
public void initBinder(WebDataBinder binder) {
//注册自定义编辑器
binder.registerCustomEditor(Date.class, new DateEditor());
}
3.通过WebBindingInitializer接口装配全局自定义转换器
在全局范围内使用自定义编辑器,则可通过WebBindingInitializer接口并实现该类中注册自定义编辑器完成。
package com.sgf.springmvc.controller;
import java.sql.Date;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
public class DateBindingInitiallizer implements WebBindingInitializer {
//实现WebBindingInitializer接口
@Override
public void initBinder(WebDataBinder binder, WebRequest arg1) {
//注册自定义编辑器
binder.registerCustomEditor(Date.class, new DateEditor());
}
}
在UserController类中不在使用@InitBinder注解,而是要在SpringMvc的配置文件中配置全局自定义编辑器。
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.sgf.springmvc.controller.DateBindingInitiallizer"></bean>
</property>
</bean>
使用了RequestMappingHandlerAdapter装配自定义编辑器,就不能在使用默认的配置方案 <mvc:annotation-driven></mvc:annotation-driven>。
三种转换器的优先顺序
①@InitBinder注解 ②自定义转换器ConversionSevice ③通过WebBindingInitializer接口装配全局自定义转换器