自定义数据类型转换器的开发步骤
2017-11-10 09:23
363 查看
自定义数据类型转换器的开发步骤:
1.编写一个类,实现converter接口
public class CustomDateConverter implements Converter<String,Date>{
@Override
public Date convert(String source) {
//使用自定义时间转换器,完成多种时间格式转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return sdf.parse(source);
} catch (ParseException e) {
//e.printStackTrace();
sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(source);
} catch (ParseException e1) {
sdf = new SimpleDateFormat("yyyy/MM/dd");
try {
return sdf.parse(source);
} catch (ParseException e3) {
sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
return sdf.parse(source);
} catch (ParseException e4) {
}
}
}
}
return new Date();//如果时间格式都不满足,则返回当前系统时间
}
}
2.配置一个转换工厂,使用自定义的转换器
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 -->
<property name="converters">
<list>
<!-- 日期类型转换 -->
<bean class="com.neuedu.mvcdemo.view.converter.CustomDateConverter"/>
</list>
</property>
</bean>
3.使用定义的转换工厂
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
4.文档头部定义:
<beans default-lazy-init="false"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
1.编写一个类,实现converter接口
public class CustomDateConverter implements Converter<String,Date>{
@Override
public Date convert(String source) {
//使用自定义时间转换器,完成多种时间格式转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return sdf.parse(source);
} catch (ParseException e) {
//e.printStackTrace();
sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(source);
} catch (ParseException e1) {
sdf = new SimpleDateFormat("yyyy/MM/dd");
try {
return sdf.parse(source);
} catch (ParseException e3) {
sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
return sdf.parse(source);
} catch (ParseException e4) {
}
}
}
}
return new Date();//如果时间格式都不满足,则返回当前系统时间
}
}
2.配置一个转换工厂,使用自定义的转换器
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 -->
<property name="converters">
<list>
<!-- 日期类型转换 -->
<bean class="com.neuedu.mvcdemo.view.converter.CustomDateConverter"/>
</list>
</property>
</bean>
3.使用定义的转换工厂
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
4.文档头部定义:
<beans default-lazy-init="false"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
相关文章推荐
- Struts2自定义类型转换器、自定义拦截器和用户输入数据的验证
- 写出自己自定义类型转换器的实现步骤及代码
- ABAP开发基础知识:3) 自定义数据类型(User-Defined Data Types)
- struts2自定义数据类型转换器------日期转换器
- Spring MVC自定义消息转换器(可解决Long类型数据传入前端精度丢失的问题)
- Android开发之使用Intent进行自定义类型数据传输
- iOS开发之数据存取3-CoreData自定义数据类型
- struts2实现自定义数据类型转换器
- Struts2笔记之自定义封装请求参数的数据类型转换器
- asp.net控件开发基础(9) ----------再谈属性,学习自定义类型转换器
- iOS开发之数据存取3-CoreData自定义数据类型
- iOS开发之数据存取3-CoreData自定义数据类型Transformable
- C++ Builder 6 进行 SOAP 开发(2) -- 通过 SOAP 传递自定义类型数据
- Dot Net WinForm 控件开发 (三) 自定义类型的属性需要自定义类型转换器
- 自定义类型转换器的步骤
- Struts2自定义类型转换器、自定义拦截器和用户输入数据的验证
- springMVC-自定义数据类型转换器
- 【IOS 开发】Object - C 入门 之 数据类型详解
- iOS开发-特殊数据类型-Id
- 【移动开发】SQLite数据库使用 数据类型小结